0

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

Community
  • 1
  • 1
  • You most likely have a spelling error. Double check it twice. – Anubian Noob Jun 10 '14 at 15:25
  • I am checking the bytes to see if it is comming empty through FileUtils.readFileToByteArray(selectedFile). –  Jun 10 '14 at 15:35
  • I did the test here, FileUtils is supressing exceptions. Let me post how I am getting the file. Maybe the problem is on it. I`ll edit the post –  Jun 10 '14 at 15:59

2 Answers2

2

If argument 2 is a directory you should be using copyFileToDirectory.

Travis Biehn
  • 116
  • 1
  • It is always a file, will never be a directory. –  Jun 10 '14 at 15:34
  • @AndreFróes From the looks of your example `dir` certainly is a directory just as Travis has said. Yes you're copying a file, but the destination you provide is a directory. – Grambot Jun 10 '14 at 15:40
  • Ops, sry for the misunderstanding there then. I just changed to copytodirectory, but still no success. –  Jun 10 '14 at 15:58
0

copyFile() doesn't take directory as a 2nd parameter (see here). It takes file (as source) and file (as destination). Create an empty file first:

File file = new File(dir+"/newfile.doc");
if (!file.exists()) {
    try {
        file.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

Use that instead of dir. Or use Travis' way. Also replace [space] with _ in your filenames.

tritop
  • 1,655
  • 2
  • 18
  • 30