question

Petros Douvantzis avatar image
Petros Douvantzis asked

Can't play video from contet URi using ACTION_VIEW intent

When the user presses the library button of our video recording app, we launch a "get_content" intent so that he can choose a video:

Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
((Activity) context).startActivityForResult(Intent.createChooser(intent, ""), 1);

This works fine and returns a video URi in onActivityResult(). This is where we want to launch another intent to play the video using the following code:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(data.getData();, "video/*");
startActivity(intent);

This does not work in Kindle devices.

After some digging, I noticed that if I supply the intent with a URi from an absolute filepath, it works. However, I can't have at this point the actual file path, but only the content URi of the user selected video file.

I tried to convert the content URi to an absolute filepath using the following code:

 protected String convertMediaUriToPath(Uri uri) {
        String [] proj={MediaStore.Video.Media.DATA};
        Cursor cursor = getContentResolver().query(uri, proj,  null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
        cursor.moveToFirst();
        String path = cursor.getString(column_index); 
        cursor.close();
        return path;
    }

It does not work in Kindle because the "data" field required to get the filepath is not supported (it works in other devices). I can only get the filename but not the actual path of the video file.

So, is there some way in Kindle devices to playback a video using its content URi?

fire tablet
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

Petros Douvantzis avatar image
Petros Douvantzis answered

I found a solution. In Android 4.4 (and Fire OS 4.x) in order to get the file path from a Uri you have to do something more complicated. There is a library that has a method that already works:

http://stackoverflow.com/a/20559175/941314

So the final code inside "onActivityResult()" is:

Uri uri = data.getData();
String path = getPath(this, uri); // the code is on the above link
File file = new File(path);
Uri fileUri = Uri.fromFile(file);
    			
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(fileUri, "video/*");   			
startActivity(intent);

Message was edited by: Petros Douvantzis

10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.