我有 ViewPager 用于显示可缩放图像(使用ImageViewTouch)。我需要从 Internet (http) 加载大型位图。大我的意思是2000x1000。图像需要很大,因为它们是可缩放的并且需要显示细节。服务器上的图像是 .jpg 格式,但这没问题 - 我可以更改它。
我怎样才能设法将这么大的图像加载到 ImageViewTouch (ImageView) 而不会出现内存问题?
现在我只使用这个(AsyncTask):
ImageView currentView; //ImageView where to place image loaded from Internet
String ImageUrl; //URL of image to load
protected Bitmap doInBackground(ArrayList... params) {
Bitmap bitmap;
InputStream in = null;
imageUrl = (String)params[0].get(1);
try{
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(imageUrl));
in = response.getEntity().getContent();
} catch(Exception e){
e.printStackTrace();
}
try {
bitmapa = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
currentView.setImageBitmap(bitmap);
}
它会导致许多内存问题:
E/dalvikvm-heap(369): 69560740-byte external allocation too large for this process.
E/GraphicsJNI(369): VM won't let us allocate 69560740 bytes
或者
E/AndroidRuntime(369): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(369): at android.os.AsyncTask$3.done(AsyncTask.java:200)
E/AndroidRuntime(369): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
E/AndroidRuntime(369): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
E/AndroidRuntime(369): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
E/AndroidRuntime(369): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
E/AndroidRuntime(369): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
E/AndroidRuntime(369): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
E/AndroidRuntime(369): at java.lang.Thread.run(Thread.java:1019)
E/AndroidRuntime(369): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
E/AndroidRuntime(369): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
E/AndroidRuntime(369): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:470)
E/AndroidRuntime(369): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:525)
E/AndroidRuntime(369): at com.package.app.ImageDownloader.doInBackground(ImageDownloader.java:78)
E/AndroidRuntime(369): at com.package.app.ImageDownloader.doInBackground(ImageDownloader.java:1)
E/AndroidRuntime(369): at android.os.AsyncTask$2.call(AsyncTask.java:185)
E/AndroidRuntime(369): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
E/AndroidRuntime(369): ... 4 more