【问题标题】:How can i copy app data to sdcard in android programatically?如何以编程方式将应用程序数据复制到android中的sdcard?
【发布时间】:2013-12-04 07:47:55
【问题描述】:

我正在做一个应用程序来备份和恢复 Android 设备中所有用户安装的应用程序。我能够备份用户选择备份的所有应用程序的 apk 文件。现在我必须将应用程序的数据文件与apk文件一起备份。

我正在尝试这样的事情

public void copyDbToSdcard(String packageName) {
    InputStream myInput;
    String dbpath = "/data/"+packageName+"/databases/";
    String sdpath = Environment.getExternalStorageDirectory().getPath();

    try {

        File exist=new File(Environment.getDataDirectory()+dbpath);
        System.out.println("1 "+ exist.exists());

        // Set the output folder on the Scard
        File directory = new File(sdpath + "/Back");
        // Create the folder if it doesn't exist:
        if (!directory.exists()) {
            directory.mkdirs();
        }
        // Set the output file stream up:
        myInput = new FileInputStream(Environment.getDataDirectory()
                + dbpath);

        OutputStream myOutput = new FileOutputStream(directory.getPath()
                + "/refuel_db");

        // Transfer bytes from the input file to the output file
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);    
        }
        // Close and clear the streams

        myOutput.flush();

        myOutput.close();

        myInput.close();

        Toast.makeText(MainActivity.this, "Backup Done Succesfully!", Toast.LENGTH_LONG)
                .show();

    } catch (FileNotFoundException e) {


        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {

        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

我收到错误日志

EACCES (Permission denied)上线

myInput = new FileInputStream(Environment.getDataDirectory()+ dbpath);

我已经在清单文件中授予了 READ 和 WRITE 权限

【问题讨论】:

  • 你不能读取其他应用的隐私数据,除非他们设置了世界读取权限。您还错误地构建了路径(dbpath 是绝对的,因此请单独使用它),但这并不重要,因为即使正确它也不起作用。

标签: android database sqlite


【解决方案1】:

尝试在中使用/sdcard

String dbpath = "/sdcard/"+packageName+"/databases/";

而不是 /data 因为它属于“内部存储”。

【讨论】:

  • @Victor 而不是Environment.getDataDirectory() 尝试使用Enrivornment.getExternalStoragePublicDirectory()
  • 这不是问题
【解决方案2】:

试试这个

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + dbpath);

并将其添加到您的清单文件中

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

希望对你有帮助:)

【讨论】:

  • 我应该在哪里保存这些 java 行?
  • 这与问题无关
  • String sdpath = Environment.getExternalStorageDirectory().getPath();(将其替换为 Environment.getExternalStorageDirectory().toString(); )并且在文件目录中 = new File(sdpath + "/Back" );而不是 "/back" 用 dbpath 替换它
【解决方案3】:
private static String audioFilePath;

布局后布局

audioFilePath =Environment.getExternalStorageDirectory().getAbsolutePath() + "/abcd";
File abcd=new File(audioFilePath);
abcd.mkdirs();

为你的问题............

但你应该在 abcd 的 sd 卡名称中创建目录

【讨论】:

  • 这不是问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-13
  • 1970-01-01
  • 2018-08-17
  • 1970-01-01
  • 2014-06-22
  • 1970-01-01
  • 2016-03-01
相关资源
最近更新 更多