0

我正在从 web 服务将图像显示到列表视图中,假设我正在测试应用程序 10 次,然后 6 次出现此异常,请任何人帮助我。我正在使用 AsyncTask 调用 Web 服务。

06-12 14:49:53.132: E/dalvikvm-heap(3071): Out of memory on a 1920016-byte allocation.
 06-12 14:49:53.160: E/AndroidRuntime(3071): FATAL EXCEPTION: AsyncTask #3
 06-12 14:49:53.160: E/AndroidRuntime(3071): java.lang.RuntimeException: An error  occured while executing doInBackground()
  06-12 14:49:53.160: E/AndroidRuntime(3071):   at android.os.AsyncTask$3.done(AsyncTask.java:278)
 06-12 14:49:53.160: E/AndroidRuntime(3071):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
 06-12 14:49:53.160: E/AndroidRuntime(3071):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
 06-12 14:49:53.160: E/AndroidRuntime(3071):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
 06-12 14:49:53.160: E/AndroidRuntime(3071):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
 06-12 14:49:53.160: E/AndroidRuntime(3071):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
 06-12 14:49:53.160: E/AndroidRuntime(3071):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-12 14:49:53.160: E/AndroidRuntime(3071):     at java.lang.Thread.run(Thread.java:856)
06-12 14:49:53.160: E/AndroidRuntime(3071): Caused by: java.lang.OutOfMemoryError
 06-12 14:49:53.160: E/AndroidRuntime(3071):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
 06-12 14:49:53.160: E/AndroidRuntime(3071):    at an droid.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
06-12 14:49:53.160: E/AndroidRuntime(3071):     at com.rentfaster.utilities.ImageLoader.decodeFile(ImageLoader.java:142)
06-12 14:49:53.160: E/AndroidRuntime(3071):     at com.rentfaster.utilities.ImageLoader.getBitmap(ImageLoader.java:78)
 06-12 14:49:53.160: E/AndroidRuntime(3071):    at com.rentfaster.handler.DetailPhotoHandler.imagefecher(DetailPhotoHandler.java:211)
  06-12 14:49:53.160: E/AndroidRuntime(3071):   at com.rentfaster.handler.DetailPhotoHandler.characters(DetailPhotoHandler.java:153)
 06-12 14:49:53.160: E/AndroidRuntime(3071):    at org.apache.harmony.xml.ExpatParser.text(ExpatParser.java:163)
 06-12 14:49:53.160: E/AndroidRuntime(3071):    at org.apache.harmony.xml.ExpatParser.appendBytes(Native Method)
 06-12 14:49:53.160: E/AndroidRuntime(3071):    at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:513)
  06-12 14:49:53.160: E/AndroidRuntime(3071):   at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:474)
  06-12 14:49:53.160: E/AndroidRuntime(3071):   at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:321)
  06-12 14:49:53.160: E/AndroidRuntime(3071):   at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:279)
 06-12 14:49:53.160: E/AndroidRuntime(3071):    at com.rentfaster.home.PropertyDetail$Photostask.doInBackground(PropertyDetail.java:1174)
  06-12 14:49:53.160: E/AndroidRuntime(3071):   at com.rentfaster.home.PropertyDetail$Photostask.doInBackground(PropertyDetail.java:1)
06-12 14:49:53.160: E/AndroidRuntime(3071):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
06-12 14:49:53.160: E/AndroidRuntime(3071):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-12 14:49:53.160: E/AndroidRuntime(3071):     ... 4 more
06-12 14:50:02.882: E/parse exception0(3258):  dont know why
06-12 14:50:02.882: E/XML Error(3258): java.net.MalformedURLException: Protocol not found: &area=53.447557,-113.460058,53.447557,-113.460058&max=100
06-12 14:50:02.921: E/MapActivity(3258): Couldn't get connection factory client

我仍然面临内存不足的问题,我正在使用此代码,请帮助我

//decodes image and scales it to reduce memory consumption
    private static Bitmap decodeFile(File f){
       try {
        //decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inSampleSize = 2;
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        //Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE=70;
        int width_tmp=o.outWidth, height_tmp=o.outHeight;
        int scale=1;
        while(true){
            if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                break;
            width_tmp/=2;
            height_tmp/=2;
            scale*=2;
        }

        //decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();

        o2.inSampleSize=2;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, null);
    } catch (FileNotFoundException e) {}
    return null;
}
4

4 回答 4

1

您在创建来自的位图时内存不足,您需要使用减小位图的大小

 BitmapFactory.Options option = new BitmapFactory.Options();
    option.inSampleSize = 2;


Bitmap bm = BitmapFactory.decodeFile(root.getPath() + "/" + imageName,
                            option);
                    imageView.setImageBitmap(bm);
于 2012-06-12T09:57:37.373 回答
0

问题是OutOfMemoryError
这些链接应该可以解决您的问题,
android如何处理内存不足异常
如何处理内存不足错误?
Android:Gallery
android 中的内存不足异常 - 创建位图时出现内存不足异常

于 2012-06-12T09:45:19.680 回答
0

您在解码来自某个流(网络服务?)的位图时内存不足,也许您不应该将图像作为消息交换的一部分(它将被 base64 编码并一次又一次地复制污染您宝贵的堆)提供图像,而是作为单独的请求提取二进制文件从服务器流式传输?

于 2012-06-12T09:30:55.580 回答
0

你得到一个 RuntimeException,它是由 OutOfMemoryError 引起的。OutOfMemoryErrors 通常发生在您尝试加载的图像对于 Android 分配给您的应用程序的空间而言太大时。

如果您可以发布以下行以及它们上方和下方的一些代码,以及调用方法并解释您在应用程序中所做的事情,我们可能会为您提供更好的帮助:

ImageLoader.java:142
ImageLoader.java:78
DetailPhotoHandler.java:211
DetailPhotoHandler.java:153
于 2012-06-12T09:32:46.617 回答