How to write files to external public storage in Android so that they are visible from Windows?
What is wrong with my implementation?
MediaStore
has not discovered your newly-created files yet. What you see in Windows — and in many on-device "gallery" apps — is based on what MediaStore
has indexed.
Use MediaScannerConnection
and its scanFile()
method to tell MediaStore
about your file, once you have written out your data to disk:
public void scanFile(Context ctxt, File f, String mimeType) {
MediaScannerConnection
.scanFile(ctxt, new String[] {f.getAbsolutePath()},
new String[] {mimeType}, null);
}
or, in Kotlin:
fun scanFile(ctxt: Context, f: File, mimeType: String) {
MediaScannerConnection.scanFile(ctxt, arrayOf(f.getAbsolutePath()), arrayOf(mimeType), null)
}
How to make visible the images in the gallery after insertion in the internal storage?
Give this a try:
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);
}
}
}
You can check the documentation here.
How to write files to external public storage in Android so that they are visible from Windows?
What is wrong with my implementation?
MediaStore
has not discovered your newly-created files yet. What you see in Windows — and in many on-device "gallery" apps — is based on what MediaStore
has indexed.
Use MediaScannerConnection
and its scanFile()
method to tell MediaStore
about your file, once you have written out your data to disk:
public void scanFile(Context ctxt, File f, String mimeType) {
MediaScannerConnection
.scanFile(ctxt, new String[] {f.getAbsolutePath()},
new String[] {mimeType}, null);
}
or, in Kotlin:
fun scanFile(ctxt: Context, f: File, mimeType: String) {
MediaScannerConnection.scanFile(ctxt, arrayOf(f.getAbsolutePath()), arrayOf(mimeType), null)
}
Related Topics
What's the Best Way to Share Data Between Activities
How to Write to a Folder on an Sd Card in Android
Rjava Load Error in Rstudio/R After "Upgrading" to Osx Yosemite
Why Does My Arraylist Contain N Copies of the Last Item Added to the List
What's Wrong With Java Date & Time API
Cannot Refer to a Non-Final Variable Inside an Inner Class Defined in a Different Method
Difference Between Matches() and Find() in Java Regex
What Is a Classpath and How to Set It
Remove Top-Level Container on Runtime
How to Call a Soap Web Service on Android
Why Doesn't "System.Out.Println" Work in Android
Set Imageview Width and Height Programmatically
Difference Between == and Equals() in Java
Java Multiple File Transfer Over Socket