【问题标题】:Java.io.File - Cannot rename and move file on AndroidJava.io.File - 无法在 Android 上重命名和移动文件
【发布时间】:2015-07-23 14:52:10
【问题描述】:

我无法重命名/移动我的临时文件并打开它

这是我用来创建临时文件的代码

@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
   //[...]
   java.io.File tempFile = java.io.File.createTempFile("filetmp", "_handled", null);
   FileOutputStream fos = new FileOutputStream(tempFile);
   fos.write(responseBody); //responseBody (byte[]) not null
   fos.close();
   //[...]
}

然后,我(尝试)将它保存在磁盘上

private void saveIntoDisk(java.io.File file) {
        if (PersitencyManager.isExternalStorageWritable()) {
            java.io.File dirEvent = this.getParentEvent().getDirectory();
            Log.d("ROOT PATH", "" + dirEvent.getAbsolutePath());
            java.io.File myNewFile = new java.io.File(dirEvent.toString() + "/"+identifiant+"_"+name);
            Log.d("FILE PATH", "" + myNewFile.getAbsolutePath());
            path = myNewFile.getAbsolutePath();
            if (!file.renameTo(myNewFile)) {
                Log.e("File Rename", "Can not rename this file"); // display on console
            } else {
                Log.i("File Rename", "Filed renamed successfully");
            }
        }
}

我创建父文件夹的方式:

public static java.io.File getFolderStorageDir(String folderName) {
        // Get the directory for the user's public pictures directory.
        java.io.File file = new java.io.File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOCUMENTS), folderName);
        if (!file.mkdirs()) {
            if (!file.isDirectory()) {
                Log.e("Directory_Creation", "Directory not created");
            }
        }
        return file;
    }

我在控制台上收到此消息:“无法重命名此文件”。 file.renameTo(myNewFile) 不起作用..

path 似乎不错:

D/FILE PATH:/storage/emulated/0/Documents/12047/4691_test.pdf

这是我的AndroidManifest.xml

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

结果:创建了父文件夹,但没有创建文件...

对我的问题有任何想法吗?

【问题讨论】:

    标签: java android file-io asynchttpclient


    【解决方案1】:

    我发现了问题..

    我无法使用位于其他存储区域的路径重命名文件。

    renameTo() : 两条路径都在同一个挂载点上。在 Android 上,应用程序是 尝试在之间进行复制时最有可能遇到此限制 内部存储和 SD 卡。

    当我创建临时文件时,我在directory 参数处提供null,正如谷歌所说

    目录: [...] 临时文件的默认位置为 null,取自 “java.io.tmpdir”系统属性。 [...]

    所以我的临时目录是在内部存储而不是 SD 卡目录。

    所以,我修改了目录:

    @Override
    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
       //[...]
       java.io.File dir = new java.io.File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DOCUMENTS), folderName);
    java.io.File tempFile = java.io.File.createTempFile("filetmp", "_handled", dir.getAbsolutePath());
       FileOutputStream fos = new FileOutputStream(tempFile);
       fos.write(responseBody); //responseBody (byte[]) not null
       fos.close();
       //[...]
    }
    

    【讨论】:

      【解决方案2】:

      尝试使用 Context 提供的 getFileStreamPath 方法,而不是使用原始 File 构造函数。也就是说,做:

      File oldfile = ctx.getFileStreamPath("shoppinglists.tmp");
      File newfile = ctx.getFileStreamPath("shoppinglists.csv");
      oldFile.renameTo(newFile);
      

      问题可能是 new File() 指的是相对于程序当前目录的名称,这可能不是,当然也不能保证是存储内部文件的目录。

      Rename a file in the internal storage

      【讨论】:

      • 它不起作用..我无法在外部存储上使用此方法
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多