Search code examples
androidhttp-postsd-cardfilenotfoundexception

FileNotFoundException when accessing file stored at /storage/emulated/0 directory in android


I am trying to pass the recorded video file saved in /storage/emulated/0/Videos/someVideo.3gp in android to remote server through POST HTTP method. I used this as a way to pass video file to remote server in FileBody format. But, at the end when I am trying to execute httpClient.execute(request) command it just keep throwing FileNotFoundException.

So, I don't understand why the video file is not accessible to outside world since I had saved it to sdcard using getExternalStorageDirectory(). I also had added write permissions to manifest file.

P.S. I am using Nexus 7 to test this. Please help..

Thanks in advance!


Solution

  • Are you checking the existence of file before executing the httpClient.execute(request) ?

    Referenced from the question that you are following:

    File sourceFile = new File(sourceFileUri);
    if (!sourceFile.isFile()) {
    Log.e("Huzza", "Source File Does not exist");
    return 0;
    }
    

    EDIT:
    May be you are not converting the Path from Uri correctly on onActivityResult(...) method I am sharing the code for getting correct path and it is working nicely in my Application.

    String videoPath = "";
    try 
    { 
     String[] filePathColumn = { MediaStore.Video.Media.DATA };
     Cursor cursor = getContentResolver().query(selectedVideoUri,
     filePathColumn, null, null, null);
     cursor.moveToFirst();
     int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
     videoPath = cursor.getString(columnIndex);
     cursor.close();
    } catch (Exception e) {
     Log.e(TAG,
     "Error parsing Video path = " + e.toString());
    }