BUG现象

Nexus5等部分机型(安卓4.4版本)出现选择自定义铃声后无法播放的现象。

BUG 原因

4.4的某些机型 使用的intent.setAction(Intent.ACTION_GET_CONTENT)获取的uri为

content://com.android.providers.media.documents/document/audio%3A1407

这种格式的uri在播放音乐的方法中不识别报错

4.3及以下版本使用的intent.setAction(Intent.ACTION_GET_CONTENT)获取的uri为

content://media/external/audio/media/91613

可以正常播放

解决办法

以下是Google developer.android.com 官方网页对Android 4.4 APIs的相关改动说明

Storage accessframework

On previousversions of Android, if you want your app to retrieve a specific type of filefrom another app, it must invoke an intent with theACTION_GET_CONTENTaction.This action is still the appropriate way to request a file that you want toimportintoyour app. However, Android 4.4 introduces theACTION_OPEN_DOCUMENTaction,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.

其中提到了:

在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

http://blog.csdn.net/tempersitu/article/details/20557383