0

I am facing a problem while setting album arts(bitmap images) on an image view but surprisingly it only crashes on my phone and runs perfectly fine on the emulator. I don't know what the problem is as i am not able to get in crash reports. But I am pretty sure that the problem is on these lines only :-

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();
}

Because, on removing these lines makes it run perfectly..

Full method of retrieving data from the external storage is:

 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;
    }

Here is the logcat details :-

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)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Aman Chatterjee
  • 85
  • 1
  • 10
  • Your title does not match your description. Is it crashing on the phone, or the emulator? Either way, you'll need to post [the stack trace](http://stackoverflow.com/questions/23353173). – Mike M. Mar 04 '18 at 06:04
  • sry...for the wrong title. Its not working on the phone.. – Aman Chatterjee Mar 04 '18 at 06:06
  • I updated the title...And how can i get the stack trace from the phone?? – Aman Chatterjee Mar 04 '18 at 06:07
  • Hook it up over USB, installing drivers, if necessary. Then Android Studio should be able to see it, and you can get the logs there. – Mike M. Mar 04 '18 at 06:09
  • ok i will try that.. – Aman Chatterjee Mar 04 '18 at 06:32
  • I updated the logCat details ..Pease Help – Aman Chatterjee Mar 04 '18 at 06:42
  • `OutOfMemoryError` - You're running out of memory, mostly due to all the images you're loading at once. – Mike M. Mar 04 '18 at 06:45
  • So what could be the possible solution...I am trying to display all the songs along with their album arts in the recycler view... – Aman Chatterjee Mar 04 '18 at 06:54
  • Scale them down, recycle the bitmaps when they're not currently in use, etc. It's kind of a lot to handle, so using an image loading library like Glide could do most of the heavy lifting for you. With that, you wouldn't load all of the images at once. You'd want to save the path in your `Data` objects, then have Glide load them as needed. If your displaying these in a `ListView`, `RecyclerView`, etc., that would be in the `Adapter`. – Mike M. Mar 04 '18 at 07:03
  • Can it be done without using any external libraries?? – Aman Chatterjee Mar 04 '18 at 07:08
  • Sure. The `BitmapFactory` class can load down-scaled `Bitmap`s: https://developer.android.com/topic/performance/graphics/load-bitmap.html#load-bitmap. On newer versions, you can also pass it an existing, unused `Bitmap` to be reused in decoding the new one, instead of allocating all new resources for it: https://developer.android.com/topic/performance/graphics/manage-memory.html#inBitmap. The `Bitmap` class itself has the `recycle()` method to completely release resources when done, etc. You'll have to handle all these things yourself, though. – Mike M. Mar 04 '18 at 07:23
  • I now use the Glide library to display albumart. Fast and efficient – Theo Mar 05 '18 at 07:09

1 Answers1

0

I used to struggle the same way, trying to prevent OOM errors. However, using the Glide library, this takes care of everything for you.

            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;
}

add to the module build.gradle:

      implementation 'com.github.bumptech.glide:glide:4.5.0'
      annotationProcessor 'com.github.bumptech.glide:compiler:4.5.0'
Theo
  • 2,012
  • 1
  • 16
  • 29
  • Thanks man It worked , Its really so much easy to work with glide...Just one question "content://media/external/audio/albumart" what is this uri ?? – Aman Chatterjee Mar 05 '18 at 17:45
  • @Aman I guess that is where the android provider gets its albumthumbs – Theo Mar 05 '18 at 20:07