首页 > 解决方案 > Android将本地应用程序数据库文件复制到内部设备内存文件夹

问题描述

我想在单击“备份”按钮后实现这一点,我的应用程序数据库文件夹中的 mydb.db 文件将被复制到设备内部存储器,应在其中创建一个新文件夹并将文件放入其中。

我尝试过的代码:

   backupbtn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {

        try {
            File data = Environment.getDataDirectory();
            File extdata = Environment.getExternalStorageDirectory();

            String backupDBPath = "/Newfolder/mydb.db";
            File ddir = new File(Environment.getExternalStorageDirectory()
                    + "/Newfolder/");
            DirectoryExist(ddir);

            String currentDBPath ="/data/mypackage/databases/mydb.db";

            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(extdata, backupDBPath);

            if (currentDB.exists()) {
                FileChannel src = new FileInputStream(currentDB).getChannel();
                FileChannel dst = new FileOutputStream(backupDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
                Toast.makeText(getApplicationContext(), "Backup is successful ", Toast.LENGTH_SHORT).show();
            }

        } catch (IOException  e) {
            e.printStackTrace();
        }

    }

});

private void DirectoryExist(File destination) {

    if (!destination.isDirectory()) {
        if (destination.mkdirs()) {
            Log.d("Carpeta creada", "....");
        } else {
            Log.d("Carpeta no creada", "....");
        }
    }
}

但是我有这个错误:java.io.FileNotFoundException: /storage/emulated/0/Newfolder/mydb.db (No such file or directory)

更新:经过调查我发现,这是一个存储权限问题。

如果我在应用程序设置中手动设置权限,它就可以工作。但是当我尝试在应用程序中设置权限时,它仍然说,权限被拒绝:

 if(!checkPermissionForReadExtertalStorage())
            {
                try {
                    requestPermissionForReadExtertalStorage();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

public boolean checkPermissionForReadExtertalStorage() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        int result = this.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
        return result == PackageManager.PERMISSION_GRANTED;
    }
    return false;
}

public void requestPermissionForReadExtertalStorage() throws Exception {
    try {
        ActivityCompat.requestPermissions((Activity) this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                READ_STORAGE_PERMISSION_REQUEST_CODE);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
}

带有权限的弹出窗口出现了,但似乎仍然不允许权限。

在清单中我有:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

但是 WRITE_EXTERNAL_STORAGE 不再用于 android 10+,所以不确定我是否需要保留它。我也有 <application android:requestLegacyExternalStorage="true"

但正如我所读到的:从 Android 11 开始,使用范围存储模型的应用程序只能访问自己的应用程序特定缓存文件。

因此,除了 app 文件夹之外,似乎没有其他选项可以在本地存储中创建任何文件夹来备份文件。一种选择似乎是通过 MediaStore API,但使用它来备份一个小 db 文件似乎是一种矫枉过正。

标签: androidbackup

解决方案


推荐阅读