[Turn]android to read local picture appears OutOfMemoryException

Source: Internet
Author: User

The previous days have been for the picture memory overflow problem, check n more information, Google completely turned over all did not find a solution, when I was almost desperate when accidentally found a netizen of a tool class, holding the last hint of hope will code co come over to try a, the result makes me overjoyed. Hey, it's settled! I do not say how happy, listen to my slow way to the cause and consequences of it!

Requirements: Download the picture down, display in space and support offline viewing

The first version of the code:

Read pictures from local
Public Bitmap GETBITMAPFROMSD (String filename) {
FileInputStream fi = null;
Bufferedinputstream bi = null;
Bitmap BP = null;
try {
fi = new FileInputStream (filename);
bi = new Bufferedinputstream (FI);
bp = Bitmapfactory.decodestream (BI);
} catch (IOException e) {
bp = null;
} finally {
try {
if (bi! = null) {
Bi.close ();
}
if (fi! = null) {
Fi.close ();
}
} catch (IOException e) {
bp = null;
}
}
return BP;
}
The problem arises, because the displayed picture is too large, so there will be outofmemoryexception. I think I can catch the exception to retrieve the picture and reload, so I want to find a solution from the Internet, what manual intervention GC, what will the picture weaken what use weak references to save the picture, some summed up particularly good (http://mzh3344258.blog.51cto.com/1823534 /804237), these methods I one by one try but the problem is still unresolved. Constantly oom, constantly try to recycle, error is not appear, but once the memory is too much will not show the picture, the default picture appears. Finally, I found the following tools from the Internet, help me to solve this problem, the specific URL forgot (thanks to the Netizen (*^__^*)), now the code is posted so that the next time to pick up

Public final class Bitmaputil {

Private static final Size zero_size = new Size (0, 0);
Private static final Options options_get_size = new options ();
Private static final Options Options_decode = new options ();
private static final byte[] LOCKED = new Byte[0];

This object is used to keep the bitmap collection order, ensuring that the last image used is recycled
private static final LinkedList cache_entries = new LinkedList ();

Thread requests a queue to create a picture
private static final Queue Task_queue = new LinkedList ();

Saves the key of the picture being processed in the queue, effectively preventing duplicate additions to the request creation queue

private static final Set Task_queue_index = new HashSet ();

Cache bitmap
private static final Map Img_cache_index = new HashMap (); Through the picture path, the picture size

private static int cache_size = 20; Number of cached pictures

static {
Options_get_size.injustdecodebounds = true;
Initialize create picture thread and wait for processing
New Thread () {
{
Setdaemon (TRUE);
}

public void Run () {
while (true) {
Synchronized (task_queue) {
if (Task_queue.isempty ()) {
try {
Task_queue.wait ();
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
Queueentry entry = Task_queue.poll ();
String key = CreateKey (Entry.path, Entry.width,
Entry.height);
Task_queue_index.remove (key);
CreateBitmap (Entry.path, Entry.width, entry.height);
}
}
}.start ();

}


public static Bitmap Getbitmap (String path, int width, int height) {
if (path==null) {
return null;
}
Bitmap Bitmap = null;
try {
if (Cache_entries.size () >= cache_size) {
Destorylast ();
}
BitMap = Usebitmap (path, width, height);
if (BitMap! = null &&!bitmap.isrecycled ()) {
return BITMAP;
}
BitMap = CreateBitmap (path, width, height);
String key = CreateKey (path, width, height);
Synchronized (LOCKED) {
Img_cache_index.put (key, BITMAP);
Cache_entries.addfirst (key);
}
} catch (OutOfMemoryError err) {
Destorylast ();
System.out.println (cache_size);
return CreateBitmap (path, width, height);
}
return BITMAP;
}


public static Size Getbitmapsize (String path) {
File File = new file (path);
if (file.exists ()) {
InputStream in = null;
try {
in = new FileInputStream (file);
Bitmapfactory.decodestream (in, null, options_get_size);
return new Size (Options_get_size.outwidth,
Options_get_size.outheight);
} catch (FileNotFoundException e) {
return zero_size;
} finally {
Closeinputstream (in);
}
}
return zero_size;
}

------------------------------------------------------------------Private Methods
Add a picture to the queue header
private static Bitmap Usebitmap (String path, int width, int height) {
Bitmap Bitmap = null;
String key = CreateKey (path, width, height);
Synchronized (LOCKED) {
BitMap = Img_cache_index.get (key);
if (null! = BitMap) {
if (Cache_entries.remove (key)) {
Cache_entries.addfirst (key);
}
}
}
return BITMAP;
}

Recycle the Last picture
private static void Destorylast () {
Synchronized (LOCKED) {
String key = Cache_entries.removelast ();
if (key.length () > 0) {
Bitmap Bitmap = Img_cache_index.remove (key);
if (BitMap! = null &&!bitmap.isrecycled ()) {
Bitmap.recycle ();
BitMap = null;
}
}
}
}

Create key
private static string CreateKey (string path, int width, int height) {
if (null = = Path | | path.length () = = 0) {
Return "";
}
Return path + "_" + Width + "_" + height;
}

Create a Bitmap object from a picture path, width height
private static Bitmap CreateBitmap (String path, int width, int height) {
File File = new file (path);
if (file.exists ()) {
InputStream in = null;
try {
in = new FileInputStream (file);
Size size = getbitmapsize (path);
if (Size.equals (zero_size)) {
return null;
}
int scale = 1;
int a = Size.getwidth ()/width;
int b = size.getheight ()/height;
Scale = Math.max (A, b);
Synchronized (Options_decode) {
Options_decode.insamplesize = scale;
Bitmap Bitmap = Bitmapfactory.decodestream (in, NULL,
Options_decode);
return BITMAP;
}
} catch (FileNotFoundException e) {
LOG.V ("Bitmaputil", "createbitmap==" +e.tostring ());
} finally {
Closeinputstream (in);
}
}
return null;
}

Close the input stream
private static void Closeinputstream (InputStream in) {
if (null! = IN) {
try {
In.close ();
} catch (IOException e) {
LOG.V ("Bitmaputil", "closeinputstream==" +e.tostring ());
}
}
}

Picture size
Static Class Size {
private int width, height;

Size (int width, int height) {
This.width = width;
This.height = height;
}

public int getwidth () {
return width;
}

public int getheight () {
return height;
}
}

Queue Cache Parameter Object
Static Class Queueentry {
public String Path;
public int width;
public int height;
}
}

I only called the Getbitmap method when I used it, I passed the height width I needed to set and the local picture path, so I could automatically return bitmap to me, And when the capture to the oomerror of the LinkedList last picture is the first saved image to overflow and recovery is done, especially attention is here to catch the error exception is not obtained, Be sure to manually capture OutOfMemoryError you can handle (estimate these reasons everyone knows, so do not repeat, children's shoes refueling!) Method is always more than difficult O (∩_∩) o)

[Turn]android to read local picture appears OutOfMemoryException

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.