开发者

moving files around on Froyo

开发者 https://www.devze.com 2023-03-18 18:17 出处:网络
in my application i create a temporary开发者_开发问答 file this way File tmp = File.createTempFile(\"TEST_\", null, getFilesDir());

in my application i create a temporary开发者_开发问答 file this way

File tmp = File.createTempFile("TEST_", null, getFilesDir());

this resolves in a file that toURI()zed corrisponds to something like

/data/data/it.lorenzoff.test/files/TEST_XXX.tmp

In certain circumstances, i'd like to move this file permanently on sdcard but this code

dest = new File("/sdcard/permanentFile");
tmp.renameTo(dest);

never works.

I'm already using these permissions

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

but renameTo continue returning false. What i'm doing wrong?

Thanks in advance

L.


The explanation can be found in documentation for File:

Many failures are possible. Some of the more likely failures include:

  • Write permission is required on the directories containing both the source and destination paths.
  • Search permission is required for all parents of both paths.
  • Both paths [must] 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.

In this case source and target file paths point to different mount points (these two mount points even have different file system). You only option is to manually copy the file to sdcard and then delete the file from internal storage.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号