Move/Rename file in SD Card

有些话、适合烂在心里 提交于 2019-12-10 12:15:17

问题


I'm trying to move a file from one directory to another (in SD Card)

I have a file's URI and the way I'm trying to move it:

Uri selectedImage = imageReturnedIntent.getData(); // this the uri, something like content://media/external/images/media/635

File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard, selectedImage);
File to = new File(sdcard, "myNewDir/mynewfile.jpg");
from.renameTo(to);

But it doesnt work, neither does it give me any error in Logcat...

Edit:

I have added both permissions to my manifest file:

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

回答1:


// this the uri, something like content://media/external/images/media/635

What you are then doing is trying to concatenate this onto Environment.getExternalStorageDirectory(). This will not work. content://media/external/images/media/635 is neither a relative filesystem path nor an absolute filesystem path. It is a Uri.

If you wish to copy the image from the Uri to a local file, use a ContentResolver to get an InputStream on the image represented by the Uri, then use Java I/O to copy the bytes from the InputStream to your target file.



来源:https://stackoverflow.com/questions/9065514/move-rename-file-in-sd-card

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!