Karl-John Chow
Karl-John Chow

Reputation: 805

Android intent.Action_view file permission denied

I am trying to get a media file from my storage and play it by the action_view intent. It's saved in the cache. My code looks like this:

@Override
    public void onClick(View arg0) {

        Intent viewMediaIntent = new Intent();
        viewMediaIntent.setAction(android.content.Intent.ACTION_VIEW);
        File file = new File(prefs.getAssetDir() + "/" + poi.getRoute().getId() + "/"
                + poiMedia.getContentMedia().getMedia().getFileName());
        Log.w("Karl",file.exists()+" < File exists"); 
        // returns true
        viewMediaIntent.setDataAndType(Uri.fromFile(file), "video/*");
        viewMediaIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(viewMediaIntent);
    }
};

The error I get is the Permission Denied Error. I already got the user-permission WRITE_EXTERNAL_STORAGE in the manifest.

Can someone help me please?

Edit:

Logcat:

Failed to open file '/data/data/nl.kloeg.beleefroutes/cache/beleefRoutes/com.burokloeg.beleefroutes.0210OpPadMetRembrandt1/38669.mp4'. (Permission denied)
E/AwesomePlayer(1931): cannot create dataSource
W/AwesomePlayer(1931): onPrepareAsyncEvent() finishSetDataSource_l error(-2147483648)
W/AwesomePlayer(1931): abortPrepare() mIsAsyncPrepare (1), err (-2147483648)

Upvotes: 2

Views: 2589

Answers (2)

Kirill Karmazin
Kirill Karmazin

Reputation: 6736

You probably would want to use FileProvider https://developer.android.com/reference/android/support/v4/content/FileProvider and check this StackOverFlow answer for a sample code: FileProvider is very confusing

Upvotes: 0

Kuang Lin
Kuang Lin

Reputation: 43

This may be late but I encountered the same thing. The way I solved it by adding

    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

hope this help someone.

Upvotes: 3

Related Questions