Skip to content Skip to sidebar Skip to footer

Capture Image Without Permission With Android 6.0

I need to let the user take a picture (from the gallery or from a camera app) with Android 6.0. Because I don't need to control the camera, I wanted to use an intent as describe he

Solution 1:

Try this:

IntentcameraIntent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
Filefile=newFile(getActivity().getApplicationContext().getExternalFilesDir(android.os.Environment.DIRECTORY_PICTURES).getAbsolutePath() + File.separator + "yourPicture.jpg");
Uriuri= Uri.fromFile(file);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, url);

This gives you access to an "external" storage writable for camera apps in this case, but the files are just visible from your app. To learn a little bit more about storage spaces in android see https://www.youtube.com/watch?v=C28pvd2plBA

Hope it helps you!

Solution 2:

It is possible if you on android 4.4+, you can specify MediaStore.EXTRA_OUTPUT, to be a file under your package-specific directories

Starting in Android 4.4, the owner, group and modes of files on external storage devices are now synthesized based on directory structure. This enables apps to manage their package-specific directories on external storage without requiring they hold the broad WRITE_EXTERNAL_STORAGE permission. For example, the app with package name com.example.foo can now freely access Android/data/com.example.foo/ on external storage devices with no permissions. These synthesized permissions are accomplished by wrapping raw storage devices in a FUSE daemon.

https://source.android.com/devices/storage/

Solution 3:

This is possible without either of the CAMERA and WRITE_EXTERNAL_STORAGE permissions.

You can create a temporary in your app's cache directory, and give other apps access to it. That makes the file writeable by the camera app:

FiletempFile= File.createTempFile("photo", ".jpg", context.getCacheDir());
tempFile.setWritable(true, false);

Now you just need to pass this file as the output file for the camera intent:

Intentintent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTempFile));  // pass temp file
startActivityForResult(intent, REQUEST_CODE_CAMERA);

Note: the file Uri won't be passed to you in the Activity result, you'll have to keep a reference to the tempFile and retrieve it from there.

Solution 4:

Since the camera app cannot access to the private dirs of your app, It's your applications duty to write the photo file to that directory. I'm not really sure about this but this works for me:

private File createImageFile()throws IOException {
    // Create an image file nameStringtimeStamp=newSimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault())
            .format(newDate());
    StringimageFileName="JPEG_" + timeStamp + "_";
    FilestorageDir= getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    return File.createTempFile(
            imageFileName,  /* prefix */".jpg",         /* suffix */
            storageDir      /* directory */
    );
}

IntenttakePictureIntent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
    // Create the File where the photo should goFilephotoFile=null;
    try {
        photoFile = createImageFile();
        takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
    } catch (IOException ex) {
        // Error occurred while creating the File
        Log.e(TAG, "Unable to create Image File", ex);
    }

    // Continue only if the File was successfully createdif (photoFile != null) {
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(photoFile));
    } else {
        takePictureIntent = null;
    }
}

But you still need WRITE_EXTERNAL_STORAGE permission before KITKAT:

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

Solution 5:

I used content:// with FileProvider.getUriForFile() as described here, and used external camera app by ACTION_IMAGE_CAPTURE intent, which is not supposed to require any permission.

But, it seems that if the app's manifest uses the android.permission.CAMERA permission, then opening an external camera app does require camera permission. See here and here.

Post a Comment for "Capture Image Without Permission With Android 6.0"