简体   繁体   English

Android:范围存储将应用程序文件夹从外部目录移动到 Android/data/com.myapp

[英]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>
  1. 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

  2. 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

  3. Now create a new xml file inside the xml folder having name file_paths现在在 xml 文件夹中创建一个名为file_paths的新 xml 文件

  4. 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.

相关问题 android.content.ActivityNotFoundException:无法找到显式活动 class {com.myapp/androidx.fragment.app.FragmentActivity} - android.content.ActivityNotFoundException: Unable to find explicit activity class {com.myapp/androidx.fragment.app.FragmentActivity} Android 12 范围存储迁移:将数据从公共 sdcard-directory 交换到 App-internal-directory,反之亦然 - Android 12 Scoped Storage Migration: Exchange Data from public sdcard-directory to App-internal-directory and vice versa android ::如何在外部存储中的app数据目录上设置app图标, - android:: how to set app icon on app data directory in external storage , 如何在android外部存储目录中创建一个文件夹? - how to create a folder in android External Storage Directory? Android 在应用程序中打开外部存储文件夹 - Android Open External Storage Folder in app 无法将文件从外部存储复制到Android 9中的数据目录 - Unable to copy file from external storage to data directory in Android 9 Android | 在 Android 11+ 上的外部存储根目录中创建文件夹 - Android | Creating Folder in External Storage Root Directory on Android 11+ Android Q 范围存储和外部库 - Android Q Scoped Storage and external libraries Android 范围存储 - 如何自定义目录选择器 - Android scoped storage - how to customize directory picker Android-在外部存储上保护应用程序数据的安全 - Android - secure app data on external storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM