contentResolver.Query错误在Android 10
我在Playstore上启动了我的应用程序。我在开发人员控制台中看到,运行Android 10的设备有错误。日志如下:
错误发生在我的prepard> prepard> prepar()
方法的contentResolver.query行
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
return;
}
Map<String, ?> carpetasMap = carpetasPreferences.getAll();
String[] carpetasArray = null;
StringBuilder selectionPlaceHolders = new StringBuilder(MediaStore.Audio.Media.DATA + " like ?");
carpetasArray = new String[carpetasMap.size()];
int indice = 0;
for (Map.Entry<String, ?> asno: carpetasMap.entrySet()) {
carpetasArray[indice] = "%" + asno.getValue().toString() + "/%";
selectionPlaceHolders.append(" OR " + MediaStore.Audio.Media.DATA + " like ?");
indice++;
}
if (carpetasMap.size() == 1) {
selectionPlaceHolders = new StringBuilder(MediaStore.Audio.Media.DATA + " like ?");
}
if (carpetasMap.size() == 0) {
if (notClickedOnce) {
carpetasArray = new String[rutas.length];
//carpetas de musica al presionar boton por primera vez
carpetasArray[0] = "%" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/Music/%";
if (rutas.length > 1) {
carpetasArray[1] = "%" + rutas[1] + "%";
selectionPlaceHolders.append(" OR " + MediaStore.Audio.Media.DATA + " like ?");
}
}
}
ContentResolver contentResolver = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri, null, String.valueOf(selectionPlaceHolders), carpetasArray, null);
if (cursor == null) {
//falló el query, manejar el error
} else if (!cursor.moveToFirst()) {
// no media en el dispositivo
} else {
int idColumn = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
do {
long thisId = cursor.getLong(idColumn);
listaCanciones.add(thisId);
} while (cursor.moveToNext());
cursor.close();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
中play()方法。
我试图在模拟器运行Android 10上重现错误。但是它可以完美地运行而没有任何错误。
我试图在物理设备(Galaxy Note 9)运行Android 10上重现该错误。但是它在那里也完美地运行而没有任何错误。
我在另一个植物设备上尝试了该应用程序,也运行了Android 7、8、11、12;在所有这些方面都没有任何错误。
您能帮助找出导致错误的原因吗?
I launched my app on the PlayStore. And I see in my developer console that a device runing Android 10 has an error. The log is the following:
The error occurs in the contentResolver.query line of my prepare()
method:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
return;
}
Map<String, ?> carpetasMap = carpetasPreferences.getAll();
String[] carpetasArray = null;
StringBuilder selectionPlaceHolders = new StringBuilder(MediaStore.Audio.Media.DATA + " like ?");
carpetasArray = new String[carpetasMap.size()];
int indice = 0;
for (Map.Entry<String, ?> asno: carpetasMap.entrySet()) {
carpetasArray[indice] = "%" + asno.getValue().toString() + "/%";
selectionPlaceHolders.append(" OR " + MediaStore.Audio.Media.DATA + " like ?");
indice++;
}
if (carpetasMap.size() == 1) {
selectionPlaceHolders = new StringBuilder(MediaStore.Audio.Media.DATA + " like ?");
}
if (carpetasMap.size() == 0) {
if (notClickedOnce) {
carpetasArray = new String[rutas.length];
//carpetas de musica al presionar boton por primera vez
carpetasArray[0] = "%" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/Music/%";
if (rutas.length > 1) {
carpetasArray[1] = "%" + rutas[1] + "%";
selectionPlaceHolders.append(" OR " + MediaStore.Audio.Media.DATA + " like ?");
}
}
}
ContentResolver contentResolver = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri, null, String.valueOf(selectionPlaceHolders), carpetasArray, null);
if (cursor == null) {
//falló el query, manejar el error
} else if (!cursor.moveToFirst()) {
// no media en el dispositivo
} else {
int idColumn = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
do {
long thisId = cursor.getLong(idColumn);
listaCanciones.add(thisId);
} while (cursor.moveToNext());
cursor.close();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
I ask for the READ_EXTERNAL_STORAGE
permission in my play() method.
I tried to reproduce the error on the emulator runing Android 10. But it runs perfect without no errors.
I tried to reproduce the error on a physical device (Galaxy Note 9) runing Android 10. But it runs perfect there too without no errors.
I tried the app on another phycical devices too runing Android 7, 8, 11, 12; and on all of them runs fine without no errors.
Can you help to find out what is causing the error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论