14

I am working with Camera Intent. Everything is working fine till Android 10, but in Android 11 I am getting result Code 0.

  1. Manifest Permission

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
  2. Intent function with file creation :

     private void openCameraApp()
     {
         Intent picIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE).
                 addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
         try {
    
             String file_path = Environment.getExternalStorageDirectory().toString() +
                     "/" + mContext.getResources().getString(R.string.app_name);
    
             File dir = new File(file_path);
             if (!dir.exists())
                 dir.mkdirs();
    
             imagePath = new File(dir, mContext.getResources().getString(R.string.app_name) + System.currentTimeMillis() + ".png");
    
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                 picIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID, imagePath));
                 setUri(FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID, imagePath));
             } else {
                 picIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imagePath));
                 setUri(Uri.fromFile(imagePath));
             }
    
             ((Activity) mContext).startActivityForResult(picIntent, CAMERA_REQUEST);
    
         } catch (Exception e) {
             logger.e(e);
         }
     }
    

I have added android:requestLegacyExternalStorage="true" in application tag of manifest file.

Ruli
  • 2,592
  • 12
  • 30
  • 40
developer
  • 423
  • 1
  • 4
  • 12

3 Answers3

35

See intent.resolveActivity returns null in API 30. Maybe there is something wrong with AndroidManifest.

<queries>
    <intent>
        <action android:name="android.media.action.IMAGE_CAPTURE" />
    </intent>
</queries>

Or see Cannot take a photo programmatically on Android 11 - intent returns canceled status.

CoolMind
  • 26,736
  • 15
  • 188
  • 224
  • 1
    Thanks for the help and sorry for being too late. I have already tried this but this is showing error while I am using in manifest, even I have used higher sdk level in my gradle. – developer Oct 21 '20 at 07:38
  • @developer, sorry, I didn't understand, what error has it shown? Maybe a warning? And after that it didn't compile or didn't work? – CoolMind Oct 21 '20 at 08:20
  • See the error : Element queries is not allowed here Inspection info: This inspection highlights unallowed XML tags in Android resource files and AndroidManifest.xml – developer Oct 21 '20 at 10:39
  • @developer, it's just a warning. The `` tag even exists in https://developer.android.com/training/basics/intents/package-visibility. You can suppress warnings. – CoolMind Oct 21 '20 at 15:35
  • Thank you. I wonder why Android does not make a public announcement on such changes. – ekashking Oct 25 '21 at 17:02
  • @ekashking, thanks! Agree with you, Android 11 brought many security changes, especially in storage handling, and often we knew about them, when something was broken. – CoolMind Oct 25 '21 at 19:23
  • I have added the same code not working.https://stackoverflow.com/questions/71953359/camera-intent-not-working-proper-in-android-11-here-is-below-my-code-i-receive – Siddharth kheni Apr 21 '22 at 12:24
  • @Siddharthkheni, but in your code you don't have ``. – CoolMind Apr 21 '22 at 13:15
  • @CoolMind I have added. didn't work it. – Siddharth kheni Apr 27 '22 at 05:07
  • Those who new to this, don't forgot to add this queries outside the application tag in manifest. – Harin Kaklotar Oct 14 '22 at 07:17
1

Remove this .putExtra(MediaStore.EXTRA_OUTPUT

akkmastr
  • 186
  • 6
0

The result code in activity.java indicates the camera activity has been cancelled:

/** Standard activity result: operation canceled. */
public static final int RESULT_CANCELED    = 0;
/** Standard activity result: operation succeeded. */
public static final int RESULT_OK           = -1;
/** Start of user-defined activity results. */
public static final int RESULT_FIRST_USER   = 1;

Try launching the activity for IMAGE_CAPTURE: startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)

ahandyapp
  • 51
  • 2
  • 4