移动文件(外部SD卡)

时间:2017-02-17 08:06:29

标签: java android file access

我尝试将文件从一个文件夹移动到另一个文件夹,如下所示:

File fileFrom = new File("/storage/1AF5-2B0F/test/testfile");
File fileTo = new File("/storage/1AF5-2B0F/test2/testfile");
boolean moveok = fileFrom.renameTo(fileTo);

moveok是假的

当使用内部SD卡进行相同操作时,它可以正常工作。如何在外部SD卡上移动文件?

我有写权限

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

2 个答案:

答案 0 :(得分:1)

使用Environment.getExternalStorageDirectory()转到external storage (SD)

的根目录
 File from = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"1AF5-2B0F/test/testfile.jpg");
    File to = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/1AF5-2B0F/test2/testfile.jpg");
boolean moveok = from.renameTo(to);

Reference

答案 1 :(得分:0)

问题可能出在外部SD卡目录中。

File from = new File(/storage/1AF5-2B0F/test/testfile");
// You can use the Environment.getExternalStorageDirectory() to get to the root of external storage (which, on some devices, is an SD card).
File to = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/test/testfile"); 
from.renameTo(to);