shoota
shoota

Reputation: 111

How to save a file that is accessible to the user outside of the app and also deletes on app uninstall?

Requirements of app:

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);
                }
            });

Upvotes: 1

Views: 174

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007658

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.

Upvotes: 1

Related Questions