问题
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