Home >Java >javaTutorial >How to Make Android Files Visible in Windows File Explorer?
Your app requires saving files in a location accessible from your computer's file explorer when connecting an Android device. However, your current implementation using Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) results in the files being invisible in Windows File Explorer.
The issue here lies in MediaStore. The files you create aren't initially detected by it. Windows File Explorer and many on-device gallery apps display their content based on MediaStore's index.
To resolve this, you need to inform MediaStore about your newly created files using the MediaScannerConnection class and its scanFile() method. Here's how you can do it:
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) }
By using MediaScannerConnection and scanFile(), you can inform MediaStore about the saved files, and they will become visible in Windows File Explorer and other apps that rely on MediaStore data.
The above is the detailed content of How to Make Android Files Visible in Windows File Explorer?. For more information, please follow other related articles on the PHP Chinese website!