Nitish
Nitish

Reputation: 14113

Error on displaying image in imageview

This one has made me bite my nails. I parsed image URL's from XML into an arraylist.I am displaying images in table layout along with some text. I am using the following code in activity for displaying image :

Bitmap bm=DownloadImage(piciterator.next().toString());
icon.setImageBitmap(bm);

Here piciterator is iterating arraylist containing URL's.

Here is DownloadImage function :

private Bitmap DownloadImage(String URL)
{

    Bitmap bitmap = null;
    InputStream in = null;  

    try {
        in = OpenHttpConnection(URL);

        bitmap = BitmapFactory.decodeStream(in);
        in.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return bitmap;                
}

And this is OpenHttpConnection function :

private InputStream OpenHttpConnection(String urlString) throws IOException

{
    InputStream in = null;
    int response = -1;

    URL url = new URL(urlString); 
    URLConnection conn = url.openConnection();

    if (!(conn instanceof HttpURLConnection))                     
        throw new IOException("Not an HTTP connection");

    try{
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect(); 

        response = httpConn.getResponseCode();                 
        if (response == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream();                                 
        }                     
    }
    catch (Exception ex)
    {
        throw new IOException("Error connecting");            
    }
    return in;     
}  

On debugging i am able to retrieve some of the images on browser. However after 23rd element in arraylist the image opens on browser but application falls on the step :

bitmap = BitmapFactory.decodeStream(in);

On image when seen on browser is comparatively smaller than others but not much smaller.

The application not even goes to try catch. It just crashes.

Help in this regard will be really appriciated.

Upvotes: 1

Views: 509

Answers (2)

rawpower
rawpower

Reputation: 2160

The fact that it just crashes without printing the stack trace hints to the potential out-of-memory problem.

IIRC, Android apps are limited to 16MB. Have you verified if the combined size of the pictures surpasses this limit?

Upvotes: 1

Tanmay Mandal
Tanmay Mandal

Reputation: 40168

Always try to use Thread in network connection.

Here is the doc.

Painless Treading

Upvotes: 0

Related Questions