应用程序在模拟器上工作但不是真正的 phone,同时从存储(专辑艺术)设置位图图像

app works on the emulator but not real phone while setting bitmap images from the storage(Album arts)

我在图像视图上设置专辑封面(位图图像​​)时遇到问题,但令人惊讶的是它只在我的 phone 上崩溃,而 运行 在模拟器上完全正常。我不知道问题是什么,因为我无法获得崩溃报告。但我很确定问题仅出在这些行上:-

try {
    MediaMetadataRetriever mmr = new MediaMetadataRetriever();
    mmr.setDataSource(songPath);

    byte[] data = mmr.getEmbeddedPicture();
    bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
} catch (NullPointerException e) {
    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.round); //Converting resource image into bitmap
    e.printStackTrace();
}

因为,删除这些行会使它 运行 完美..

从外部存储中检索数据的完整方法是:

 public ArrayList<Data> GetPlaylist() {

        Bitmap bitmap=null;
        ContentResolver contentResolver = getActivity().getContentResolver();
        Uri songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Cursor songCursor = contentResolver.query(songUri, null, null, null, MediaStore.Audio.Media.TITLE + " ASC");

            if (songCursor != null && songCursor.moveToFirst()) {

                do {
                    int songId = songCursor.getInt(songCursor.getColumnIndex(MediaStore.Audio.Media._ID));
                    String songTitle = songCursor.getString(songCursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                    String songArtist = songCursor.getString(songCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                    String songPath = songCursor.getString(songCursor.getColumnIndex(MediaStore.Audio.Media.DATA));

                    try {
                        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
                        mmr.setDataSource(songPath);
                        byte[] data = mmr.getEmbeddedPicture();
                        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                    } catch (NullPointerException e) {
                        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.round); //Converting resource image into bitmap
                        e.printStackTrace();
                    }

                    songList.add(new Data(songTitle, songArtist,bitmap));
                }
                while (songCursor.moveToNext());
            }
            songCursor.close();
        return songList;
    }

这里是 logcat 详细信息 :-

03-04 12:09:39.711 30277-30277/com.dannproductions.myproject E/HAL: PATH3 /odm/lib64/hw/gralloc.qcom.so
03-04 12:09:39.711 30277-30277/com.dannproductions.myproject E/HAL: PATH2 /vendor/lib64/hw/gralloc.qcom.so
03-04 12:09:39.711 30277-30277/com.dannproductions.myproject E/HAL: PATH1 /system/lib64/hw/gralloc.qcom.so
03-04 12:09:39.711 30277-30277/com.dannproductions.myproject E/HAL: PATH3 /odm/lib64/hw/gralloc.msm8953.so
03-04 12:09:39.711 30277-30277/com.dannproductions.myproject E/HAL: PATH2 /vendor/lib64/hw/gralloc.msm8953.so
03-04 12:09:39.711 30277-30277/com.dannproductions.myproject E/HAL: PATH1 /system/lib64/hw/gralloc.msm8953.so
03-04 12:09:39.947 30277-30277/com.dannproductions.myproject E/MediaMetadataRetrieverJNI: getEmbeddedPicture: Call to getEmbeddedPicture failed.
03-04 12:09:41.652 30277-30277/com.dannproductions.myproject E/MediaMetadataRetrieverJNI: getEmbeddedPicture: Call to getEmbeddedPicture failed.
03-04 12:09:41.727 30277-30277/com.dannproductions.myproject E/MediaMetadataRetrieverJNI: getEmbeddedPicture: Call to getEmbeddedPicture failed.
03-04 12:09:41.804 30277-30277/com.dannproductions.myproject E/MediaMetadataRetrieverJNI: getEmbeddedPicture: Call to getEmbeddedPicture failed.
03-04 12:09:42.613 30277-30277/com.dannproductions.myproject E/MediaMetadataRetrieverJNI: getEmbeddedPicture: Call to getEmbeddedPicture failed.
03-04 12:09:43.794 30277-30277/com.dannproductions.myproject E/MediaMetadataRetrieverJNI: getEmbeddedPicture: Call to getEmbeddedPicture failed.
03-04 12:09:44.499 30277-30277/com.dannproductions.myproject E/MediaMetadataRetrieverJNI: getEmbeddedPicture: Call to getEmbeddedPicture failed.
03-04 12:09:44.782 30277-30277/com.dannproductions.myproject E/MediaMetadataRetrieverJNI: getEmbeddedPicture: Call to getEmbeddedPicture failed.
03-04 12:09:45.030 30277-30277/com.dannproductions.myproject E/MediaMetadataRetrieverJNI: getEmbeddedPicture: Call to getEmbeddedPicture failed.
03-04 12:09:45.534 30277-30277/com.dannproductions.myproject E/MediaMetadataRetrieverJNI: getEmbeddedPicture: Call to getEmbeddedPicture failed.
03-04 12:09:45.658 30277-30277/com.dannproductions.myproject E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: com.dannproductions.myproject, PID: 30277
                                                                               java.lang.OutOfMemoryError: Failed to allocate a 9815052 byte allocation with 4403064 free bytes and 4MB until OOM
                                                                                   at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                                                                                   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                                                   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:624)
                                                                                   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:457)
                                                                                   at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:480)
                                                                                   at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:512)
                                                                                   at com.dannproductions.myproject.FragmentMusic.GetPlaylist(FragmentMusic.java:99)
                                                                                   at com.dannproductions.myproject.FragmentMusic.onCreateView(FragmentMusic.java:50)
                                                                                   at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
                                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
                                                                                   at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
                                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
                                                                                   at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
                                                                                   at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
                                                                                   at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
                                                                                   at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
                                                                                   at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2199)
                                                                                   at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:651)
                                                                                   at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:145)
                                                                                   at android.support.v4.view.ViewPager.populate(ViewPager.java:1236)
                                                                                   at android.support.v4.view.ViewPager.populate(ViewPager.java:1084)
                                                                                   at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)
                                                                                   at android.view.View.measure(View.java:19759)
                                                                                   at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                                                                                   at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                                                                                   at android.view.View.measure(View.java:19759)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6122)
                                                                                   at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                   at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                                                                                   at android.view.View.measure(View.java:19759)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6122)
                                                                                   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                                   at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                                                                                   at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                                                                                   at android.view.View.measure(View.java:19759)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6122)
                                                                                   at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                   at android.view.View.measure(View.java:19759)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6122)
                                                                                   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                                   at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                                                                                   at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                                                                                   at android.view.View.measure(View.java:19759)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6122)
                                                                                   at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                   at com.android.internal.policy.DecorView.onMeasure(DecorView.java:690)
                                                                                   at android.view.View.measure(View.java:19759)
                                                                                   at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2313)
                                                                                   at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1400)
                                                                                   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1649)
                                                                                   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1288)
                                                                                   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6359)
03-04 12:09:45.658 30277-30277/com.dannproductions.myproject E/AndroidRuntime:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:873)
                                                                                   at android.view.Choreographer.doCallbacks(Choreographer.java:685)
                                                                                   at android.view.Choreographer.doFrame(Choreographer.java:621)
                                                                                   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:859)
                                                                                   at android.os.Handler.handleCallback(Handler.java:754)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                   at android.os.Looper.loop(Looper.java:163)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:6342)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)

我以前也是这样挣扎的,试图防止OOM错误。但是,使用 Glide 库,这会为您处理一切。

            Uri album_uri = getAlbumUri(mContext,album_id);

    if(album_uri!=null ) {
        Glide.with(mContext)
                .asBitmap()
                .load(album_uri)
                .into(holder.image);
    }


     public Uri getAlbumUri(Context mContext,String album_id){
    if(mContext!=null) {
        Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
        Uri imageUri = Uri.withAppendedPath(sArtworkUri, String.valueOf(album_id));
        return imageUri;
    }
    return null;
}

加入模块build.gradle:

      implementation 'com.github.bumptech.glide:glide:4.5.0'
      annotationProcessor 'com.github.bumptech.glide:compiler:4.5.0'