2017-08-16 116 views
0

在我的應用中,我讓用戶從他們的畫廊中選擇一張照片。 我用一個意圖是這樣的:沒有找到處理意圖的活動{act = android.intent.action.PICK dat = content:// media/external/images/media}

Intent pickPictureIntent = new Intent(Intent.ACTION_PICK, 
       MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

而且開始之前,我這個意圖我檢查是否有任何應用程序,可以處理:

if (pickPictureIntent.resolveActivity(getActivity().getPackageManager()) != null) { 
    startActivityForResult(pickPictureIntent, SELECT_PICTURE_FROM_GALLERY_REQUEST_CODE); 
} 

但我的兩個用戶得到這個例外,當他們試圖從他們的圖庫中選擇一張照片:

Exception android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.PICK dat=content://media/external/images/media } 

據我知道什麼時候有沒有活動,以處理這個意圖出現這種情況,但是當你看到我檢查沒有活動的可能性在我的代碼中處理意圖。

回答

1

試試這個:

Intent intent = new Intent(); 
intent.setType("image/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE); 

這帶來了文件的應用程序。允許用戶也可以使用他們可能已經安裝的任何圖庫應用程序:

Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT); 
getIntent.setType("image/*"); 


Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
pickIntent.setType("image/*"); 

Intent chooserIntent = Intent.createChooser(getIntent, "Select Image"); 
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent}); 

startActivityForResult(chooserIntent, PICK_IMAGE); 
+0

您確定ActivityNotFoundException完全不會發生在這段代碼中嗎? – Mostafa

+0

此代碼不應該有ActivityNotFoundException –

相關問題