When doing Android projects, we often need to read pictures from local or network and convert them to Bitmap pictures for use. Here is how to read local pictures and convert them:
Java code
/** * Get the bitmap url locally or on the network - the absolute path to the network or local image, for example: * * A. Network path: url=http://blog.foreverlove.us/girl2.png; * * B. Local path: url=file://mnt/sdcard/photo/image.png; * * C. Supported image formats, png, jpg, bmp, gif, etc.* * @param url * @return */ public static Bitmap GetLocalOrNetBitmap(String url) { Bitmap bitmap = null; InputStream in = null; BufferedOutputStream out = null; try { in = new BufferedInputStream(new URL(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; } }Note: Constant.IO_BUFFER_SIZE is just a constant, which can be changed to a constant, such as 2*1024, which actually depends on your image size. You can set it yourself according to the size of the image.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.