Capture image without permission with Android 6.0

前端 未结 5 1179
庸人自扰
庸人自扰 2021-02-04 07:29

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

5条回答
  •  Happy的楠姐
    2021-02-04 08:29

    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:

    File tempFile = 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:

    Intent intent = new Intent(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.

提交回复
热议问题