How to get Camera Intent to put a photo in internal memory?

I have been all this day and cannot make it work. It is used to work before the previous person who worked on it worked.

    cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
        String imageName = CustomApp.getTimeStamp(0) ;
        String path = CustomApp.getCurrentActivity().getDir("images", Context.MODE_WORLD_WRITEABLE).getPath()+File.separator+imageName;

        File file = new File(path) ;
        Uri img = Uri.fromFile(file) ;

        Intent passthruDataIntent = new Intent();

        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, img);
        CustomApp.getCurrentActivity().startActivityForResult(cameraIntent, CustomConstants.REQUESTCODE_CAMERA);

Similar code was posted here, but it doesn't seem to work on my kernel 4 in 4.2.2. I tried external storage, and then it works fine. Any insight into why this might not work would be very helpful. Thank.

+3
source share
3 answers

Internal storage is private for each application - a third-party camera application does not have the right to write to the internal storage.

+8
source

, , , .

, Android 6.0/API 23, , , . , (android.permission.CAMERA). , , android.permission.READ_EXTERNAL_STORAGE . , (, " " ). , , , : (1) (2) . , , , , .

, , , ,

Android - . - , / , , .

, , , ( ).

. , , , .

:

/**
 * A content provider that allows to store the camera image internally without requesting the
 * permission to access the external storage to take shots.
 */
public class CameraPictureProvider extends ContentProvider {
    private static final String FILENAME = "picture.jpg";

    private static final Uri CONTENT_URI = Uri.parse("content://xyz.example.app/cameraPicture");

    @Override
    public boolean onCreate() {
        try {
            File picture = new File(getContext().getFilesDir(), FILENAME);
            if (!picture.exists())
                if (picture.createNewFile()) {
                    getContext().getContentResolver().notifyChange(CONTENT_URI, null);
                    return true;
                }
        } catch (IOException | NullPointerException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Nullable
    @Override
    public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) throws FileNotFoundException {
        try {
            File picture = new File(getContext().getFilesDir(), FILENAME);
            if (!picture.exists())
                picture.createNewFile();
            return ParcelFileDescriptor.open(picture, ParcelFileDescriptor.MODE_READ_WRITE);
        } catch (IOException | NullPointerException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        return null;
    }

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

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, ContentValues values) {
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }
}

:

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

, - , :

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

, ( ).

, , 23 . , , .

+4

I had the same problem. I solved this by first saving the photos to external memory, and then copying them to internal memory. Hope this helps.

+1
source

All Articles