1

I'm trying to download an image from a URL and convert it into a bitmap, but the line

Bitmap myBitmap = BitmapFactory.decodeStream(input);

always causes the debugger to skip to the following line

return null;

without ever actually printing out the stack trace and the Exception variable also doesn't exist in the variables listed in the Debugger. I read a lot about how there might be issues with urls not actually leading to images, not well formated images and the like but it still has the same issue with a hardcoded image that I'm positive exists.

public static Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(
                "http://www.helpinghomelesscats.com/images/cat1.jpg");
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

Since it seems like the manifest.xml file might be involved I've edited to add here.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.delivery"
android:versionCode="1"
android:versionName="1.0">

<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".IntroPage"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Browse"></activity>
    <activity android:name=".ViewProduct"></activity>
    <activity android:name=".ViewOrder"></activity>
    <activity android:name=".GetAddress"></activity>
    <activity android:name=".ConfirmOrder"></activity>
</application>

4
  • 3
    have you used internet permission? Commented Nov 21, 2012 at 5:17
  • 1
    please try to see what've been throwed, you can log the IOexception using Log.i("TAG",e.getMessage) and post it here Commented Nov 21, 2012 at 5:43
  • If the debugger skips to the return null; in your catch branch, there must have been an exception thrown and the variable e should contain that exception. Commented Nov 21, 2012 at 6:39
  • Yes, internet permission is there. That's what's so concerning to me, even when I add the Log.i("TAG",e.getMessage), and put breakpoints on that line and the e.printStackTrace() line it still doesn't stop until the return null line. Commented Nov 21, 2012 at 15:33

3 Answers 3

3

following code work for all type of images.

    try {

        URL url = new URL("http://www.helpinghomelesscats.com/images/cat1.jpg");
        InputStream in = url.openConnection().getInputStream(); 
        BufferedInputStream bis = new BufferedInputStream(in,1024*8);
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        int len=0;
        byte[] buffer = new byte[1024];
        while((len = bis.read(buffer)) != -1){
            out.write(buffer, 0, len);
        }
        out.close();
        bis.close();

        byte[] data = out.toByteArray();
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        imageView.setImageBitmap(bitmap);
    }
    catch (IOException e) {
        e.printStackTrace();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for coding a reliable answer that doesn't pipe the InputStream into the BitmapFactory. Though I would suggest re-indenting your code as it doesn't look very nice.
0

Hi you may give these 2 pemssions in the android manifeast file

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

In your activity:

url="http://www.helpinghomelesscats.com/images/cat1.jpg"
Bitmap bmp=readBitmapFromNetwork(url);



 public static Bitmap readBitmapFromNetwork(String imgurl) {
         URL url;
         Bitmap bmp = null;
         InputStream is = null;
           BufferedInputStream bis = null;
           System.out.println("image url ==========   "+imgurl);
        try {

            url=new URL(imgurl);
            System.out.println("url.getPath()"+url.getPath());          
                try {
               URLConnection conn = url.openConnection();
                    conn.connect();
                    is = conn.getInputStream();
                    bis = new BufferedInputStream(is);
                bmp = BitmapFactory.decodeStream(bis);
                } catch (MalformedURLException e) {                 
                    System.out.println("Bad ad URL");
                    e.printStackTrace();
                } catch (IOException e) {                   
                    System.out.println("Could not get remote ad image");
                    e.printStackTrace();
                }

        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
         finally {
                try {
                    if( is != null )
                        is.close();
                    if( bis != null )
                      bis.close();
                } catch (IOException e) {
                    System.out.println("Error closing stream.");
                    e.printStackTrace();
                }
            }   


           return bmp;
            }

2 Comments

This code catches an exception on the conn.connect(); line and then prints out the following error.
11-21 10:34:34.554: W/System.err(818): java.net.UnknownHostException: Host is unresolved: www.helpinghomelesscats.com:80
0

This works for me, found it on here while searching out another issue. This way it's not holding up the UI. works very well in ArrayAdapters as well.

Useage:

_coverArt = (ImageView) row.findViewById(R.id.top_CoverArt);

new DownloadImageTask(_coverArt)
            .execute("Put your URL here Hint: make sure it's valid http://www.blah...");

The Asynctask

class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;

public DownloadImageTask(ImageView bmImage) {
    this.bmImage = bmImage;
}

protected Bitmap doInBackground(String... urls) {
    String urldisplay = urls[0];
    Bitmap mIcon11 = null;
    try {
        InputStream in = new URL(urldisplay).openStream();
        mIcon11 = BitmapFactory.decodeStream(in);
    } catch (Exception e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }
    return mIcon11;
}

protected void onPostExecute(Bitmap result) {
    bmImage.setImageBitmap(result);
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.