جافا عنوان URL الخاص بك
معلومات المنتج:
الصورة النقطية العامة الثابتة LoadImageFromUrl(String url, int sc) {
عنوان URL م؛
InputStream i = null;
BufferedInputStream bis = null;
ByteArrayOutputStream out = null;
بايت isBuffer[] = بايت جديد[1024];
إذا (url == فارغ)
عودة فارغة؛
يحاول {
م = عنوان URL الجديد(url);
i = (InputStream) m.getContent();
bis = new BufferedInputStream(i, 1024 * 4);
out = new ByteArrayOutputStream();
إنت لين = 0;
بينما ((len = bis.read(isBuffer)) != -1) {
out.write(isBuffer, 0, len);
}
out.Close();
مكرر.إغلاق();
} التقاط (MalformedURLException e1) {
e1.printStackTrace();
عودة فارغة؛
} قبض (IOException ه) {
printStackTrace();
}
إذا (خارج == فارغة)
عودة فارغة؛
byte[] data = out.toByteArray();
خيارات BitmapFactory.Options = BitmapFactory.Options() الجديدة؛
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, options);
options.inJustDecodeBounds = false;
int be = (int) (options.outHeight / (float) sc);
إذا (يكون <= 0) {
يكون = 1؛
} وإلا إذا (يكون > 3) {
يكون = 3؛
}
options.inSampleSize = be;
الصورة النقطية bmp = null;
يحاول {
bmp = BitmapFactory.decodeByteArray(data, 0, data.length, options); // 返回缩略图
} التقاط (OutOfMemoryError ه) {
// TODO: التعامل مع الاستثناء
System.gc();
bmp = null;
}
عودة بمب؛
}