I am planning to implement scoped storage for my existing app. All my app data is stored in External Memory in storage/emulated/0/MyAppName path. I have to move this data to private folder like Android/data/com.myapp. Can anyone provide some code snippet to help in this regard?
-
You mean `/storage/emulated/0/Android/data/com.myapp` !? – blackapps Nov 10 '20 at 10:39
2 Answers
in this SO topic you have some examples how to move files to new directory (in short: use renameTo
method from File
class)
remember that when you update your targetSdkVersion
and "turn on" Scoped Storage then you won't get access to old folder. at first release version with file-moving snippet, let users run your app for a while (most of active users will move files to new folder), then release new app version with Scoped Storage support
you have to take into account that some part of users may stay with (very) old app and some day will update straight to Scoped-Storage-supporting (targetSdkVersion
bumped up), they will lose their data (file access in prev folder). longer you will keep version pre-Scoped-Storage with moving files code on market - smaller part of users will lose data

- 17,866
- 3
- 32
- 74
You can also use App-specific files.
first of all, let's consider your application id is com.myapp
now add the following to your manifiest file inside application attribute
<application...>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
Now go to
res
directory in your project, right click on it and selectnew
, then selectAndroid Resource Directory
.Now select resource type as
xml
, write directory name asxml
and sourece set asmain src/main/res
Now create a new xml file inside the xml folder having name
file_paths
And write following inside it.
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="my_images" path="Android/data/com.myapp/files/Pictures" /> <external-path name="my_documents" path="Android/data/com.myapp/files/Documents" /> <external-path name="my_videos" path="Android/data/com.myapp/files/Movies" /> </paths>
Now to use it you can use the following code
private File createImageFile(String fileName) throws IOException {
File storageDir = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File imageFile = File.createTempFile(fileName, ".jpg", storageDir);
return imageFile;
}
public Uri getFileUri(String fileName, String imageString) {
File imageFile = null;
Uri uri = null;
try {
imageFile = createImageFile(fileName);
imageFile.createNewFile();
FileOutputStream fo = new FileOutputStream(imageFile.getPath());
fo.write(imageString.getBytes());
fo.flush();
fo.close();
} catch (Exception e) {
e.printStackTrace();
}
if (imageFile != null) {
uri = FileProvider.getUriForFile(activity, "com.myapp", imageFile);
}
return uri;
}

- 89
- 12
-
Nice answer. But sorry you posted it at the wrong question. Find your post and move your answer to it. – blackapps Nov 10 '20 at 10:26