0

I'm trying to relate videos of my gallery to my app. When I open an activity to search for that video, I'm able when the activity gives me the result, but after I get out of this activity and try to access the same video again, the phone says that is impossible to open the video. I know that my permission to watch it in the first moment is just temporary, I'd like to make it permanent, so I can access those videos whenever I want. Thanks for the attention! This the code that I use to search for my video

addVideoLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.INTERNAL_CONTENT_URI);
            i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivityForResult(i, Constants.PICK_VIDEO);
        }
    });

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == Constants.PICK_VIDEO && resultCode == RESULT_OK) {
        mVideoPath = data.getData().toString();
        addVideoText.setVisibility(View.GONE);
        videoView.setVideoPath(mVideoPath);
        videoView.start();
        videoView.setBackground(null);
    }
}

And this is the Manifest:

<?xml version="1.0" encoding="utf-8"?>

<permission android:name="org.example.android.guitarHelper.provider.READWRITE"/>
<uses-permission android:name="org.example.android.guitarHelper.provider.READWRITE"/>


<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="Guitar Helper"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".EditSongActivity"
        android:label="@string/title_activity_new_song"
        android:parentActivityName=".MainActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.danielspeixoto.guitarhelper.MainActivity" />
    </activity>
    <activity
        android:name=".RemindersActivity"
        android:label="Reminders"
        android:parentActivityName=".MainActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.danielspeixoto.guitarhelper.MainActivity" />
    </activity>
    <activity
        android:name=".AddSongActivity"
        android:label="Add Song"
        android:parentActivityName=".MainActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.danielspeixoto.guitarhelper.MainActivity" />
    </activity>
    <activity
        android:name=".SearchActivity"
        android:label="@string/title_activity_search"
        android:theme="@style/AppTheme.NoActionBar"></activity>
    <provider android:name="com.example.danielspeixoto.guitarhelper.GuitarProvider"
        android:authorities="com.example.danielspeixoto.guitarhelper.provider"
        android:exported="true"
        android:readPermission="com.example.danielspeixoto.guitarhelper.provider.READWRITE"
        android:writePermission="com.example.danielspeixoto.guitarhelper.READWRITE"/>

</application>

1 Answers1

0

Persisting URI permissions is only possible on Android 4.4+. And even then only if the other application supports it. See the documentation of ContentResolver.takePersistableUriPermission() and Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION.

However, you should be able to pass on the temporary URI permission to another Activity. For that put the content URI you received in the data field of the Intent used to start your second Activity. Then add FLAG_GRANT_READ_URI_PERMISSION to that Intent.

Since you're using MediaStore and already have the permission to read external storage you could also just retrieve the path to the file on external storage. See this answer to the question "Get filename and path from uri from mediastore".

Community
  • 1
  • 1
cketti
  • 1,277
  • 11
  • 15