Java.io.File - 无法在 Android 上重命名和移动文件

标签 java android file-io asynchttpclient

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

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

@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) 不起作用..

路径似乎不错:

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" />

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

对我的问题有什么想法吗?

最佳答案

我发现了问题..

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

renameTo() : Both paths be on the same mount point. On Android, applications are most likely to hit this restriction when attempting to copy between internal storage and an SD card.

当我创建临时文件时,我在 directory 参数处给出 null ,正如 Google 所说

directory : [...] null for the default location for temporary files, which is taken from the "java.io.tmpdir" system property. [...]

所以我的临时目录位于内部存储上,而不是 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();
   //[...]
}

关于Java.io.File - 无法在 Android 上重命名和移动文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31590930/

相关文章:

android - 与其他布局相比,约束布局是否昂贵?

java IO将一个文件复制到另一个

python - 修改NamedTemporaryFile的内容(Python 3)

java - 在 BigDecimal 中查找有效数字位数的好方法?

Android View 处理滚动手势但忽略触摸

java - 将 Pojo 组件转换为 EJB3

android - 流媒体库 VLC Android

c++ - std::getline 和 eol 与 eof

java - 使用简单 XML 序列化 TimeUnit

java - 返回一个回文字符串,它不是预期的字符串。背后的机制是什么