图像内容提供商是否有“库存”选择器?

例如,当我想在股票消息应用程序中将图像附加到文本消息时,我会看到一个熟悉的系统对话框,其中显示了相机、图库和其他图像内容提供程序。

我想在我自己的应用程序中使用它。我看到很多库允许用户在画廊和相机之间进行选择,但我希望所有用户安装的图像源都出现。

Messages(以及其他常用应用程序,例如 Mail)中的系统对话框真的是为这些应用程序定制的吗?我们真的需要自己建造吗?存储访问框架似乎不是正确的解决方案,因为它绕过了相机(或我没有想到但可能存在于用户设备上的其他图像源)。


慕村9548890
浏览 109回答 2
2回答

长风秋雁

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

开满天机

您要求的事情可以使用系统访问框架工作来完成。在清单文件中添加存储权限,然后检查权限是否被接受。然后您可以打开打开的文档选择让用户选择图像。代码在一些onClick里面Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT);i.addCategory(Intent.CATEGORY_OPENABLE);i.setType("image/*");startActivityForResult(i, 41);这将只为图像文件打开文件选择器活动。然后你可以在里面取回结果:&nbsp;@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {&nbsp; &nbsp; super.onActivityResult(requestCode, resultCode, data);&nbsp; &nbsp; if (resultCode == Activity.RESULT_OK){&nbsp; &nbsp; &nbsp; &nbsp; if (requestCode == 41) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (data != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Uri uri = data.getData();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //This is the uri to the file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //To get the file use the uri&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}您仍然必须自己创建 BottomSheetDialog,但您的工作将大大减少,因为只有 2 个选项:1. 相机应用程序,2。文件选择器。您必须自己处理相机事件并获取图像 uri。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java