【问题标题】:Can't create new folders on SD card to backup many file无法在 SD 卡上创建新文件夹以备份许多文件
【发布时间】:2019-07-27 06:44:15
【问题描述】:

我正在尝试备份目录,但卡在无法创建新文件夹。

首先,我授予 Manifest 权限并已请求运行时权限:

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

注意:我的设备上的Environment.getExternalStorageDirectory() 返回内部存储而不是 SdCard。但是,我使用 File Picker 从 SdCard 中选择文件并正确 READ 其数据。

但是,这是我备份目录的方法:

private void backupSmaliDir(String smaliDir){
    StringBuilder result = new StringBuilder("");
    String backupDirPath = smaliDir+"-BACKUP";
    StringBuffer listFile = getAllFiles(new File(smaliDir));
    String[] lines = listFile.toString().split("\n");

    result.append(String.format("BACKUP DIR: %s\nHAVE %s file to backup\n", backupDirPath, String.valueOf(lines.length)));
    try {
        for (String path : lines) {
            File f = new File(path.replaceFirst(smaliDir, backupDirPath));
            File newf = f.getParentFile();
            result.append(String.format("\n Old: %s\n New: %s\n", path, newf.getCanonicalPath()+"/"+ new File(path).getName()));

            boolean isCopied = false;
            result.append(String.format("[w] EXISTS: %s | MKDIRS: %s\n", newf.exists(), newf.mkdirs()));
            if  (newf.exists() || newf.mkdirs()) {
                // just copy path (FILE) to newf (FILE)
                File fi = new File(path);
                File fo = new File(newf.getCanonicalPath()+"/"+fi.getName());
                copyFileUsingStream(fi, fo);
                isCopied = true;
            }
            if (isCopied) result.append("\tCopied!\n");
            else result.append("\tCanceled!\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    saveBackupLog(result.toString());
}

我不知道为什么newf.exists() || newf.mkdirs() 总是返回False,这意味着我的 SD 卡上没有新的目录。

有一些结果日志:

Smali Directory: /storage/AF7E-4CF0/toolbox/IPHONE_CONTROL_PSYCHO/smali

BACKUP DIR: /storage/AF7E-4CF0/toolbox/IPHONE_CONTROL_PSYCHO/smali-BACKUP
HAVE 141 file to backup

 Old: /storage/AF7E-4CF0/toolbox/IPHONE_CONTROL_PSYCHO/smali/android/support/v9/app/a.smali
 New: /storage/AF7E-4CF0/toolbox/IPHONE_CONTROL_PSYCHO/smali-BACKUP/android/support/v9/app/a.smali
[w] EXISTS: false | MKDIRS: false
    Canceled!

 Old: /storage/AF7E-4CF0/toolbox/IPHONE_CONTROL_PSYCHO/smali/android/support/v9/app/aa.smali
 New: /storage/AF7E-4CF0/toolbox/IPHONE_CONTROL_PSYCHO/smali-BACKUP/android/support/v9/app/aa.smali
[w] EXISTS: false | MKDIRS: false
    Canceled!

 Old: /storage/AF7E-4CF0/toolbox/IPHONE_CONTROL_PSYCHO/smali/android/support/v9/app/ab.smali
 New: /storage/AF7E-4CF0/toolbox/IPHONE_CONTROL_PSYCHO/smali-BACKUP/android/support/v9/app/ab.smali
[w] EXISTS: false | MKDIRS: false
    Canceled!
.....

注意 2:如果我在 Internal Storage 中备份一个目录,例如 /storage/emulated/0/tool,则此代码运行良好! 那么有什么问题以及我该如何解决呢?

【问题讨论】:

  • 试试 -> newf.setExecutable(true);
  • @gnanajeyam95 感谢您的建议,但仅包括创建文件夹 backupDirPath 是行不通的。

标签: java android file android-sdcard android-external-storage


【解决方案1】:

在清单中添加以下内容:

<application
    android:requestLegacyExternalStorage="true"
    ...

因为要求 Manifest.permission.WRITE_EXTERNAL_STORAGE 对于 Android 10+ 来说已经不够用了。

【讨论】:

    猜你喜欢
    • 2018-05-18
    • 2020-04-10
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    相关资源
    最近更新 更多