External Storage Permission Issue with MediaProvider / Ring Tones

前端 未结 3 1472
北恋
北恋 2021-01-03 04:35

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)

j         


        
3条回答
  •  感动是毒
    2021-01-03 04:47

    You can find the external storage directory without needing WRITE_EXTERNAL_STORAGE or READ_EXTERNAL_STORAGE by using Environment.getExternalStorageDirectory().

    You can then compare the paths for the URIs provided by RingtoneManager to this path to see if they are on the external storage or not and if so add those items to a List.

    Then, rather than passing the raw Cursor to the UI you can use that List with a ListAdapter instead.

    For example (untested, you may need to change the method of comparing paths):

    class RingtoneDetails
    {
        public String ID;
        public String Title;
        public Uri Uri;
    
        public RingtoneDetails(String id, String title, Uri uri)
        {
            ID = id;
            Title = title;
            Uri = uri;
        }
    }
    
    private List getNonExternalRingtones(RingtoneManager manager)
    {
        List ringtones = new List();
        Cursor cursor = manager.getCursor();
        String extDir = Environment.getExternalStorageDirectory().getAbsolutePath();
    
        while (cursor.moveToNext()) 
        {
            String id = cursor.getString(cursor.getColumnIndex(RingtoneManager.ID_COLUMN_INDEX));
            String title = cursor.getString(cursor.getColumnIndex(RingtoneManager.TITLE_COLUMN_INDEX));
            Uri uri= cursor.getString(cursor.getColumnIndex(RingtoneManager.URI_COLUMN_INDEX));
    
            if(!uri.getPath().contains(extDir))
            {
                ringtones.add(new Ringtone(id, title, uri));
            }
        }
    
        return ringtones;
    }
    

提交回复
热议问题