Document directory
Download the image from the image address to read the content of the given image file. Use fileinputstream
public static byte[] mReaderPicture(String filePath) {byte[] arr = null;try {File file = new File(filePath);FileInputStream fReader = new FileInputStream(file);arr = new byte[1024*100];fReader.read(arr);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}return arr;}
Create a New Graph Based on the byte array.
Public static void mwriterpicture (string newfilename, byte [] B) {try {file = new file (newfilename); fileoutputstream fstream = new fileoutputstream (File); fstream. write (B); fstream. close (); system. out. println ("image created successfully" + B. length);} catch (exception e) {// todo: handle exception }}
Returns the byte []
Public static byte [] mreaderpicturetointernet (string strur1) {byte [] imgdata = NULL; Url URL; try {url = new URL (strur1); urlconnection connection = URL. openconnection (); int length = (INT) connection. getcontentlength (); inputstream is = connection. getinputstream (); If (length! =-1) {imgdata = new byte [length]; byte [] temp = new byte [500*1024]; int readlen = 0; int destpos = 0; while (readlen = is. read (temp)> 0) {system. arraycopy (temp, 0, imgdata, destpos, readlen); // arraycopy (Object SRC, int srcpos, object DEST, int destpos, int length) // copy an array from the specified source array, and copy from the specified position to the specified position of the target array to the end of destpos + = readlen ;}} return imgdata ;} catch (exception e) {// todo auto-generated catch blocke. printstacktrace ();} return imgdata ;}
Directly retrieve images from a specified URL
public static void DownPictureToInternet(String filePath,String strUr1){try {URL url = new URL(strUr1);InputStream fStream = url.openConnection().getInputStream();int b = 0;FileOutputStream fos = new FileOutputStream(new File(filePath));while ((b=fStream.read())!=-1) {fos.write(b);}fStream.close();fos.close();} catch (Exception e) {e.printStackTrace();}}
Application:
Public static void main (string [] ARGs) {mwriterpicture ("test/1.jpg", mreaderpicture (" Res/me.jpg "); mwriterpicture (" test/2.jpg ", mreaderpicturetointernet ("http://pic2.desk.chinaz.com/file/201209/7/qinghimingyue4_p.jpg"); downpicturetointernet ("test/download .jpg", "http://img3.100bt.com/upload/ttq/20130205/1360069663700.jpg ");}
Source code download: http://download.csdn.net/detail/oyuntaolianwu/5804447