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