我一直在努力改變從服務器上下載的位圖的大小,因爲我知道這是我收到的OOM錯誤的問題。我已經嘗試了下面的其他例子,以及https://developer.android.com/training/displaying-bitmaps/load-bitmap.html,但我無法做出正面或反面的關於如何使用它以及在哪裏使用它。我想將我的位圖縮放到屏幕分辨率,但無法縫製出來。感謝您的幫助。在android中更改位圖的大小以避免OOM
也不是說這是在AsyncTask
,如果這是有所作爲的話。
這裏是我的設置位圖代碼:
public class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {
BitmapFactory.Options options = new BitmapFactory.Options();
private final WeakReference<ImageView> imageViewReference;
Resources resources;
public ImageDownloaderTask(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
@Override
protected Bitmap doInBackground(String... params)
{
return downloadBitmap(params[0]);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
Log.d("HTTPS No go", bitmap.toString());
}
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
if (bitmap != null) {
//Scale the bitmap to a smaller size
imageView.setImageBitmap(bitmap);
} else {
Log.d("Downloading the image: ", "No Image found");
}
}
}
}
//URL connection to download the image
private Bitmap downloadBitmap(String url) {
HttpURLConnection urlConnection = null;
try{
URL uri = new URL(url);
urlConnection = (HttpURLConnection) uri.openConnection();
urlConnection.setRequestMethod("GET");
int statusCode = urlConnection.getResponseCode();
//check if the HTTP status code is equal to 200, which means that it is ok
if (statusCode != 200) {
return null;
}
InputStream inputStream = urlConnection.getInputStream();
if (inputStream != null) {
/*
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;
int sampleSize = calculateInSampleSize(options, imageWidth, imageHeight);
*/
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
}catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} {}
return null;
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height/2;
final int halfWidth = width/2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight/inSampleSize) >= reqHeight
&& (halfWidth/inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
}
UPDATE:
這個固定了一下。它允許我僅註銷一次,但是當我第二次註銷時,它會與舊的OOM錯誤一起崩潰。
InputStream inputStream = urlConnection.getInputStream();
if (inputStream != null) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=1; //try to decrease decoded image
Bitmap bitmap = BitmapFactory.decodeStream(inputStream , null, options);
return bitmap;
}
}catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} {}
你可以發佈你所有的'AsyncTask'代碼嗎? –
添加@ L.Swifter – MNM
@MNM您想調整大小與固定大小成比例,如128 * 128的圖像。 –