Save uri to sharedPreferences and play with media player

This is the code I am using to save the string representation of the Uri to SharedPreferences:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQ_CODE_PICK_SOUNDFILE && resultCode == Activity.RESULT_OK){
        if ((data != null) && (data.getData() != null)){
            SharedPreferences sharedPref = getSharedPreferences("customName", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putString("uritostring", data.getData().toString());
            editor.apply();

        }
    }
}

      

Note that when I add one of the following two right here, it works without issue:

MediaPlayer mp=MediaPlayer.create(this,data.getData());

MediaPlayer mp=MediaPlayer.create(this,Uri.parse(data.getData().toString()));

      

However, when I try to create the MediaPlayer later in another similar activity, this doesn't work:

SharedPreferences sharedPref = getSharedPreferences("customName", Context.MODE_PRIVATE);
Uri uri = Uri.parse(sharedPref.getString("uritostring","defaultUriString"));
mp.create(this,uri);

      

I am lost with what difference can save and load sharedPreferences. From debugging, as far as I can tell, the uri is identical to the result of Uri.parse (data.getData (). ToString ()) from above.

Any hints and pointers would be greatly appreciated.

Regards

Julius

EDIT:

I figured I solved it by calling the method MediaPlayer.create()

with the context getApplicationContext()

, but closing the application and reopening it again results in a dysfunctional method call.

+3


source to share


1 answer


Had the same problem and found a solution here:
http://www.andreamaglie.com/access-storage-framework-uri-permission/
and here
fooobar.com/questions/106928 / ...

These fixes will help you when you are using KitKat + (SDK 19+). Basically you lose the file permission when you save and retrieve the URI from the settings. With the steps described, you can keep this permission by the system, and therefore the application will be allowed to access the file later. For example, I used this to select a beep that would later receive the called service and play.

To summarize (also in case the first site cannot be accessed after a while):

Permission required

<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />

      

Getting URI (important to use ACTION_OPEN_DOCUMENT)



Intent audioIntent = new Intent();
audioIntent.setType("audio/*");
audioIntent.setAction(Intent.ACTION_OPEN_DOCUMENT);
audioIntent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(audioIntent, PICK_AUDIO_REQUEST);

      

Read URI and save read permission (in onActivityResult)

        Uri alarmSoundUri = data.getData();

        getActivity().grantUriPermission(getActivity().getPackageName(), alarmSoundUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
        final int takeFlags = data.getFlags() & Intent.FLAG_GRANT_READ_URI_PERMISSION;
        // Check for the freshest data.
        //noinspection WrongConstant
        getActivity().getContentResolver().takePersistableUriPermission(alarmSoundUri, takeFlags);
        getPrefs().setAlarmSoundUri(alarmSoundUri.toString());

      

Getting URI from settings

String alarmSoundUri = getPrefs().getAlarmSoundUri();
Uri parsedUri = Uri.parse(alarmSoundUri);
...

      

getPrefs () ... should obviously be your own preference implementation.

0


source







All Articles