0

Sorry,

I have no experience with the Android file system, I am struggling to understand it via the documentation and the tutorials.

I am trying to copy a file from a location to the external storage of my app.

    final File filetobecopied =item.getFile();
    File path=getPrivateExternalStorageDir(mContext);
    final File destination = new File(path,item.getName());
    try 
       {copy(filetobecopied,destination);
  } 
       catch (IOException e) {Log.e("",e.toString());}

public void copy(File src, File dst) throws IOException {
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dst);

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
    Toast.makeText(mContext,"COPIED",Toast.LENGTH_SHORT).show();
}

public File getPrivateExternalStorageDir(Context context) {
    File file = context.getExternalFilesDir(null);
    if (!file.mkdirs()) {
        Log.e("", "Directory not created");
    }
    return file;
}

I get the following error:

09-18 10:14:04.260: E/(7089): java.io.FileNotFoundException: /storage/emulated/0/Android/data/org.openintents.filemanager/files/2013-08-24 13.18.14.jpg: open failed: EISDIR (Is a directory)
user229044
  • 232,980
  • 40
  • 330
  • 338
Lisa Anne
  • 4,482
  • 17
  • 83
  • 157
  • The exception tells that the file you are trying to copy is a directory actually. Have you verified that it really is a file? Anyway, on which line gets the exception thrown? – Kai Sep 18 '13 at 08:27
  • Here you get `13.18.14.jpg` as your file name and then you call `file.mkdirs()` . Possible help http://stackoverflow.com/questions/12339673/fileoutputstream-crashes-with-open-failed-eisdir-is-a-directory-error-when . – rockstar Sep 18 '13 at 08:29
  • @user714965 what I am trying to do is to CREATE the destination file with `final File destination = new File(path,item.getName());` where _path_ is indeed a directory, and `item.getName()` is a string that should be the file name – Lisa Anne Sep 18 '13 at 08:30
  • Did you try printing the values of `path` and `item.getName()`? Also make sure you add `WRITE_EXTERNAL_STORAGE` permission in the manifest file. – Shashank Kadne Sep 18 '13 at 08:56

3 Answers3

2

http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String) use this example of code and use standart examples of google)

Lebedevsd
  • 950
  • 5
  • 12
1

Try

final File destination = new File(path + "/" + item.getName());

instead.

Kai
  • 38,985
  • 14
  • 88
  • 103
1

I assume, folder directory is not created or your external storage state is not mounted.

Before you perform file operations, you should sanitize your path. Following code is a sample code that I often use.

public File sanitizePath(Context context) {
        String state = android.os.Environment.getExternalStorageState();
        File folder = null;
        if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
            folder = new File(context.getFilesDir() + path);
            // path is the desired location that must be specified in your code.
        }else{
            folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + path);
        }
        if (!folder.exists()){
            folder.mkdirs();
        }

        return folder;
    }

And be sure about that, when your directory has to be created before you perform file operations.

Hope this will help.

EDIT: By the way, you have to add following permission to your manifest.xml file

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
gokhanakkurt
  • 4,935
  • 4
  • 28
  • 39