简体   繁体   English

如何使用 MediaStore 指定特定的图库文件夹

[英]how to intent specific gallery folder by using MediaStore

I tried this code.我试过这个代码。

public static void openGallery(Context context) {
       String bucketId = "";
       final String[] projection = new String[] {"DISTINCT " + MediaStore.Images.Media.BUCKET_DISPLAY_NAME + ", " + MediaStore.Images.Media.BUCKET_ID};
       final Cursor cur = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
       while (cur != null && cur.moveToNext()) {
           final String bucketName = cur.getString((cur.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME)));
           if (bucketName.equals("Your_dir_name")) {
               bucketId = cur.getString((cur.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_ID)));
               break;
           }
       }
       Uri mediaUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

       if (bucketId.length() > 0) {
           mediaUri = mediaUri.buildUpon()
                   .authority("media")
                   .appendQueryParameter("bucketId", bucketId)
                   .build();
       }
       if(cur != null){
           cur.close();
       }
       Intent intent = new Intent(Intent.ACTION_VIEW, mediaUri);
       context.startActivity(intent);
   }

But I failed to open the gallery specific folder, but the gallery home screen appears with 'file not supported' Toast.但是我无法打开特定于画廊的文件夹,但画廊主屏幕显示“文件不受支持”Toast。 Is there any way I can solve this problem?有什么办法可以解决这个问题吗?

You can't set initial path to specific folder when working with files stored in MediaStore .处理存储在MediaStore文件时,您无法设置特定文件夹的初始路径。

However, you can use DocumentsContract.EXTRA_INITIAL_URI when working with DocumentFile , so you can set initial path when user wants to select files from Storage Access Framework (SAF):但是,您可以在使用DocumentsContract.EXTRA_INITIAL_URI时使用DocumentFile ,因此当用户想要从存储访问框架(SAF)中选择文件时,您可以设置初始路径:

Uri initialUri = DocumentFileCompat.createDocumentUri(DocumentFileCompat.PRIMARY, "DCIM")
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
        .putExtra(DocumentsContract.EXTRA_INITIAL_URI, initialUri)

DocumentFileCompat.createDocumentUri() helps you constructing the initial URI. DocumentFileCompat.createDocumentUri()帮助您构建初始 URI。 You can get DocumentFileCompat from SimpleStorage .您可以从SimpleStorage获取DocumentFileCompat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android-如何使用隐式意图在图库中打开特定文件夹? - Android - How to open a specific folder in a gallery using an implicit intent? 如何使用Intent打开指定文件夹中的库 - How to open gallery in specified folder using intent 如何在 android 工作室中使用 MediaStore Api 在 android 11 中获取特定文件夹,如 WhatsApp (.Status) 文件夹 - How to get specific folder like WhatsApp (.Status) folder in android 11 using MediaStore Api in android studio 如何使用 MediaStore 从具有路径、持续时间和缩略图的特定文件夹中获取所有视频? - How to get all video from specific folder with path,duration and thumbnail using MediaStore? 如何设置 MEDIASTORE 以从 android 上的特定文件夹获取图像 - how to set MEDIASTORE to get image from specific folder on android 如何使用 android 中的意图打开存储中的特定文件夹 - How to open specific folder in the storage using intent in android Qt和Android Gallery-使用QAndroidJniObject的MediaStore - Qt and Android Gallery - MediaStore using QAndroidJniObject 将意图图像保存到图库/文件夹 - Save intent image to gallery/folder 如何使用 MediaStore API 访问 Android 11 的 Whatsapp 文件夹? - How to access Whatsapp folder of Android 11 by using MediaStore API? 使用 MediaStore 访问文件夹中的最近图像 - Accessing recent images in a folder using MediaStore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM