![](/img/trans.png)
[英]How to show images from assets or internal storage in a gallery to pick from?
[英]How to make visible the images in the gallery after insertion in the internal storage?
我只是在我的內部存儲中插入了一些圖片,但那時它們在圖庫中不可見。
有人可以解釋我為什么嗎?
這是我的代碼:
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;
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
}
}```
試試這個:
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(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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.