doInBackground和ListFragment中的错误

时间:2014-04-03 08:45:46

标签: android

我有一个适配器,ListView和数据。我从屏幕上显示的网站加载数据。 滚动并在我到达新数据结束时加载。一切顺利。但是当我向上滚动尝试加载图像错误输出时。并且总是不同。

protected String doInBackground(String... arg) {
    if (arg[1].equals("video")) {
                requestCode = 2;
                Document doc;
                try {
                    String text = "total: "+Runtime.getRuntime().totalMemory()+"\n"+
                            "freeMemory: "+Runtime.getRuntime().freeMemory()+"\n"+
                            "max: "+Runtime.getRuntime().maxMemory();

                    Log.d("=========", text);
                    linkVideo = null;
                    doc = Jsoup.connect(arg[0]).get();
                    title = doc.select("a[href]");
                    for (Element titles : title) {
                        if (titles.outerHtml().contains("Скачать (Загрузок:")) {
                            linkImage=titles.attr("abs:href");
                            Picasso p = Picasso.with(MyActivity.context);
                            linkVideo = p.load(linkImage).get();
                            Log.d("HZ",linkVideo.toString());
                            break;
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
}
        return null;

错误日志

04-03 14:38:33.395: ERROR/AndroidRuntime(30812): FATAL EXCEPTION: AsyncTask #3
        java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:278)
        at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
        at java.util.concurrent.FutureTask.run(FutureTask.java:137)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        at java.lang.Thread.run(Thread.java:856)
        Caused by: java.lang.OutOfMemoryError
        at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
        at com.squareup.picasso.NetworkBitmapHunter.decodeStream(NetworkBitmapHunter.java:108)
        at com.squareup.picasso.NetworkBitmapHunter.decode(NetworkBitmapHunter.java:60)
        at com.squareup.picasso.BitmapHunter.hunt(BitmapHunter.java:123)
        at com.squareup.picasso.RequestCreator.get(RequestCreator.java:225)
        at com.example.GoogleMemu.NewThread.doInBackground(NewThread.java:75)
        at com.example.GoogleMemu.NewThread.doInBackground(NewThread.java:23)
        at android.os.AsyncTask$2.call(AsyncTask.java:264)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

  @Override
    public void onBackPressed() {
        super.onBackPressed();
        b = null;
        iv.setImageBitmap(null);
        try {
            iv.destroyDrawingCache();
        }catch (Exception ex){ex.printStackTrace();}

        iv=null;
        try {
            finish();
        }catch (Exception ex){ex.printStackTrace();}

    }

2 个答案:

答案 0 :(得分:1)

尝试使用以下代码行创建缩放位图:

Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, width,
                height, true);

我已经给出了这个问题的答案。请点击以下链接

Exception : OutOfMemoryError

试试这个:

注意:IMAGE_MAX_SIZE根据您的密度设置此变量的大小。通过传递它在metod

final int IMAGE_MAX_SIZE;
if(dens.equalsIgnoreCase(density)){
        IMAGE_MAX_SIZE = 25000;//0.2 MP == 200000
}else if(dens.equalsIgnoreCase(density)){
    IMAGE_MAX_SIZE = 40000;
}else if(dens.equalsIgnoreCase(density)){
    IMAGE_MAX_SIZE = 70000;
}else{
    IMAGE_MAX_SIZE = 60000;
}

URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
connection.addRequestProperty(BConstant.WEB_SERVICES_COOKIES,
                cookie);
connection.addRequestProperty(
                BConstant.WEB_SERVICES_TOKEN_HEADER, token);
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
int height = myBitmap.getHeight();
int width = myBitmap.getWidth();
double y = Math.sqrt(IMAGE_MAX_SIZE / (((double) width) / height));
double x = (y / height) * width;
Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, (int) x,
                (int) y, true);
myBitmap.recycle();
    myBitmap = scaledBitmap;
System.gc();
return myBitmap;

答案 1 :(得分:0)

答案很简单:您正在加载图片 - 而且图片太大导致OutOfMemoryError,从而导致您的应用崩溃。考虑到您正在处理该列表,您不需要加载完整尺寸的图片。考虑加载/解码缩小的图像。

请看一下这个Android tutorial page - 具体来说,部分"将缩小后的版本加载到内存中#34;。