在 KitKat 之前(或在新画廊之前)Intent.ACTION_GET_CONTENT返回一个这样的 URI
Intent.ACTION_GET_CONTENT
内容://媒体/外部/图像/媒体/3951。
使用ContentResolver和查询 MediaStore.Images.Media.DATA返回的文件 URL。
ContentResolver
MediaStore.Images.Media.DATA
然而,在 KitKat 中,Gallery 会返回一个 URI(通过“Last”),如下所示:
内容://com.android.providers.media.documents/document/image:3951
我该如何处理?
尝试这个:
if (Build.VERSION.SDK_INT <19){ Intent intent = new Intent(); intent.setType("image/jpeg"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, getResources().getString(R.string.select_picture)),GALLERY_INTENT_CALLED); } else { Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("image/jpeg"); startActivityForResult(intent, GALLERY_KITKAT_INTENT_CALLED); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode != Activity.RESULT_OK) return; if (null == data) return; Uri originalUri = null; if (requestCode == GALLERY_INTENT_CALLED) { originalUri = data.getData(); } else if (requestCode == GALLERY_KITKAT_INTENT_CALLED) { originalUri = data.getData(); final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); // Check for the freshest data. getContentResolver().takePersistableUriPermission(originalUri, takeFlags); } loadSomeStreamAsynkTask(originalUri); }
大概需要
@SuppressLint(“NewApi”)
为了
获取PersistableUriPermission