Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Camera intent not responding in Android 11

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.

like image 298
developer Avatar asked Oct 16 '20 16:10

developer


People also ask

How do I open the camera on Android 11?

To launch the camera, swipe the camera icon upwards.

Which intent action do you use to take a picture with a camera app?

The Android Camera application encodes the photo in the return Intent delivered to onActivityResult() as a small Bitmap in the extras, under the key "data" . The following code retrieves this image and displays it in an ImageView .


1 Answers

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.

like image 59
CoolMind Avatar answered Sep 22 '22 19:09

CoolMind