[英]Android: Scoped Storage Moving App Folder from External Directory to Android/data/com.myapp
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.
我所有的应用程序数据都存储在storage/emulated/0/MyAppName路径中的外部存储器中。 I have to move this data to private folder like Android/data/com.myapp .
我必须将此数据移动到像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)在这个SO 主题中,您有一些示例如何将文件移动到新目录(简而言之:使用
File
类中的renameTo
方法)
remember that when you update your targetSdkVersion
and "turn on" Scoped Storage then you won't get access to old folder.请记住,当您更新
targetSdkVersion
并“打开”范围存储时,您将无法访问旧文件夹。 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在第一个带有文件移动片段的发布版本中,让用户运行您的应用一段时间(大多数活跃用户会将文件移动到新文件夹),然后发布具有 Scoped Storage 支持的新应用版本
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).您必须考虑到部分用户可能会继续使用(非常)旧的应用程序,并且有一天会直接更新到 Scoped-Storage-supporting(
targetSdkVersion
上升),他们将丢失数据(文件访问权限在 prev 文件夹中)。 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
首先,让我们考虑您的应用程序 ID 是
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
.现在转到项目中的
res
目录,右键单击它并选择new
,然后选择Android Resource Directory
。
Now select resource type as xml
, write directory name as xml
and sourece set as main src/main/res
现在选择资源类型为
xml
,将目录名称写入xml
并将源设置为main src/main/res
Now create a new xml file inside the xml folder having name file_paths
现在在 xml 文件夹中创建一个名为
file_paths
的新 xml 文件
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.