0

I have an adapter, ListView and data. I load data from a website showing on the screen. Scrolls and when I reach the end of the new data is loaded. everything works well. But when I scroll up trying to load image error output. and always differently.

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;

error log

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)

and

  @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();}

    }
IP696
  • 181
  • 1
  • 2
  • 11

2 Answers2

1

Try to create scaled bitmap using following line of the code:

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

I have already given the answer for this question. Please follow the below link

Exception : OutOfMemoryError

Try this:

Note: IMAGE_MAX_SIZE set the size of this variable according to your density. By passing it in the 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;
Community
  • 1
  • 1
rupesh
  • 2,865
  • 4
  • 24
  • 50
  • To use `Bitmap.createScaledBitmap`, you need to first load the full bitmap into memory - but this causes the out of memory error. You have to use bitmap decoding options to load it scaled down already. – Aleks G Apr 03 '14 at 08:56
0

The answer is quite simple: you are loading an image - and it's too large resulting in OutOfMemoryError, thus crashing your app. Considering that you're dealing with the list, you don't need to load full-size images. Consider loading/decoding scaled down images instead.

Have a look at this Android tutorial page - specifically, section "Loading scaled down version into memory".

Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • I load the list of small images. They are loaded normally. but when I try to download the image in the normal form is an error, and on the first or third or fourth picture – IP696 Apr 03 '14 at 08:52
  • @IP696 Make sure you recycle your bitmaps when you don't need them any more, otherwise the memory used by the images is not freed and loading new ones would add to the total memory used. – Aleks G Apr 03 '14 at 08:55
  • take pictures from the site. Online they resolution 1600x1200 and ~1mb load a bitmap full picture then show it to ImageView when a user wants to download, I remain the same but the bitmap is already in the phone memory – IP696 Apr 03 '14 at 09:07
  • @IP696 1600x1200 image is 1mb jpeg or png when saved as a file. When decoded as a bitmap, one image that size would take 1600*1200*32/8 = 7680000 bytes = 7.3 MB. You should NOT load full size images for the list. Instead, load scaled down images into the list (use the link I provided, it has sample code); then when needed load 1 full size image; then recycle it before loading the next one. – Aleks G Apr 03 '14 at 10:01