Usage of android to retrieve network images

Source: Internet
Author: User

For android, the main function of obtaining network images is to read the data stream of network images into the memory and then use
1. Bitmap bitMap = BitmapFactory. decodeByteArray (data, 0, length );
To spread the image to the bitmap type.
1. imageView. setImageBitmap (bitMap );
To convert
When bitmap is obtained, null occurs.
Error code:
Byte [] data = GetImageForNet. getImage (path );
Int length = data. length;
Bitmap bitMap = BitmapFactory. decodeByteArray (data, 0, length );
ImageView. setImageBitmap (bitMap );
The following is a list of code for the GetImageForNet. getImage () method.
Public static byte [] getImage (String path) throws Exception {
URL url = new URL (path );
HttpURLConnection httpURLconnection = (HttpURLConnection) url. openConnection ();
HttpURLconnection. setRequestMethod ("GET ");
HttpURLconnection. setReadTimeout (6*1000 );
InputStream in = null;
Byte [] B = new byte [1024];
Int len =-1;
If (httpURLconnection. getResponseCode () = 200 ){
In = httpURLconnection. getInputStream ();
In. read (B );
In. close ();
Return B;
}
Return null;
}
It seems that there is no problem to get the network image input stream, fill in the binary array, return the binary array, and then use Bitmap bitMap = BitmapFactory. decodeByteArray (data, 0, length); data is the returned binary array
It seems no problem to get bitMap, but bitMap is null!
The data required by the BitmapFactory. decodeByteArray method is not necessarily a byte array in the traditional sense. Check the android api and find that the data byte array required by BitmapFactory. decodeByteArray is not the expected array! Instead, the input stream is transferred to the byte array format of the byte memory output stream.
Correct code:
Try {
Byte [] data = GetImageForNet. getImage (path );
String d = new String (data );
// File file = new File ("1.jpg ");
// OutputStream out = new FileOutputStream (file );
// Out. write (data );
// Out. close ();
Int length = data. length;
Bitmap bitMap = BitmapFactory. decodeByteArray (data, 0, length );
ImageView. setImageBitmap (bitMap );
// ImageView. seti
} Catch (Exception e ){
Log. I (TAG, e. toString ());
Toast. makeText (DataActivity. this, "failed to get image", 1). show ();
}
The following is the code list of the improved GetImageForNet. getImage () method.
Public static byte [] getImage (String path) throws Exception {
URL url = new URL (path );
HttpURLConnection httpURLconnection = (HttpURLConnection) url. openConnection ();
HttpURLconnection. setRequestMethod ("GET ");
HttpURLconnection. setReadTimeout (6*1000 );
InputStream in = null;
Byte [] B = new byte [1024];
Int len =-1;
If (httpURLconnection. getResponseCode () = 200 ){
In = httpURLconnection. getInputStream ();
Byte [] result = readStream (in );
In. close ();
Return result;

}
Return null;
}

Public static byte [] readStream (InputStream in) throws Exception {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
Byte [] buffer = new byte [1024];
Int len =-1;
While (len = in. read (buffer ))! =-1 ){
OutputStream. write (buffer, 0, len );
}
OutputStream. close ();
In. close ();
Return outputStream. toByteArray ();
}

 


From qingli518

Related Article

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.