外部存储权限问题与MediaProvider /振铃音 [英] External Storage Permission Issue with MediaProvider / Ring Tones

查看:316
本文介绍了外部存储权限问题与MediaProvider /振铃音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一些用户报告试图选择在我的应用程序铃声时,谷歌播放下面的错误。 (还有更多的,但它是不相关)

Some of my users have reported to Google Play the following error when trying to select a ringtone in my app. (there's more to it, but it's not relevant)

java.lang.SecurityException: Permission Denial: 
reading com.android.providers.media.MediaProvider 
uri content://media/external/audio/media 
from pid=5738, uid=10122 requires android.permission.READ_EXTERNAL_STORAGE

我认为这个问题的发生是由于某些音调是在外部存储。我不想要在我的应用程序的 READ_EXTERNAL_STORAG​​E 的权限,除非我绝对必须的。

I assume this issue is happening due to certain tones that are on external storage. I don't want to include the READ_EXTERNAL_STORAGE permission in my app unless I absolutely have to.

有没有办法规避这个问题,只是排除任何音,可能是在外部存储?

Is there a way to circumvent the issue and just exclude any tones that may be on the external storage?

注:我收到的铃声与 RingtoneManager ,并将其转换字符串之间乌里。没有其他的code接触到用户的媒体。

Note: I'm getting ringtones with RingtoneManager and convert them between String and Uri. No other code is touching the user's media.

另外,我没有行号,因为堆栈跟踪是混淆code和重新映射的堆栈跟踪未提供行号。

Also, I do not have a line number since the stacktrace is from obfuscated code and re-mapping the stack trace did not provide line number.

推荐答案

刚刚有同样的问题,并提出了以下解决方案:

Just had the same problem and came up with the following solution:

private Cursor createCursor()
{
    Uri uri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;

    String[] columns = new String[]
    {
        MediaStore.Audio.Media._ID,
        MediaStore.Audio.Media.TITLE,
        MediaStore.Audio.Media.TITLE_KEY
    };

    String filter = createBooleanFilter(MediaStore.Audio.AudioColumns.IS_ALARM);
    String order = MediaStore.Audio.Media.DEFAULT_SORT_ORDER;

    return getContext().getContentResolver().query(uri, columns, filter, null, order);
}

private String createBooleanFilter(String... columns)
{
    if(columns.length > 0)
    {
        StringBuilder sb = new StringBuilder();
        sb.append("(");
        for(int i = columns.length - 1; i > 0; i--)
        {
            sb.append(columns[i]).append("=1 or ");
        }
        sb.append(columns[0]);
        sb.append(")");
        return sb.toString();
    }
    return null;
}

要得到你需要结合 INTERNAL_CONTENT_URI _ID 列的值,可以在铃声的URI通过这样做 ContentUris 类:

To get the Uri of a ringtone you need to combine the INTERNAL_CONTENT_URI with the _ID column value, you can do this by using ContentUris class:

Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, cursor.getLong(0));

这篇关于外部存储权限问题与MediaProvider /振铃音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆