My development phone is a Nexus 5
, running Android 4.4.2
.
In my application, I am attempting to create a folder on external storage that wi
In terms of this being an indexing issue with the Nexus, this worked for me:
MediaScannerConnection.scanFile(this, new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
You should call it straight after creating and saving the file. By using the scanner, I was able to see newly created files and directories simply by replugging the device in.
According to the docs:
MediaScannerConnection provides a way for applications to pass a newly created or downloaded media file to the media scanner service. The media scanner service will read metadata from the file and add the file to the media content provider.
Hope this helps someone else.
Typically, I do this:
/**
* Return a dedicated directory named "MyApp" at the top of external storage
*/
public static file getDataDir() {
File sdcard = Environment.getExternalStorageDirectory();
if( sdcard == null || !sdcard.isDirectory() ) {
// TODO: warning popup
Log.w(TAG, "Storage card not found " + sdcard);
return null;
}
File dataDir = new File(sdcard, "MyApp");
if( !confirmDir(dataDir) ) {
// TODO: warning popup
Log.e(TAG, "Unable to create " + dataDir);
return null;
}
return dataDir;
}
private static boolean confirmDir(File dir) {
if (dir.isDirectory()) return true; // already exists
if (dir.exists()) return false; // already exists, but is not a directory
return dir.mkdirs(); // create it
}
Also, add this permission to your manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
If you want private storage (typically under /data/) for your app (not on sdcard), then look into getDir()
, getFilesDir()
, fileList()
, openFileInput()
, openFileOutput()
, etc.
There are also helper functions to get private directories within the sdcard for API 8 and above: getExternalFilesDir()
, getExternalCacheDir()
, etc.
Turns out the issue was due to, I think, indexing.
Basically all of the other devices I tested on, allowed me to reconnect the USB connection and view the created files and folders.
For some reason my Nexus 5 doesn't index the folders/files, even though they exist.
I downloaded a different 3rd party file explorer application and noticed all the folders and files were there.
So to view these folders and files via USB debugging, I have to restart the phone in order to re-index them, which seems quite annoying but it is better than it not working at all.
If you are running an Android device with api >= 23, you need to request permission from user before you call mkdir() function. My previous answer with example code can be found https://stackoverflow.com/a/38694026/5089713
For android sdk version 23 and above you should check if the user has granted permission of external storage.
private void createFolder() {
if (isStoragePermissionGranted()) {
File folder = new File(Environment.getExternalStorageDirectory()+ File.separator + "DebugData");
if(!folder.exists()){
folder.mkdir();
}
}
public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
return true;
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
return true;
}
}
The code above worked for me and I hope it will work for you.
Had the same problem. Turned out I was testing on Android 6 and did not check for runtime WRITE_EXTERNAL_STORAGE permission.