Store image from camera into private app cache directory

不羁岁月 提交于 2020-01-13 08:46:32

问题


I would like to capture an image from the camera and save it to the private app cache directory. I realize that I have to give the camera app permission to write to my private directory, so I added the FLAG_GRANT_WRITE_URI_PERMISSION flag.

What's happening is, that the camera app opens, I can take the picture, but when i click on the OK button, nothing happens. The camera app stays open. No log output. I guess it's because of a permission problem.

private void getCameraImage() {
    try {
        mTmpFile = File.createTempFile("tmp", ".jpg", getCacheDir());
        Uri imgUri = Uri.fromFile(mTmpFile);
        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        i.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
        i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        // i.setData(imgUri); // if uncommented, i get an ActivityNotFound Exception
        startActivityForResult(i, REQUEST_CAMERA);
    } catch (IOException e) {
        Log.e(TAG, "getCameraImage()", e);
        Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show();
    }
}

Any insights, on how I could correct that?

Edit:

When I change the directory to the public SD card, then it works fine.

mTmpFile = File.createTempFile("tmp", ".jpg", Environment.getExternalStorageDirectory());

Thanks Simon


回答1:


Instead of passing the Uri to store the picture you can use http://developer.android.com/reference/android/hardware/Camera.html#takePicture(android.hardware.Camera.ShutterCallback, android.hardware.Camera.PictureCallback, android.hardware.Camera.PictureCallback, android.hardware.Camera.PictureCallback)

and get the content in your app memory. From there you will be able to do anything you want.

Do not forget that camera applications are using also the gallery and the gallery app is not allowed to check your private directory.



来源:https://stackoverflow.com/questions/13407899/store-image-from-camera-into-private-app-cache-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!