2013年7月21日星期日

Android loading one hundred picture, the list shows, how to ensure not OOM?

Pictures from the network, the size is 1024 * 768 Big Picture, requiring slide show, how to do not OOM?
large cattle have any good suggestions?
------ Solution ---------------------------------------- ----
show thumbnails bar, then eat a big map memory oh.
------ Solution ---------------------------------------- ----
judgment slip event, the slide does not load images

timely recovery

can only be the case. .
------ Solution ---------------------------------------- ----
only this
------ Solution ----------------------------- ---------------
I have come to listen to opinions.
------ Solution ---------------------------------------- ----
fact, nothing of wisdom, then the large size of the picture, to look at the resolution size of memory, you can consider in the final device RAM 2GB or 4GB, a picture memory occupancy rate Resolution length x width x4 calculations, you will understand. Recovery of memory with weak references allow the JVM manages it.
------ Solution ---------------------------------------- ----
hey. . Forum a long time did not come.
this problem. Actually very simple.
3-steps
1: soft references. Consumption of it, I said this basic futile. Is to prevent hang up, or can not see the picture.
2: I put the code.
 
public static Bitmap loadImageFromUrl (String url, int sc) { URL m; InputStream i = null; BufferedInputStream bis = null; ByteArrayOutputStream out = null; byte isBuffer [] = new byte [1024]; if (url == null)
return null; try { m = new URL (url); i = (InputStream) m.getContent ();
bis = new BufferedInputStream (i, 1024 * 4); out = new ByteArrayOutputStream (); int len ​​= 0; while ((len = bis.read (isBuffer))! = -1) { out.write (isBuffer, 0, len); }
out.close (); bis.close (); } Catch (MalformedURLException e1) { e1.printStackTrace (); return null; } Catch (IOException e) { e.printStackTrace (); }
if (out == null)
return null; byte [] data = out.toByteArray (); BitmapFactory.Options options = new BitmapFactory.Options (); options.inJustDecodeBounds = true; BitmapFactory.decodeByteArray (data, 0, data.length, options); options.inJustDecodeBounds = false; int be = (int) (options.outHeight / (float) sc); if (be <= 0) { be = 1; } Else if (be> 3) { be = 3; }
options.inSampleSize = be; Bitmap bmp = null; try { bmp = BitmapFactory.decodeByteArray (data, 0, data.length, options); / / Return to the thumbnail
} Catch (OutOfMemoryError e) { / / TODO: handle exception
System.gc (); bmp = null; }
return bmp; }



3. violence set the memory space
private final static float TARGET_HEAP_UTILIZATION = 0.75f;
private final static int CWJ_HEAP_SIZE = 16 * 1024 * 1024;
VMRuntime.getRuntime ()
. setTargetHeapUtilization (TARGET_HEAP_UTILIZATION);
VMRuntime.getRuntime (). setMinimumHeapSize (CWJ_HEAP_SIZE);

------ Solution ------------------------------------ --------



The brother said good
------ Solution ------------------------------- -------------
can consider, downloaded to the local file system cache to make the case. Just need to periodically clean the disk.
------ Solution ---------------------------------------- ----

I feel even downloaded to the local load time will be the same OOM error? This I still do not know how to solve, I have encountered this problem. My pictures even less, just ten sheets. But all at once the program needs to generate bitmap, then there mistake. I have not run out of a recycled one method. But do not know why there is an error in the simulator above, in my phone no errors. android points are not all the 8M memory? Do not understand.
------ Solution ---------------------------------------- ----
page loading, first load the interface diagram can be displayed, timely recovery is no longer displayed after browsing pictures can only be the case, there is no good way to go.
------ Solution ---------------------------------------- ----
"guaranteed" can only be used in still life more suitable, Android compatibility is a large problem.
------ For reference only -------------------------------------- -

abbreviations can be used, but can not shrink too much, I am now a narrowing of the general, that is the time to set options for inSampleSize decodeStream is at least 2, if the screen is 320 * 480 when he was 3, But the simulator is still there OOM. . .
------ For reference only -------------------------------------- -

this good, copying collection.
First, soft references. Also has been used before, it should be in memory when they were jvm compulsory recycling is not it. Obviously there are a lot of memory later discovered, is far from the memory situation, bitmap or be recycled, look after some bloggers say Android 2.2, the soft and weak references, as long as there is no gc got. So nothing really feeling the effect, often slippery slide forward went forward network load images
The second point, the core is a compressed image, reducing the quality of the picture. Finally the try catch is to force, ha ha
The third point, have not tried, do not know what consequences there! !
------ For reference only -------------------------------------- -
  This reply was moderator deleted at 2012-10-01 09:13:51
------ For reference only ---------------------------------- -----
  This reply was moderator deleted at 2012-10-01 09:13:49
------ For reference only ---------------------------------- -----
sorry to be late to find a job knot stickers, thank large cattle way! !
------ For reference only -------------------------------------- -
seeking skia.os, compiler pass Yeah, seeking god blessing. . . .
------ For reference only -------------------------------------- -
landlord not solve the problem?

没有评论:

发表评论