Einar Sundgren
Einar Sundgren

Reputation: 4423

Mediaplayer fails to open parsed URI

Trying to open a file using Uri.parse, but I keep getting denied access to it. I'm very puzzled of what is wrong.

The Manifest.xml cropped for readability

<application>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</application>

The code snippet causing the error:

Uri video = Uri.parse(Environment.getExternalStorageDirectory()
               .getAbsolutePath()+"/victamjpeg.sdp");

videoView.setVideoURI(video);

And the actual error:

10-30 01:11:38.640 4293-4293/? W/MediaPlayer: Couldn't open file on client side; trying server side: java.io.FileNotFoundException: No content provider: /storage/1A0C-331C/victamjpeg.sdp

10-30 01:11:38.640 4293-4293/? W/VideoView: java.io.FileNotFoundException: /storage/1A0C-331C/victamjpeg.sdp: open failed: EACCES (Permission denied)

10-30 01:11:38.640 4293-4293/? W/VideoView: at java.io.FileInputStream.(FileInputStream.java:76) 10-30 01:11:38.640 4293-4293/? W/VideoView: at java.io.FileInputStream.(FileInputStream.java:76) 

It looks like I don't have the permissions to read it, but I have to my knowledge set that permission. Doing an ls -l from adb shell proves that the file is at /sdcard/victamjpeg.sdp and owned by root.

Upvotes: 1

Views: 10057

Answers (2)

CommonsWare
CommonsWare

Reputation: 1006704

First, it appears that your <uses-permission> elements are in the wrong place. They need to be children of <manifest>, peers of <application>. What you have shows them as children of <application>.

Second, replace:

Uri video = Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+"/victamjpeg.sdp");

with:

Uri video = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "victamjpeg.sdp"));

This handles that case where getExternalStorageDirectory() returns a value with a trailing /. It also puts the scheme in the Uri, which your implementation does not, resulting in a universally invalid Uri.

Also note that if you are testing on Android 6.0 that the storage permissions are dangerous, and you will need to use the runtime permission system if your targetSdkVersion is 23 or higher.

Upvotes: 4

Selva
Selva

Reputation: 338

The uses-permission tags need to be outside the application tag.

Upvotes: 3

Related Questions