1

Requirements of app:

  • Saved files visible in stock device's file browser. (for user manipulation)
  • Files are deleted when app is uninstalled.

I came across this post (How can I let users access the internal storage directory of my app?) where the OP seemed to be wanting the same behavior I do and seemed to have it solved by using getExternalFilesDir() [last post] but when I use that method my files are not visible to the user outside of my app. So how do I get both functionalities in my app?

externalDir = mContext.getExternalFilesDir(null);
File myDir = new File(externalDir.toString() + "/my_snapshots");
myDir.mkdirs()
String fname = "Image-" + n + ".jpg";
            File file = new File(myDir, fname);
FileOutputStream out = new FileOutputStream(file);

MediaScannerConnection.scanFile(mContext, 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);
                }
            });
Community
  • 1
  • 1
shoota
  • 111
  • 11

1 Answers1

1

seemed to have it solved by using getExternalFilesDir()

That is the correct solution.

when I use that method my files are not visible to the user outside of my app

That is because you did not index the files using MediaScannerConnection and its scanFile() method. Once you do that, MTP clients and any on-device apps that use MediaStore will see your files.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Aw crap I forgot to say that I have used scanFile(). And I know scanFile() works because if I save the file to internal public the image shows up in the gallery. – shoota Dec 17 '15 at 18:28
  • Added the pertinent psuedo-code to see if you can spot something that may be off. – shoota Dec 17 '15 at 18:33
  • @shoota: I have had no problems with `scanFile()` in conjunction with `getExternalFilesDir()`, though it may require you to do a "refresh" on the client side, or unplug/re-plug-in the device, to get it to pick up the change. The main difference in my code versus yours is that I use `getAbsolutePath()` instead of `toString()`, though in this case I'm not sure that there's a difference. The files will show up in `Android/data/your.application.id.here/files/` on external storage (which, due to Googly stupidity, is typically called "Internal Storage" in a desktop file browser). – CommonsWare Dec 17 '15 at 18:44
  • I can see my file using AFT under `Android/data/com.my_company/my_snapshots` so we know it's there but why is it not showing under Samsung's My Files app? – shoota Dec 17 '15 at 19:02
  • @shoota: Since I have no idea what Samsung's My Files app looks at, I can't answer that. Bear in mind that many devices don't have a "stock device's file browser", and that such apps' behavior can vary widely. – CommonsWare Dec 17 '15 at 19:07