随着Android版本的升级,系统对媒体文件访问进行了限制。
原因:在媒体库中IS_PENDING值为1,标识为被应用独占,更改IS_PENDING为0就可以被其他应用读取到信息
直接更改数据库会报错,需要用音频资源的id生成的资源更改IS_PENDING字段
private void updateMediaDB(File path) {
ContentResolver resolver = AppUtil.getContext().getContentResolver();
String selection = MediaStore.Audio.Media.DATA + " = ?";
String[] selectionArgs = new String[] {path.getAbsolutePath()};
Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Media._ID}, selection, selectionArgs, null);
if (cursor.moveToFirst()) {
long id = cursor.getLong(0);
Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Audio.Media.IS_PENDING, 0);
resolver.update(uri, contentValues, null, null);
}
cursor.close();
}