IO important understanding and notes

Source: Internet
Author: User
Part 1: display the network image to the ImageView Control 1. Use the image path to obtain the public static byte [] getImage (String path) byte array of the image) throws Exception {URL imageUrl = new URL (path); HttpURLConnection connection = (HttpURLConnection) imageUrl. openConnection (); connection. setConnectTimeout (5000); connection. setRequestMethod ("POST"); if (connection. getResponseCode () == 200) {InputStream inputStream = connection. getInputStream (); byte [] imageData = GetResour Ce. readResource (inputStream); return imageData;} return null;} analysis: (1) Get HttpURLConnection connection through URL so that the resource can establish a connection and set the connection attribute value (2) use HttpURLConnection connection to obtain the input stream. you can think like this: at this time, the image has been saved to the input stream inputStream (3) and the image data in the input stream is output to the byte array. that is, byte [] imageData = GetResource. readResource (inputStream ). the following readResource (inputStream) method is as follows: public static byte [] readResource (InputStream inputStream) throws Exception {ByteArrayOutputStream outputStream = new ByteArrayOutputStream (); byte [] buffer = new byte [1024]; int len = 0; while (len = inputStream. read (buffer ))! =-1) {outputStream. write (buffer, 0, len);} inputStream. close (); outputStream. close (); return outputStream. toByteArray ();} analysis: (1) if there is no method in the input stream, you can directly convert the data in the input stream to a string or byte array at a time !!!!!!! Idea: To extract the data in the input stream, you must first read the data (that is, the commonly used read () series of methods) each time when the array is full, an output stream writes the data into the output stream (commonly used writer () methods !!! The array here is equivalent to an item Transfer Station, playing a buffer. (2) When all the data is transferred to the input stream outputStream, you can convert all the data in the output stream to a byte array, that is, outputStream. toByteArray (); (3) In this example, it demonstrates the use of the input stream and output stream. in the corresponding API of the input stream, the data in the input stream is read to an array, or only one byte is read, or a method in a row such as FileInputStream is read: public int read (byte [] B, int off, int len) reads data of up to len bytes from the input stream into one byte array public int read () read a Data byte from the input stream. For example, in the BufferedReader class, the public String readLine () method reads a text line. returned value: the string containing the row content writes data in the byte array to the output stream in the corresponding API of the output stream, Or write data at a certain position in the array to the output stream, such as the ByteArrayOutputStream class method: public void write (byte [] B, int off, int len) write len bytes starting from offset off in the specified byte array to this byte array output stream public void write (int B) write the specified byte to the output stream of this byte array. Then we can find that: (1) data in the output stream can be converted to a byte array, such as ByteArrayOutputStream: public byte [] toByteArray (): Creates a new byte array. The size is the current size of the output stream, and the valid content of the buffer has been copied to the array. (2) You can convert the data in the output stream to a String, for example, in the ByteArrayOutputStream method: public String toString (): Convert the content of the buffer to a String, converts bytes to characters based on the default character encoding of the platform. 2. Generate a bitmap and use ImageView to display byte [] imageData = GetImageResource. getImage (path); Bitmap bitmap = BitmapFactory. decodeByteArray (imageData, 0, imageData. length); imageView. setImageBitmap (bitmap); Part 2: copy a local photo package cn. io; // FileInputStream and FileOutputStream are used to copy photos. // note that images cannot be copied using a forward stream (such as FileReader and FileWriter, because it will go to the tables Table // use the available () method in InputStream in method 2 to create a buffer // the advantage of method 2 is that you do not need to perform cyclic operations and store all the data in an array first, then retrieve all the data and save it to the destination import java. io. *; publ Ic class CopyPhoto {public static void main (String [] args) throws Exception {FileInputStream FD = null; FileOutputStream fos = null; //................................................ The following describes how to use the method 1: FS = new FileInputStream ("F: \ 1.JPG"); fos = new FileOutputStream (" F: \ 2.JPG "); byte [] temp1 = new byte [1024*1024]; int length = 0; while (length = Fi. read (temp1 ))! =-1) {fos. write (temp1, 0, length); fos. flush ();}//................................................ The following are the methods used: (2) FS = new FileInputStream ("F: \ 3.JPG"); fos = new FileOutputStream (" F: \ 4.JPG"); byte [] temp2 = new byte [FCM. available ()]; FCM. read (temp2); fos. write (temp2); Sox. close (); fos. close ();}}

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.