简体   繁体   中英

Is it possible to capture image from Camera and pick from Gallery without WRITE_EXTERNAL_STORAGE or READ_EXTERNAL_STORAGE permission in Android v6.0?

I am able to get image from camera without any permissions in API 22 by using /data/Android// as storage path (since Android allows this directory to be writable without explicitly asking for permission in API 19+).

Unfortunately, the same is not working in API 23. In fact, camera is returning responseCode as 0 ( RESULT_CANCELLED )

What am I doing wrong?

Edit: This version of code if working up to API 22

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File image_file = null;
try {
    image_file = File.createTempFile("image_file", ".jpg", getFilesDir());
    image_file.setWritable(true, false);
} catch (IOException e) {
    e.printStackTrace();
}
image_file_uri = Uri.fromFile(image_file);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, image_file_uri);
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent.setType("image/*");
Intent chooserIntent = Intent.createChooser(galleryIntent, getString(R.string.image_intent_chooser_title));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{cameraIntent});
startActivityForResult(chooserIntent, CODE_IMAGE_CAPTURE);

Is there any requirement that your application be used to capture the image? If not have a look at this link to use an existing camera application:

Using Existing Camera Apps

private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Uri fileUri;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

When the startActivityForResult() method is executed, users see a camera application interface. After the user finishes taking a picture (or cancels the operation), the user interface returns to your application, and you must intercept the onActivityResult() method to receive the result of the intent and continue your application execution. For information on how to receive the completed intent, see Receiving camera intent result .

Yes it's possible using the camera intent as answered by Cory Charlton. This Gist should be very useful for you. It provide a helper class with two methods: selectImageFromGallery() and takePhotoWithCamera(). The first one allow picking picture from Gallery or device folders, and the second one to take a picture using the default camera. You can also send the picture to a crop intent to edit it and apply filters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM