roy mathew
roy mathew

Reputation: 7800

Issue related to downloading of images in android

I am trying to download an image from server and save it in the internal storage of android. It is working well in all cases if I am downloading an image less than 2 MB. But when I download any JPEG image of size greater than 2 MB, I am facing two issues.

First one is that when I am trying to download and save the image from server using the emulator, I am getting an error like "bitmap size exceeds VM budget" and activity crashes.

Second issue is that this problem is not their when I run the application on my phone, but instead of that another issue is happening. When I am displaying the downloaded image, the image contains some other colours also(kind of rainbow colours).

Here is the code that I am using to download and save the image:

//For downloading the image file
    void downloadFile(String fileUrl)
    {
        URL myFileUrl = null;
        try {
            myFileUrl = new URL(fileUrl);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try 
        {
            HttpURLConnection conn = (HttpURLConnection) myFileUrl .openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bmImg = BitmapFactory.decodeStream(is);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    //For saving the downloaded image 
    void saveImage() {
        try 
        {
            String fileName ="displayimg.png";
            final FileOutputStream fos = openFileOutput(fileName, Activation.MODE_PRIVATE);
            bmImg.compress(CompressFormat.PNG, 90, fos);

        } catch (Exception e) 
        {
            e.printStackTrace();
        }

    }

Here is the code for displaying the image:

private Bitmap myBitmap;
myBitmap= BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ivDisplayImage.setImageBitmap(myBitmap);

Upvotes: 0

Views: 687

Answers (4)

Raghunandan
Raghunandan

Reputation: 133560

You can use Universal Image Loader. https://github.com/nostra13/Android-Universal-Image-Loader. Asynchronously Download Images. You cache images in memory or sdcard.

You can also have a look at Lazy loading https://github.com/thest1/LazyList.

Also recycle bitmaps when not in use.

 bitmap.recycle();

To load bitmpas efficiently see the documentation in the link. http://developer.android.com/training/displaying-bitmaps/load-bitmap.html.

java.lang.OutofMemoryError: bitmap size exceeds VM budget means you are having a memory leak. http://android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html.

http://www.youtube.com/watch?v=_CruQY55HOk. The talk is about memory management and avoiding memory leaks. Also explains how to use MAT Analyzer to find memory leaks.

Bitmap color change while compressing to png. Check the answer probably related to your question. Also check on what android version your phone is running.

Bitmap color change while compressing to png. Check this link.

Upvotes: 1

Hardik Joshi
Hardik Joshi

Reputation: 9507

You have to use ImageLoader class which scale your bitmap and display it in imageview.

Also follow this tutorial for display image from web.

Hope this helps you.

Upvotes: 2

Amitabh Sarkar
Amitabh Sarkar

Reputation: 1311

There are some issues with your code, first thing is you are trying to compress image with png format, png format dont compress. you should insted use jpeg. that will reduce the image size and also quality. Second thing there is a very efficient way to download image and show in an imageview, here is link: Displaying Bitmaps Efficiently it also explains your first problem java.lang.OutofMemoryError: bitmap size exceeds VM budget. and there are some good library I suggest you to use theme like

Upvotes: 3

user2157571
user2157571

Reputation:

Hey i would suggest you to use this library: http://loopj.com/android-async-http/

Here is sample code:

            AsyncHttpClient client = new AsyncHttpClient();
        String[] allowedContentTypes = new String[] { "image/jpeg" }; // here you need to type the mimetype of the file

        client.get("link to jpg or any image"),
                new BinaryHttpResponseHandler(allowedContentTypes) {
                    @Override
                    public void onSuccess(byte[] fileData) { 
                        // this means it downloaded succesfully and you could make a filoutputstream to sdcard 
                        FileOutputStream fos;
                        try {
                            fos = new FileOutputStream(Environment
                                    .getExternalStorageDirectory()
                                    + "/Images/test.jpg");

                            fos.write(fileData);
                            fos.close();
                            fos.flush();
                        } catch (FileNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } finally {
                        // here you could set it to an imageview or do anything you wouyld like

                        }
                    }
                });
    }

If you have any questions let me know.

Upvotes: 0

Related Questions