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?
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
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 select new
, then select Android Resource Directory
.
Now select resource type as xml
, write directory name as xml
and sourece set as main 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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.