0

I am making an Android app. I updated the target version from 28 to 33. I need to use the picture selector. I made it such that if the Build.VERSION.SDK_INT is higher than S_V2, then action MediaStore.ACTION_PICK_IMAGES is used. Otherwise Intent.ACTION_PICK used. I extracted it into a tool. However, when I use it elsewhere in the project, sometimes it works, but sometimes I get an exception. The exception that I get:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.provider.action.PICK_IMAGES cat=\[android.intent.category.DEFAULT\] dat=content://media/... typ=iamge/\* }

I don't understand why this exception is thrown. My code:

Intent intent = new Intent();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.S_V2){
intent.setAction(MediaStore.ACTION_PICK_IMAGES);
} else {
intent.setAction(Intent.ACTION_PICK);
}
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
activity.startActivityForResult(intent, requestCode);

What should I do to solve the exception? Any help is appreciated.

Dinux
  • 644
  • 1
  • 6
  • 19
QiaoHW
  • 1
  • 1
  • 1
    Your error says you spelled `image` wrong - it says you spelled it `iamge`. Do you still get the same error when you spell the mime type correctly? – ianhanniballake May 22 '23 at 04:53
  • I checked code ,You are right ,I did make a spelling mistake in one place and missed it – QiaoHW May 22 '23 at 05:45

1 Answers1

0

Put your code into a try-catch block like below,

 try {
       Intent intent = new Intent();
       if (Build.VERSION.SDK_INT > Build.VERSION_CODES.S_V2) {
           intent.setAction(MediaStore.ACTION_PICK_IMAGES);
       } else {
           intent.setAction(Intent.ACTION_PICK);
       }
       intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
       activity.startActivityForResult(intent, requestCode);
   } catch (ActivityNotFoundException ex) {
       e.printStackTrace();
   }

If this not work try to use Intent code as a below,

Intent intent = new Intent(this, class);
inkredusk
  • 919
  • 4
  • 16