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" />
What you are then doing is trying to concatenate this onto
Environment.getExternalStorageDirectory(). This will not work.content://media/external/images/media/635is neither a relative filesystem path nor an absolute filesystem path. It is aUri.If you wish to copy the image from the
Urito a local file, use aContentResolverto get anInputStreamon the image represented by theUri, then use Java I/O to copy the bytes from theInputStreamto your target file.