Java crawls and generates thumbnails based on URLs
Copy Code code as follows:
public static Bitmap loadimagefromurl (String url, int sc) {
URL m;
InputStream i = null;
Bufferedinputstream bis = null;
Bytearrayoutputstream out = null;
byte isbuffer[] = new byte[1024];
if (url = null)
return null;
try {
m = new URL (URL);
i = (InputStream) m.getcontent ();
bis = new Bufferedinputstream (i, 1024 * 4);
out = new Bytearrayoutputstream ();
int len = 0;
while (len = Bis.read (isbuffer))!=-1) {
Out.write (isbuffer, 0, Len);
}
Out.close ();
Bis.close ();
catch (Malformedurlexception E1) {
E1.printstacktrace ();
return null;
catch (IOException e) {
E.printstacktrace ();
}
if (out = = null)
return null;
byte[] data = Out.tobytearray ();
Bitmapfactory.options Options = new Bitmapfactory.options ();
Options.injustdecodebounds = true;
Bitmapfactory.decodebytearray (data, 0, data.length, options);
Options.injustdecodebounds = false;
int be = (int) (Options.outheight/(float) SC);
if (be <= 0) {
be = 1;
else if (be > 3) {
be = 3;
}
Options.insamplesize = be;
Bitmap bmp = null;
try {
BMP = Bitmapfactory.decodebytearray (data, 0, data.length, options); return thumbnail image
catch (OutOfMemoryError e) {
Todo:handle exception
System.GC ();
BMP = NULL;
}
return BMP;
}