我知道这是重复的,但我无法解决问题。
在我的 Android 应用程序中,我从一个 url 下载了一个图像,它因内存不足异常而崩溃。我在下载功能行
收到错误。Bitmap.createScaledBitmap
一切都在 doInBackground 中。
private Bitmap downloadImage(String url) {
Bitmap bitmap = null;
InputStream stream = null;
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
try {
stream = getHttpConnection(url);
bitmap = BitmapFactory.decodeStream(stream, null, bmOptions);
// add on 12/02/2014
bitmap = Bitmap.createScaledBitmap( // Out of memory exception
bitmap, (int) (bitmap.getWidth() * 0.8),
(int) (bitmap.getHeight() * 0.8), true);
// Adding given image bitmap to byte array for edit spot.
ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream1);
byte[] array = stream1.toByteArray();
AddNewSpotValues.comm_2_picture_path = array;
stream.close();
} catch (IOException e1) {
Log.e("image download problem IO", e1.getMessage());
e1.printStackTrace();
}
return bitmap;
}
// Makes HttpURLConnection and returns InputStream
private InputStream getHttpConnection(String urlString)
throws IOException {
InputStream stream = null;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
try {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setRequestMethod("GET");
httpConnection.connect();
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
stream = httpConnection.getInputStream();
}
} catch (Exception ex) {
Log.e("image download problem", ex.getMessage());
ex.printStackTrace();
}
return stream;
}
日志猫
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:718)
at android.graphics.Bitmap.createBitmap(Bitmap.java:695)
at android.graphics.Bitmap.createBitmap(Bitmap.java:628)
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:508)
at com.fssd.spot.setting.MyAccount_MySpot.downloadImage(MyAccount_MySpot.java:353)
at com.fssd.spot.setting.MyAccount_MySpot.access$6(MyAccount_MySpot.java:336)
at com.fssd.spot.setting.MyAccount_MySpot$GetDetailOfSelectedSPOT.doInBackground(MyAccount_MySpot.java:315)
at com.fssd.spot.setting.MyAccount_MySpot$GetDetailOfSelectedSPOT.doInBackground(MyAccount_MySpot.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
... 4 more
请帮我。