【问题标题】:How to make visible the images in the gallery after insertion in the internal storage?插入内部存储后如何使图库中的图像可见?
【发布时间】:2026-01-01 07:50:01
【问题描述】:

我刚刚在我的内部存储中插入了一些图片,但它们在画廊中不可见。

谁能解释一下为什么?

这是我的代码:


    File photosFolder = new File(getContext().getExternalFilesDir(null),"myPictures");
            photosFolder.mkdirs();
            String[] pictures = null;
            try {
                pictures = assetManager.list("photos");
            } catch (IOException e) {
                Log.e("tag", "Failed to get asset file list.", e);
            }
            for(String filename : pictures) {
                InputStream in = null;
                OutputStream out = null;
                try {
                    in = assetManager.open("photos/"+filename);
                    File outFile = new File(photosFolder, filename);
                    out = new FileOutputStream(outFile);
                    copyFile(in, out);
                    in.close();
                    in = null;
                    out.flush();
                    out.close();
                    out = null;
                    MediaScannerConnection.scanFile(getContext(), new String[]{outFile.toString()}, null, new MediaScannerConnection.OnScanCompletedListener() {
                        @Override
                        public void onScanCompleted(String path, Uri uri) {
                            Log.i("External Storage", "Scanned"+ path +":");
                            Log.i("External Storage", "uri "+uri);
                        }
                    });
                } catch(IOException e) {
                    Log.e("tag", "Failed to copy asset file: " + filename, e);
                }
            }

我对此没有任何结果,有什么帮助吗?

【问题讨论】:

  • 在 Android 10+ 上,其他应用无权访问您选择的位置中的文件。在早期版本的 Android 上,最终MediaStore 会将它们编入索引,尽管您可以加快进程by using MediaScannerConnection
  • @CommonsWare 我还是个初学者,我不太明白如何用 MediaScanner 实现它:(
  • 嗯,好吧,the linked-to question and answer 有 Java 和 Kotlin 的示例代码。
  • 它们在您的目录中可见吗?
  • @Danish 是的

标签: android assets internal-storage


【解决方案1】:

试试这个:

      File photosFolder = null;
       if(Build.VERSION_CODES.R>Build.VERSION.SDK_INT) {
      photosFolder = new File(Environment.getExternalStorageDirectory(), "myPictures");//this will create a seperate directory in your external storage if you are using android 10 device
                        }
                        else
                        {
                            photosFolder=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath(),"myPictures");//though depreciated it still works.In android 11 you can create your directory inside public directories using this method. 
                        }
                            if (!file.exists()) {
                                file.mkdir();
                            }

    String[] pictures = null;

    try {
        pictures = assetManager.list("photos");
    } catch (IOException e) {
        Log.e("tag", "Failed to get asset file list.", e);
    }
    for(String filename : pictures) {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open("photos/"+filename);
            File outFile = new File(photosFolder, filename);
            out = new FileOutputStream(outFile);
            copyFile(in, out);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
            MediaScannerConnection.scanFile(this,
            new String[] { outFile.toString() }, null,
            new MediaScannerConnection.OnScanCompletedListener() {
        public void onScanCompleted(String path, Uri uri) {
            Log.i("ExternalStorage", "Scanned " + path + ":");
            Log.i("ExternalStorage", "-> uri=" + uri);
        } catch(IOException e) {
            Log.e("tag", "Failed to copy asset file: " + filename, e);
        }
    }
     
        }

您可以在这里查看文档:https://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory%28java.lang.String%29

【讨论】:

  • 我试过这个,它在日志中返回:I/External Storage: Scanned/storage/emulated/0/Android/data/com.data.myApp/files/myPictures/dataset12soleil。 jpg: I/External Storage: uri null 我应该需要“权限:读取外部存储”还是只需要“写入外部存储”就足够了?
  • 你必须同时添加权限。
  • 我两个都有 :( @Danish
  • 实际上从 android 10 开始,您将不会在您的图库中看到那些存在于 Android/data/yourpackagename 中的图片。
  • 终于成功了!我不得不将我的“myPictures”文件夹重命名为“Pictures”。然后我遇到了权限问题,这篇文章帮助我解决了这个问题:https://*.com/questions/8854359/exception-open-failed-eacces-permission-denied-on-android