我正在創建一個應用程序,我希望用戶選擇將文件保存在哪個文件夾中。我首先要查看是否可以找出如何將圖像保存在另一個文件夾中默認的文件夾,但我不斷得到一個錯誤,不知道爲什麼。從相機存儲圖像在新文件夾中android
這裏是我必須啓動相機意圖和獲取文件目錄名
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smart_wardrobe);
camera = (Button)findViewById(R.id.cameraBtn);
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
file = getOutputMediaFileUri();
intent.putExtra(MediaStore.EXTRA_OUTPUT, file);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
});
}
下面的代碼是爲uriFileDirectory
private static Uri getOutputMediaFileUri(){
return Uri.fromFile(getOutputMediaFile());
}
private static File getOutputMediaFile(){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
if (! mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
return mediaFile;
}
下面的代碼是我的onActivityResult方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
Toast.makeText(this, "There is someting wrong with your code", Toast.LENGTH_LONG).show();
}
}
if (requestCode == SELECT_PICTURE){
if (resultCode == RESULT_OK) {
images = data.getData();
selectedImagePath = getPath(images);
}
}
}
有人可以幫助修復我的代碼嗎?
你得到了什麼錯誤? – Jas
請張貼您的'日誌貓'。您還需要在清單中添加'<使用權限android:name =「android.permission.WRITE_EXTERNAL_STORAGE」/>'權限。 –
你有什麼錯誤? –