长风秋雁
我会建议Intent.ACTION_PICK private void selectFileFromGallery() { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent.setType("image/*"); startActivityForResult(Intent.createChooser(intent, "Select Picture"), requestGallery); }它将打开一个这样的对话框,其中包含所有可用作图像选择器的用户应用程序:然后当用户选择图像时: @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { if (requestCode == requestGallery) { onSelectFromGalleryResult(data); } } @SuppressWarnings("deprecation") private void onSelectFromGalleryResult(Intent data) { Uri uri = data.getData(); if (!uri.toString().contains("file")) { if (uri.toString().contains("external")) {//chosen from external storage photoUri = Uri.parse("file://" + getRealPathFromURI(uri)); } else { photoUri = Uri.parse("file://" + (Build.VERSION.SDK_INT <= 18 ? getRealPathFromURI_API11to18(getActivity(), uri) : getRealPathFromURI_API19(getActivity(), uri))); } } @SuppressLint("NewApi") private String getRealPathFromURI_API19(Context context, Uri uri) { return RealPathUtil.INSTANCE.getRealPathFromURI_API19(context, uri); } public String getRealPathFromURI(Uri contentUri) { String[] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = getActivity().managedQuery(contentUri, proj, null, null, null); int column_index = cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } @SuppressLint("NewApi") private String getRealPathFromURI_API11to18(Context context, Uri contentUri) { String[] proj = { MediaStore.Images.Media.DATA }; String result = null; CursorLoader cursorLoader = new CursorLoader( context, contentUri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if (cursor != null) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; }