contentResolver.Query错误在Android 10

发布于 2025-02-11 10:17:09 字数 2865 浏览 0 评论 0原文

我在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:

Error Log

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文