1

I want to know why this error comes when try to upload file with real device using Marshmallow. Can you please explain the solution for this.

12-28 10:39:32.606: E/3(17552): Excption : java.io.FileNotFoundException: /storage/emulated/0/WoodenstreetD.doc: open failed: ENOENT (No such file or directory)

I was stuck with this for last week and still getting same issue. Thanks In Advance.

  • Please add your code..!! – AndiGeeky Dec 28 '15 at 05:17
  • http://stackoverflow.com/questions/11620641/android-error-open-failed-enoent .Check this link it's because of write protection of sd card – Vivek Mishra Dec 28 '15 at 05:17
  • I ask question for this here is link - http://stackoverflow.com/questions/34469351/getting-error-while-uploading-file-from-real-device-to-server –  Dec 28 '15 at 05:18
  • If the message of the exception claims that the specified file is a directory, then you must either alter the name of the file or delete the existing directory (if the directory is not being used by an application). – IntelliJ Amiya Dec 28 '15 at 05:19
  • How may i do this, please suggest and help me. I am new to android. –  Dec 28 '15 at 05:22
  • i assume problem is here `file1 = new File(Environment.getExternalStorageDirectory() .getAbsolutePath(), file_1); fileBody1 = new FileBody(file1);` – IntelliJ Amiya Dec 28 '15 at 05:37
  • I did same already as you told, but not working –  Dec 28 '15 at 05:39
  • `file1` getting nothing .Print it first time – IntelliJ Amiya Dec 28 '15 at 05:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99090/discussion-between-coreand-and-intellij-amiya). –  Dec 28 '15 at 05:43
  • String : WoodenstreetD.doc file : /storage/emulated/0/document/ECCA-8724:WoodenstreetD.doc filebody 1 : org.apache.http.entity.mime.content.FileBody@350889e2 –  Dec 28 '15 at 05:50

2 Answers2

2

This error comes due to wrong path. The path your getting from onActivityResult is not correct. Use this to get path of selected file.

public static String getRealPathFromUri(Context ctx, Uri uri) {
    String[] filePathColumn = { MediaStore.Files.FileColumns.DATA };

    Cursor cursor = ctx.getContentResolver().query(uri, filePathColumn,
            null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        picturePath = cursor.getString(columnIndex);
        Log.e("", "picturePath : " + picturePath);
        cursor.close();
    }
    return picturePath;
}

picturePath gives you full path of image/file.

It'll work for me.

0

i worte this and it'll work for me.

 public static java.io.File convertUriTFile(Activity activity, Uri uri) {
        InputStream inputStream = null;
        java.io.File file = null;
        try {
            inputStream = activity.getContentResolver().openInputStream(uri);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            file = new java.io.File(activity.getCacheDir(), "temp");
            try (OutputStream output = new FileOutputStream(file)) {
                byte[] buffer = new byte[4 * 1024];
                int read;

                while ((read = inputStream.read(buffer)) != -1) {
                    output.write(buffer, 0, read);
                }

                output.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return file;

    }