Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store image from camera into private app cache directory

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

like image 337
SimonSays Avatar asked Nov 15 '12 23:11

SimonSays


People also ask

What is cache directory in Android?

Your Android phone's limited storage can fill up quickly. And one cause of this that's easy to overlook is the stored information that apps regularly create to run at their best. These temporary data files are known as a cache; a fair chunk of your Android phone's storage space might be filled up with cache files.

Where is app cache stored Android?

On Android Studio you can use Device File Explorer to view /data/data/your_app_package/cache.


1 Answers

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.

like image 79
danysz Avatar answered Oct 21 '22 20:10

danysz