关于项目中audio文件无法播放的报告
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解决了项目中的问题 代码如下: 原代码: case1: IntentintentMyRingtone =newIntent( Intent.ACTION_GET_CONTENT); intentMyRingtone.setType("audio/*"); intentMyRingtone .setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(intentMyRingtone, SELECT_MY_RINGTONE); break; 更改以后的代码: case1: IntentintentMyRingtone =newIntent( 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版本更新后,项目中的代码不适用造成的。 另外:很多开发人员也经常遇到image获取的问题 本文转自HDDevTeam 51CTO博客,原文链接:http://blog.51cto.com/hddev/1548932,如需转载请自行联系原作者