将相机中的图像存储在新文件夹android

时间:2015-12-03 05:05:37

标签: android camera android-image android-external-storage

我正在创建一个应用程序,我希望用户选择保存其图像的文件夹。我首先想看看我是否可以弄清楚如何将图像保存在除默认文件夹之外的另一个文件夹中但我不断得到一个错误,无法弄清楚原因。

这是我必须启动摄像头意图并获取文件目录名称

的代码
 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);
        }
    }
}

有人可以帮我修复我的代码吗?

1 个答案:

答案 0 :(得分:2)

首先检查您在Manifest中的许可。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

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" +
                   file, 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();
        }
    }

问题出在你的祝酒词上。在Camera Intent中,我们将intent作为null,并且您正尝试从那里获取数据。你必须使用Uri。