【发布时间】: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 usingMediaScannerConnection。 -
@CommonsWare 我还是个初学者,我不太明白如何用 MediaScanner 实现它:(
-
嗯,好吧,the linked-to question and answer 有 Java 和 Kotlin 的示例代码。
-
它们在您的目录中可见吗?
-
@Danish 是的
标签: android assets internal-storage