1

I want to implement a file explorer which allow to browse images and videos from DCIM/CAMERA directory inside sdcard

to do that I have used this code here

it is useful

but it doesn't match exactly to my needs then I have made some modifications and the important one is in the getbitmap method :

    @SuppressLint("NewApi")
    private Bitmap getBitmap(String url) 
    {
        File f=fileCache.getFile(url);

        //from SD cache
        Bitmap b = decodeFile(f);
        if(b!=null)
            return b;

        //from web
        try {
            Bitmap bitmap=null;
//            URL imageUrl = new URL(url);
//            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
//            conn.setConnectTimeout(30000);
//            conn.setReadTimeout(30000);
//            conn.setInstanceFollowRedirects(true);
//            InputStream is=conn.getInputStream();
//            OutputStream os = new FileOutputStream(f);
//            Utils.CopyStream(is, os);
//            os.close();
//            conn.disconnect();
            if(url.endsWith(".mp4")   || url.endsWith(".3gp") 
                    || url.endsWith(".MP4") || url.endsWith(".3GP"))
                bitmap = ThumbnailUtils.createVideoThumbnail(url,
                        MediaStore.Images.Thumbnails.MINI_KIND);
            else{
                f = new File(url);
                bitmap = decodeFile(f);
            }

            return bitmap;
        } catch (Throwable ex){
           ex.printStackTrace();
           if(ex instanceof OutOfMemoryError)
               memoryCache.clear();
           return null;
        }


    }

this code works well but only when I have a lot of videos in the sdcard and I scroll quickly then I have this error :

   12-12 16:36:27.633: E/dalvikvm-heap(3577): Out of memory on a 4147216-byte allocation.
12-12 16:36:27.633: I/dalvikvm(3577): "pool-2-thread-2" prio=5 tid=16 RUNNABLE
12-12 16:36:27.633: I/dalvikvm(3577):   | group="main" sCount=0 dsCount=0 obj=0x4222c220 self=0x4f65e6c8
12-12 16:36:27.633: I/dalvikvm(3577):   | sysTid=3603 nice=0 sched=0/0 cgrp=apps handle=1297195824
12-12 16:36:27.633: I/dalvikvm(3577):   | schedstat=( 0 0 0 ) utm=116 stm=15 core=0
12-12 16:36:27.633: I/dalvikvm(3577):   at android.graphics.Bitmap.nativeCreate(Native Method)
12-12 16:36:27.633: I/dalvikvm(3577):   at android.graphics.Bitmap.createBitmap(Bitmap.java:669)
12-12 16:36:27.633: I/dalvikvm(3577):   at android.graphics.Bitmap.createBitmap(Bitmap.java:649)
12-12 16:36:27.633: I/dalvikvm(3577):   at android.media.MediaMetadataRetriever._getFrameAtTime(Native Method)
12-12 16:36:27.633: I/dalvikvm(3577):   at android.media.MediaMetadataRetriever.getFrameAtTime(MediaMetadataRetriever.java:243)
12-12 16:36:27.633: I/dalvikvm(3577):   at android.media.MediaMetadataRetriever.getFrameAtTime(MediaMetadataRetriever.java:267)
12-12 16:36:27.633: I/dalvikvm(3577):   at android.media.ThumbnailUtils.createVideoThumbnail(ThumbnailUtils.java:163)
12-12 16:36:27.633: I/dalvikvm(3577):   at com.mem.view.file_explorer.ImageLoader.getBitmap(ImageLoader.java:82)
12-12 16:36:27.633: I/dalvikvm(3577):   at com.mem.view.file_explorer.ImageLoader.access$0(ImageLoader.java:58)
12-12 16:36:27.633: I/dalvikvm(3577):   at com.mem.view.file_explorer.ImageLoader$PhotosLoader.run(ImageLoader.java:157)
12-12 16:36:27.633: I/dalvikvm(3577):   at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
12-12 16:36:27.633: I/dalvikvm(3577):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-12 16:36:27.633: I/dalvikvm(3577):   at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-12 16:36:27.643: I/dalvikvm(3577):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-12 16:36:27.643: I/dalvikvm(3577):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-12 16:36:27.643: I/dalvikvm(3577):   at java.lang.Thread.run(Thread.java:856)

this error point on this line of code :

bitmap = ThumbnailUtils.createVideoThumbnail(url,
                            MediaStore.Images.Thumbnails.MINI_KIND);

how can I fix this issue

thank you in advance

Community
  • 1
  • 1
begiPass
  • 2,124
  • 6
  • 36
  • 57
  • 1
    Might be your bitmaps are not being garbage collected, so get OOM. Start DDSM/monitor and check your heap memory usage as you scroll. I had a look at the MemoryCache class, it looks like it's not thread safe, since it is not synchronizing on the map collection that it uses for the caching. Try another library out there, like https://github.com/nostra13/Android-Universal-Image-Loader which is used in lot of projects. Otherwise, share more code on how you are actually loading images through the library. You might want to try synchronizing the cache and see what happens. – Sun Dec 16 '13 at 16:40
  • thank you Sun, I have tested universal image loader libray and modify its code source to get frames from videos and now it works – begiPass Dec 18 '13 at 09:17
  • Glad it worked for you, I'm leaving an answer here, just accept it if its fine with you. – Sun Dec 18 '13 at 15:15

1 Answers1

0

Might be your bitmaps are not being garbage collected, so get OOM. Start DDSM/monitor and check your heap memory usage as you scroll. I had a look at the MemoryCache class, it looks like it's not thread safe, since it is not synchronizing on the map collection that it uses for the caching.

Try another library out there, like universal image loader which is used in lot of projects. Otherwise, share more code on how you are actually loading images through the library. You might want to try synchronizing the cache and see what happens.

Sun
  • 2,658
  • 6
  • 28
  • 33