public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private class AsyncImageLoader extends AsyncTask<String, Void, Bitmap[]> {
Bitmap bitmap[];
protected void onPreExecute() {
pDialog.setMessage(getContext().getResources().getString(R.string.please_wait));
showDialog();
}
@Override
protected Bitmap[] doInBackground(String... params) {
hideDialog();
bitmap = new Bitmap[2];
bitmap[0] = getBitmapFromURL(params[0]);
bitmap[1] = getBitmapFromURL(params[1]);
return bitmap;
}
@Override
protected void onPostExecute(Bitmap[] bm) {
imgCover.setImageBitmap(bm[1]);
bm[1].recycle();
imgProfile.setImageBitmap(bm[0]);
bm[0].recycle();
if (MainActivity.PROFILE_UID.equals(MainActivity.USER_UID))
FragmentDrawer.imgProfileNavDrawer.setImageBitmap(bm[0]); // Sol drawer' da çıkan yuvarlak resmi güncellemek için
}
}
01-10 21:11:14.621 7906-8194/project.com.holobech E/AndroidRuntime:致命異常:的AsyncTask#2 了java.lang.RuntimeException:執行doInBackground()時發生錯誤java.util.concurrent上的java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 上的android.os.AsyncTask $ 3.done(AsyncTask.java:299) .FutureTask.setException(FutureTask.java:219) at java.util.concurrent.FutureTask.run(FutureTask.java:239) at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:573) 在java.lang.Thread.run(線程。的java:856) 所致:java.lang.OutOfMemoryError 在android.graphics.BitmapFactory.nativeDecodeStream(本機方法) 在android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:528) 在android.graphics.BitmapFactory。 decodeStream(BitmapFactory.java:600) at project.com.holobech.activity.ProfileFragment.getBitmapFromURL(ProfileFragment.java:508) at project.com.holobech.activity.ProfileFragment $ AsyncImageLoader.doInBackground(ProfileFragment.java:529) at project.com.holobec h.activity.ProfileFragment $ AsyncImageLoader.doInBackground(ProfileFragment.java:516) at android.os.AsyncTask $ 2.call(AsyncTask.java:287) at java.util.concurrent.FutureTask.run(FutureTask.java:234 ) at java.util.concurrent.ThreadPoolExecutor $ Worker。運行(ThreadPoolExecutor.java:573) 在java.lang.Thread.run(Thread.java:856)的AsyncTask#2了java.lang.RuntimeException
它可以完美的Android 5.0及更高版本。但是,當我嘗試使用4.2.2版本時,發生錯誤。我該如何解決這個問題? 感謝您的建議
看起來像logcat中的內存不足錯誤,這可以解釋爲什麼你在不同的設備/ Android API上得到不同的結果。 – NameSpace
我處理內存錯誤,但第一個錯誤仍然存在 –