java pour l'url
复制代码代码如下 :
public static Bitmap loadImageFromUrl (String url, int sc) {
URLm ;
InputStream i = nul ;
BufferedInputStream bis = null ;
ByteArrayOutputStream out = null ;
octet isBuffer[] = nouvel octet[1024];
si (url == nul)
renvoie null ;
essayer {
m = nouvelle URL(url);
je = (InputStream) m.getContent();
bis = nouveau BufferedInputStream(i, 1024 * 4);
out = nouveau ByteArrayOutputStream();
int longueur = 0 ;
while ((len = bis.read(isBuffer)) != -1) {
out.write(isBuffer, 0, len);
}
out.close();
bis.close();
} catch (MalformedURLException e1) {
e1.printStackTrace();
renvoie null ;
} catch (IOException e) {
e.printStackTrace();
}
si (sortie == nul)
renvoie null ;
byte[] data = out.toByteArray();
Options BitmapFactory.Options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, options);
options.inJustDecodeBounds = false;
int be = (int) (options.outHeight / (float) sc);
si (être <= 0) {
être = 1 ;
} sinon si (être > 3) {
être = 3;
}
options.inSampleSize = être;
Bitmap bmp = nul ;
essayer {
bmp = BitmapFactory.decodeByteArray(data, 0, data.length, options); // 返回缩略图
} catch (OutOfMemoryError e) {
// TODO : gérer l'exception
Système.gc();
bmp = nul ;
}
retourner bmp ;
}