Nexus5等部分机型(安卓4.4版本)出现选择自定义铃声后没法播放的现象。html
4.4的某些机型 使用的intent.setAction(Intent.ACTION_GET_CONTENT)获取的uri为android
content://com.android.providers.media.documents/document/audio%3A1407app
这种格式的uri在播放音乐的方法中不识别报错ide
4.3及如下版本使用的intent.setAction(Intent.ACTION_GET_CONTENT)获取的uri为测试
content://media/external/audio/media/91613ui
能够正常播放spa
如下是Google developer.android.com 官方网页对Android 4.4 APIs的相关改动说明.net
Storage accessframeworkhtm
On previousversions of Android, if you want your app to retrieve a specific type of filefrom another app, it must invoke an intent with the ACTION_GET_CONTENT action.This action is still the appropriate way to request a file that you want toimport intoyour app. However, Android 4.4 introduces the ACTION_OPEN_DOCUMENT action,which allows the user to select a file of a specific type and grant your applong-term read access to that file (possibly with write access) withoutimporting the file to your app.blog
其中提到了:
在4.3或如下能够直接用ACTION_GET_CONTENT的,在4.4或以上,官方建议用ACTION_OPEN_DOCUMENT
可是根据其说明ACTION_GET_CONTENT是能够用的,只是ACTION_OPEN_DOCUMENT被推荐而已。可是事实上某些4.4机型(neuxs 5等)若是还用ACTION_GET_CONTENT的方法,返回的uri跟4.3是彻底不同的,4.3返回的是带文件路径的,而4.4返回的倒是content://com.android.providers.media.documents/document/audio:3951这样的,没有路径,只有音频编号的uri.
因此为了解决这种问题,咱们尝试使用Google推荐的ACTION_OPEN_DOCUMENT action
测试结果显示,使用官方推荐的action解决了项目中的问题
代码以下:
原代码:
case 1:
IntentintentMyRingtone = new Intent(
Intent.ACTION_GET_CONTENT);
intentMyRingtone.setType("audio/*");
intentMyRingtone .setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intentMyRingtone,
SELECT_MY_RINGTONE);
break;
更改之后的代码:
case 1:
IntentintentMyRingtone = new Intent(
Intent.ACTION_GET_CONTENT);
intentMyRingtone.setType("audio/*");
if (Build.VERSION.SDK_INT < 19) {
intentMyRingtone
.setAction(Intent.ACTION_GET_CONTENT);
}else{
intentMyRingtone
.setAction(Intent.ACTION_OPEN_DOCUMENT);
}
startActivityForResult(intentMyRingtone,
SELECT_MY_RINGTONE);
break;
机型问题有时候老是伴随着版本问题,遇到问题关注Android版本的不一样有时候很重要。
前期问题没改出来也是没有考虑到报错手机的一直特征是4.4.
Google搜索的关键词也很重要,它有时候决定整个你修改bug的方向问题,提炼出正确的搜索关键字可以快速找到相关的文章,提供了别人解决问题的经验。
关注Android版本更新,了解重要的更新说明,项目的不少bug都是Android版本更新后,项目中的代码不适用形成的。
另外:不少开发人员也常常遇到p_w_picpath获取的问题
另附两个地址供遇到p_w_picpath文件获取uri问题的朋友参考:http://blog.csdn.net/eastman520/article/details/17756817