插入内部存储后如何使图库中的图像可见?
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;
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);
}
}
我没有得到任何结果,有什么帮助吗?
试一试:
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);
}
}
}
您可以查看文档here。
我刚刚在我的内部存储中插入了一些图片,但它们在画廊中不可见。
谁能解释一下为什么?
这是我的代码:
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);
}
}
我没有得到任何结果,有什么帮助吗?
试一试:
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);
}
}
}
您可以查看文档here。