android从url加载采样位图
我试图从url高效地加载位图,但最终我得到了空的imageview。如果从资源解码,一切都正常android从url加载采样位图,android,bitmap,memory-efficient,Android,Bitmap,Memory Efficient,我试图从url高效地加载位图,但最终我得到了空的imageview。如果从资源解码,一切都正常 try { InputStream in = new java.net.URL(urldisplay).openStream(); mIcon11 = decodeSampledBitmapFromStream(in, 200, 200); } protected void onPostExecute(Bitmap result) { bmImage.setImageBitma
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = decodeSampledBitmapFromStream(in, 200, 200);
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
它是在旧版本的Android中复制的,比如2.1?不,我现在正在使用4.1.2 emulator。无论如何,请尝试用另一个my Response:的流包装您的InputStream。它可以帮助处理不总是加载的位图。
public static Bitmap decodeSampledBitmapFromStream(InputStream in, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(in, null, options);
}