Skip to content Skip to sidebar Skip to footer

How To Have The Camera Intent Put A Photo Into Internal Storage?

I have been at this all day and can't seem to get it to work. It use to work before according to the previous person who worked on it. cameraIntent = new Intent('android.media.

Solution 1:

Internal storage is private for each app -- the third-party camera app has no rights to write to your internal storage.

Solution 2:

I was fighting with the same problem and got a solution much later that the question was asked, but I believe this might be useful to the community.

With the new dynamic permissions introduced in Android 6.0 / API level 23 the topic question has become particularly important, since you need to request the permissions at runtime and handle the both accepting and rejecting reactions of the user. To use the camera activity you need to ask for the corresponding permission first (android.permission.CAMERA). Then, if you store the picture in an external directory, the corresponding permission android.permission.READ_EXTERNAL_STORAGE also needed to be granted to your app by the user. A runtime permission request seems natural to the user at the moment when the user is about to perform the intended action (e.g., if the camera access permission request appears just after the button "Take photo" is pressed). However, if you use the external storage to save the camera picture, you need to ask at the same time for two permissions when your app takes a photo: (1) use the camera and (2) access the external storage. The latter might be frustrating since it is not necessarily clear why your app tries to reach the user files while the user expects just a photo to be taken.

The solution allowing to avoid the external storage and to save the camera picture directly consists in using the content providers. According to the storage options documentation,

Android provides a way for you to expose even your private data to other applications — with a content provider. A content provider is an optional component that exposes read/write access to your application data, subject to whatever restrictions you want to impose.

This is exactly what you need to allow to the camera activity to save the picture directly into the local storage of your app, so that you can easily access it then without requesting additional permissions (only the camera access needed to be granted).

A good article with a code example is provided here. The following generalized code inspired by this article is used in our app to do the trick.

The content provider class:

/**
 * A content provider that allows to store the camera image internally without requesting the
 * permission to access the external storage to take shots.
 */publicclassCameraPictureProviderextendsContentProvider {
    privatestatic final StringFILENAME = "picture.jpg";

    privatestatic final UriCONTENT_URI = Uri.parse("content://xyz.example.app/cameraPicture");

    @OverridepublicbooleanonCreate() {
        try {
            File picture = newFile(getContext().getFilesDir(), FILENAME);
            if (!picture.exists())
                if (picture.createNewFile()) {
                    getContext().getContentResolver().notifyChange(CONTENT_URI, null);
                    returntrue;
                }
        } catch (IOException | NullPointerException e) {
            e.printStackTrace();
        }
        returnfalse;
    }

    @Nullable@OverridepublicParcelFileDescriptoropenFile(@NonNullUri uri, @NonNullString mode) throws FileNotFoundException {
        try {
            File picture = newFile(getContext().getFilesDir(), FILENAME);
            if (!picture.exists())
                picture.createNewFile();
            returnParcelFileDescriptor.open(picture, ParcelFileDescriptor.MODE_READ_WRITE);
        } catch (IOException | NullPointerException e) {
            e.printStackTrace();
        }
        returnnull;
    }

    @Nullable@OverridepublicCursorquery(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        returnnull;
    }

    @Nullable@OverridepublicStringgetType(@NonNull Uri uri) {
        String lc = uri.getPath().toLowerCase();
        if (lc.endsWith(".jpg") || lc.endsWith(".jpeg"))
            return"image/jpeg";
        returnnull;
    }

    @Nullable@OverridepublicUriinsert(@NonNull Uri uri, ContentValues values) {
        returnnull;
    }

    @Overridepublic int delete(@NonNull Uri uri, String selection, String[] selectionArgs) {
        return0;
    }

    @Overridepublic int update(@NonNull Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return0;
    }
}

The content provider is needed to be declared in the app manifest:

<providerandroid:authorities="xyz.example.app"android:enabled="true"android:exported="true"android:name="xyz.example.app.CameraPictureProvider" />

Finally, to use the content provider in order to capture the camera picture, the following code is invoked from a calling activity:

IntenttakePictureIntent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, CameraPictureProvider.CONTENT_URI);
startActivityForResult(takePictureIntent, 0);

Please note that the camera permission request needed to be handled separately (it is not done in the presented code sample).

It is also worth noticing that the permission requests needed to be handled only if you are using build tools version 23 or higher. The same code is compatible with lower-level build tools, and is useful in case you are not bothered by the runtime permission requests but just want to avoid using the external storage.

Solution 3:

I had the same problem. I solved it by first saving the photos on external memory and then copied to internal memory. Hope this helps.

Post a Comment for "How To Have The Camera Intent Put A Photo Into Internal Storage?"