音乐播放器请求写入外部存储权限



我正在Android中创建一个音乐播放器应用程序。它工作得很好,但每当我在歌曲列表中滚动时,它就会开始崩溃,并给出这个异常

Process: com.example.lenovo.musicplayer, PID: 31100
java.lang.SecurityException: External path: /storage/emulated/0/Android/data/com.android.providers.media/albumthumbs/1460104607336: Neither user 10294 nor current process has android.permission.WRITE_EXTERNAL_STORAGE.      
at android.os.Parcel.readException(Parcel.java:1555)      
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:190)
at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:153)
at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:691)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1170)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:994)
at android.content.ContentResolver.openInputStream(ContentResolver.java:719)
at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:1110)
at com.example.lenovo.musicplayer.Adapter.getView(Adapter.java:119)
at android.widget.AbsListView.obtainView(AbsListView.java:2571)
at android.widget.ListView.makeAndAddView(ListView.java:1894)
at android.widget.ListView.fillDown(ListView.java:710)
at android.widget.ListView.fillGap(ListView.java:674)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5900)
at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:5378)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:825)
at android.view.Choreographer.doCallbacks(Choreographer.java:619)
at android.view.Choreographer.doFrame(Choreographer.java:578)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:811)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6102)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

当我在清单文件中授予写入权限时,这个问题就会消失,但我不明白的是为什么当我没有向任何类型的存储写入任何内容时,它会请求WriteExternalStoragePermission。据我所知,音乐播放器不需要写入外部存储权限。

这是歌曲列表活动的代码

   public class ActivitySongsList extends AppCompatActivity {
public static ArrayList<MusicBean>songsList;
    ListView listView;
    Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_songs_list);
        toolbar= (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        listView= (ListView) findViewById(R.id.lv_songs);
        songsList=getAudioList();
        Adapter adapter=new Adapter(this,songsList);
        listView.setAdapter(adapter);
        getSupportActionBar().setTitle("Songs");
        toolbar.setTitleTextColor(Color.parseColor("#ffffff"));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(ActivitySongsList.this,ActivityMusicDetails.class);
                intent.putExtra("song",songsList.get(position));
                intent.putExtra("position",String.valueOf(position));
                startActivity(intent);
            }
        });
    }
    private ArrayList<MusicBean> getAudioList() {
        ArrayList<MusicBean> musicBeanArrayList=new ArrayList<>();
        try {
            final Cursor mCursor = getContentResolver().query(
                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                    new String[]{MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM_ID,MediaStore.Audio.Media.ARTIST,MediaStore.Audio.Media.TITLE}, null, null,
                    "LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
        int count = mCursor.getCount();
        int i = 0;
        if (mCursor.moveToFirst()) {
            do {
                String song=mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
                String path=mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
                long album_id=mCursor.getLong(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
                String artist=mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
                Uri sArtworkUri = Uri
                        .parse("content://media/external/audio/albumart");
                Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, album_id);
                musicBeanArrayList.add(new MusicBean(song,path,albumArtUri.toString(),String.valueOf(album_id),artist));
                /*songs[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
                mAudioPath[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));*/
                i++;
            } while (mCursor.moveToNext());
        }
        mCursor.close();
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return musicBeanArrayList;
    }
}

它崩溃了,这条线上出现了异常

bitmap = MediaStore.Images.Media.getBitmap(ctx.getContentResolver(), uri);

在这个代码中,我得到了相应mp3文件的专辑艺术。但这段代码也只是从外部存储读取,因此也不应该请求写入权限,因为我只读取位图而不是写入位图。

来自处理"/media/external/audio/albumart"的MediaProvider代码

openFile(Uri uri, String mode) 内部

try {
    pfd = openFileAndEnforcePathPermissionsHelper(newUri, mode);
} catch (FileNotFoundException ex) {
    // That didn't work, now try to get it from the specific file
    pfd = getThumb(database, db, audiopath, albumid, null);
}

因此,如果找不到文件,它将调用getThumb

getThumb调用makeThumbInternal

然后在某些条件下,它调用

writeAlbumArt(need_to_recompress, out, compressed, bm);

在最新版本中,writeAlbumArt调用

final long identity = Binder.clearCallingIdentity();

在写作之前,这应该可以解决问题。但修复是在2015年8月27日提交的,所以只有棉花糖有机会获得修复。

需要它,因为MediaStore需要它,没有READ-ONLY;MediaStore正在访问您的行为上的外部文件,所以是您在进行读取。

您可以使用folling链接https://github.com/android/platform_packages_providers_mediaprovider/blob/master/src/com/android/providers/media/MediaProvider.java是为你提供帮助。。

在清单文件中声明权限如下:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

相关内容

  • 没有找到相关文章

最新更新