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