MediaStore 可以访问设备主外部存储文件吗

问题描述

Android studio 自带的 android 虚拟设备只能有一个内存(我猜)。因此,当我尝试使用 MediaStore 访问设备上存在的图像时,我获得了所有图像。但是,当我尝试使用内部存储器和外部 SD 卡访问存储在真实设备上的图像时,我只能获得 SD 卡上存在的图像。我使用了 Android's official site 中的代码,但它实际上无法获取设备(而不是外部 SD 卡)上存在的图像。我看到了这个 stackoverflow post,有人称我正在尝试访问主要外部存储的内存。我怎样才能做到这一点?

我的代码如下

// root_extra was gotten from the starter of this activity. It can either be MediaStore.Images.Media.EXTERNAL_CONTENT_URI or MediaStore.Images.Media.INTERNAL_CONTENT_URI based on whichever storage location user wants to access by clicking either of two buttons available in the starter activity (Phone storage and SD card)
     String root_extra = getIntent().getStringExtra(STORAGE_ACCESS_ROOT);
            Uri storageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            if (root_extra != null) {
                storageUri = root_extra.equals(EXTERNAL) ? MediaStore.Images.Media.EXTERNAL_CONTENT_URI
                        : MediaStore.Images.Media.INTERNAL_CONTENT_URI;
            }
    
            String[] projection = {
                    MediaStore.Images.Media._ID,MediaStore.Images.Media.BUCKET_disPLAY_NAME,MediaStore.Images.Media.SIZE,MediaStore.Images.Media.disPLAY_NAME};
            Cursor imgCursor = getApplicationContext()
                    .getContentResolver()
                    .query(storageUri,projection,null,null);
    
            int bucketId = imgCursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
            int imgSize = imgCursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE);
            int name = imgCursor.getColumnIndexOrThrow(MediaStore.Images.Media.disPLAY_NAME);
            int bucketName
                    = imgCursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_disPLAY_NAME);
    
            List<String> dirName = new ArrayList<>();
            while (imgCursor.movetoNext()) {
                long id = imgCursor.getLong(bucketId);
                int size = imgCursor.getInt(imgSize);
                String fileName = imgCursor.getString(name);
                String folderName = imgCursor.getString(bucketName);
    
                Uri contentUri
                        = ContentUris.withAppendedId(storageUri,id);
                Image currentimage = new Image(contentUri,size,fileName,folderName);
    
                int directoryIndex = linearSearch(dirName,folderName);
                // if search result (directoryIndex) passes this test,then it means that there is
                // no such directory in list of directory names
                if (directoryIndex < 0) {
                    imageDirectoryList.add(new ArrayList<>());
                    dirName.add(folderName);
                    directoryIndex = linearSearch(dirName,folderName);
                    if (directoryIndex >= 0)
                        imageDirectoryList.get(directoryIndex).add(currentimage);
                } else {
                    imageDirectoryList.get(directoryIndex).add(currentimage);
                }
            }
    
            imgCursor.close();
            runOnUiThread(() -> {
                imageAdapter.notifyDataSetChanged();
                doViewUpdate();
            });

我使用 MediaStore.Images.Media.INTERNAL_CONTENT_URI 访问主外部存储(或我称之为内部存储器),但它不起作用。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...