I am trying to copy a file to another folder in the android, but so far, i got no success. I manage to do so with a selected image and when taking a photo, but not with files.
I've read and tried several solutions passed by the community (searched over the forum and the internet), but none of it was able to solve my problem when copying.
First things first. I added the permissions to my manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
after that, before copying a file, i print its filepath and the directory file path:
06-10 11:11:11.700: I/System.out(1442): /mimetype/storage/sdcard/Misc/Javascript erros for Submit and Plan buttons in IE.doc
06-10 11:11:11.710: I/System.out(1442): /storage/sdcard/files/queue
both exists:
to copy the file to the expected folder I used the FileUtils:
try {
FileUtils.copyFile(selectedFile, dir);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The problem is: I get no exception and the file isn't there. I tried this solution either: How to move/rename file from internal app storage to external storage on Android?
same thing, no exception, but no file either.
**Edited**
This is how I get the file.
Uri uri = data.getData();
File selectedFile = new File(uri.getPath());
File dir = new File(Environment.getExternalStorageDirectory(), "files/queue");
To get the image, I do this way:
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
saveChecklistImage(BitmapFactory.decodeFile(picturePath));
I retrieve the file here when I call BitmapFactory.decodeFile(picturePath), then, I save it without a problem. Should file follow this standard as well?
**Solution**
I found out a way to do so in this post: android get real path by Uri.getPath()
Specifically this method:
private String getRealPathFromURI(Uri contentURI) {
String result;
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) { // Source is Dropbox or other similar local file path
result = contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(idx);
cursor.close();
}
return result;
kind of same thing i was doing with image, but in a much smarter way ;D
easier than I tought. Thanks for the helpers