5

I am trying to pick a video, selecting and displaying the video works fine, but when I try to get the full path to the video file I get a wrong path.

In the following code fragment I am trying to obtain the full path to the file:

Uri uri = Uri.parse(url);
File file = new File((uri.getPath()));

And then when I set the multipart entity filebody I pass it the file.toString().

I am getting the following exception when trying to upload the file.

03-28 12:43:27.129: W/System.err(6018): java.io.FileNotFoundException: /content:/media/external/video/media/32398: open failed: ENOENT (No such file or directory)
03-28 12:43:27.157: W/System.err(6018):     at libcore.io.IoBridge.open(IoBridge.java:416)
03-28 12:43:27.157: W/System.err(6018):     at java.io.FileInputStream.<init>(FileInputStream.java:78)
03-28 12:43:27.157: W/System.err(6018):     at org.apache.http.entity.mime.content.FileBody.writeTo(FileBody.java:92)
03-28 12:43:27.165: W/System.err(6018):     at org.apache.http.entity.mime.HttpMultipart.doWriteTo(HttpMultipart.java:206)
03-28 12:43:27.165: W/System.err(6018):     at org.apache.http.entity.mime.HttpMultipart.writeTo(HttpMultipart.java:224)
03-28 12:43:27.169: W/System.err(6018):     at org.apache.http.entity.mime.MultipartEntity.writeTo(MultipartEntity.java:183)
03-28 12:43:27.169: W/System.err(6018):     at org.apache.http.impl.entity.EntitySerializer.serialize(EntitySerializer.java:97)
03-28 12:43:27.169: W/System.err(6018):     at org.apache.http.impl.AbstractHttpClientConnection.sendRequestEntity(AbstractHttpClientConnection.java:162)
user1940676
  • 4,348
  • 9
  • 44
  • 73

3 Answers3

2

you will need to query MediaStore.Images.Media.DATA column to get real path of File using URI from MediaStore.Images.Media ContentProvider.

see following post for getting filename and path from uri from mediastore

Community
  • 1
  • 1
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
2

Get file path from URI::

   public String getRealPathFromURI(Context context, Uri contentUri) {
      Cursor cursor = null;
      try { 
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
      } finally {
        if (cursor != null) {
          cursor.close();
        }
      }
    }
R_K
  • 803
  • 1
  • 7
  • 18
1

I tried all the various answers on StackOverflow regarding taking Uris and turning them into File/file paths, and none of them worked. I asked around, and it appears that this behavior is no longer supported. It was suggested that instead if you need access to the video/image: "Just open the URI directly with ContentResolver's openInputStream".

RyanCheu
  • 3,522
  • 5
  • 38
  • 47