将uri保存到sharedPreferences并与Mediaplayer一起播放 [英] Save uri to sharedPreferences and play with mediaplayer

查看:125
本文介绍了将uri保存到sharedPreferences并与Mediaplayer一起播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用来将Uri的字符串表示形式保存到SharedPreferences的代码:

This is the code I use to save the string representation of the Uri to the 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 append either of the following two right here it works without a problem:

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

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

但是,当我稍后稍后尝试在另一个活动中创建MediaPlayer时,它不起作用:

However when I try to create a MediaPlayer later in another activity like this, it does not work:

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

我不知道保存和加载到sharedPreferences会有什么不同.据我所知,从调试来看, uri 与上面的 Uri.parse(data.getData().toString())的结果相同.

I'm lost at what difference the saving and loading to sharedPreferences could reasonably make. From debugging as far as I can tell uri is identical to the result of Uri.parse(data.getData().toString()) from above.

非常感谢您的帮助和指点.

Any help and pointers are greatly appreciated.

最诚挚的问候

朱利叶斯

我以为我可以通过在getApplicationContext()上下文中调用MediaPlayer.create()方法来解决此问题,但是关闭应用程序并重新打开它会使方法调用再次失灵.

I thought I solved this by calling the MediaPlayer.create() method with the getApplicationContext() context, but closing the application and reopening it renders the method call disfunctional again.

推荐答案

遇到了同样的问题,并在此处找到了解决方法:
http://www.andreamaglie.com/access-storage-framework-uri -permission/
在这里
https://stackoverflow.com/a/25194460/1993098

Had the same issue and found the solution here:
http://www.andreamaglie.com/access-storage-framework-uri-permission/
and here
https://stackoverflow.com/a/25194460/1993098

当您使用KitKat +(SDK 19+)时,此修复程序将为您提供帮助.基本上,当您从首选项中保存和检索URI时,您会失去访问文件的权限.通过上述步骤,您可以保留系统的此权限,因此以后将允许该应用访问文件.例如,我用它来选择一个警报声音,随后将被触发的服务检索并播放.

This fixes will help you, when you use KitKat+ (SDK 19+). Basically you loose the permission to access the file when you save and retrieve the URI from preferences. With the steps described you can persist this permission by the system and so the app will be allowed to access the file later. I for example used this to pick an alarm sound, which will than later be retrieved by an triggered service and played.

总结一下(也可以在一段时间后无法访问第一个站点的情况):

To sum it up (also in case the first site shouldn't be reachable after some time):

需要权限

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

获取URI (使用ACTION_OPEN_DOCUMENT很重要)

Getting the URI (it is 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);

读取URI并保留读取权限(在onActivityResult中)

Reading the URI and persisting the permission to read (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());

从首选项"中检索URI

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

getPrefs()...显然必须是您自己的首选项实现.

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

这篇关于将uri保存到sharedPreferences并与Mediaplayer一起播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆