Reputation: 49
How can I solve this error on android 9.0. When I run on android 7 and below there is no error
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.PICK dat=content://media/external/audio/media }
Here is the method
private void pickAudio() {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, PICK_AUDIO_REQUEST);
}
Upvotes: 0
Views: 3525
Reputation: 2441
For anyone facing this issue on Android API level 30 or above, one of the possible reasons could be Android Package Visibility changes.
You need to add
<queries>
<intent>
<action android:name="android.intent.action.PICK" />
<data android:mimeType="*/*" />
</intent>
</queries>
to your Manifest.xml
file
If the issue persists, it means there is no app to handle that request. You will have to go with
try-catch
then.
Upvotes: 1
Reputation: 715
You can use a try catch to catch the exception and display a Alert Dialog? **Note: i changed your method name
private void pickAudioIntent() {
try{
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, PICK_AUDIO_REQUEST);
}catch(Exception e){
//No activity? Display alert Dialog to user
}
You can also check before hand if any activity can handle the intent by doing the below
public static boolean isIntentAvailable(Intent intent) {
final PackageManager mgr = mContext.getPackageManager();
List<ResolveInfo> list =
mgr.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
That would change the pickAudioIntent() to :
private void pickAudioIntent() {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
if(isIntentAvailable(i)){
startActivityForResult(i, PICK_AUDIO_REQUEST);
return;
}
//Show Dialog to user as no intent available
}
Upvotes: 2