When doing an Android project, we often need to read a picture from a local or a network and convert it to a bitmap picture for use, and here's how to read the local picture and convert it:
Java code
/** * Gets the absolute path to the local or network bitmap URL-network or local picture, such as: * A. Network path: url= "http://blog.for
Everlove.us/girl2.png ";
* B. Local path: Url= "File://mnt/sdcard/photo/image.png"; * C. Supported picture formats, PNG, jpg,bmp,gif, etc. * * @param URL * @return/public static Bitmap Getlocalornetbitmap (St
Ring URL) {Bitmap Bitmap = null;
InputStream in = null;
Bufferedoutputstream out = null;
try {in = new Bufferedinputstream (URL). OpenStream (), constant.io_buffer_size);
Final Bytearrayoutputstream DataStream = new Bytearrayoutputstream ();
out = new Bufferedoutputstream (DataStream, constant.io_buffer_size);
Copy (in, out);
Out.flush ();
byte[] data = Datastream.tobytearray ();
Bitmap = Bitmapfactory.decodebytearray (data, 0, data.length);
data = null;
return bitmap;
catch (IOException e) {e.printstacktrace ();
return null; }
}
Description: Constant.io_buffer_size is a constant, can be changed to a constant, such as 2*1024, in fact, depending on the size of your picture, your own according to the size of the image set it.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.