0

Though not very elegant, I used a quick hack to get around this with web views. However, I still hope to discover what was wrong.

With respect to the flickr API,

I have an image that I want a Bitmap object for and I have access to the user id grimneko and picture id 8638235984.

Therefore, I tried

URL url = new URL("http://www.flickr.com/photos/grimneko/8637130199/lightbox/");
Bitmap mp = BitmapFactory.decodeStream(url.openConnection().getInputStream());

However, mp is null in this case. I also tried www.google.com as the url and that also does not appear to work.

However, http://3.bp.blogspot.com/-sUAEV-EIlyY/T4WIGDkoLpI/AAAAAAAACKI/epNLfw01cW0/s320/corgi+working.jpg does work.

My guess is that Bitmap objects can only be created from urls that have image extensions. Is this correct?

If that is the case, how might I render the image from that flickr url in a Image View?

user1431282
  • 6,535
  • 13
  • 51
  • 68

2 Answers2

0

Try this..

   public static Bitmap getBitmapFromURL() {
        try {
            URL url = new URL("http://www.flickr.com/photos/grimneko/8637130199/lightbox/");
            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;
        }
}

and also refer this link..

How to load an ImageView by URL in Android?

Community
  • 1
  • 1
Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • Hi Tamilan, thank you for the response. However, if you look at the code above it is almost identical. Upon testing this, it still returns null. – user1431282 Oct 06 '13 at 06:32
  • @user1431282 have you check with this link http://3.bp.blogspot.com/-sUAEV-EIlyY/T4WIGDkoLpI/AAAAAAAACKI/epNLfw01cW0/s320/corgi+working.jpg did you get image – Hariharan Oct 06 '13 at 06:36
  • Yes :) its does work with that image as I mentioned in my original post. I was wondering more about why this approach does not work with any of the flickr images. – user1431282 Oct 06 '13 at 22:21
  • @user1431282 because that's not single image that's gallery that's why it's not woking.. – Hariharan Oct 07 '13 at 03:58
  • @user1431282 here is that same image link http://farm9.staticflickr.com/8246/8637130199_0b39510452_o.jpg try with this link – Hariharan Oct 07 '13 at 04:11
0
private class setProfileData extends AsyncTask<Void, Void, Bitmap> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage("Loading ..");
        progressDialog.setCancelable(true);
        progressDialog.show();

    }

    @Override
    protected Bitmap doInBackground(Void... arg0) {
        try {

            String imgurl = imageURL.replace(" ", "%20");
            Log.e("Profile pic path ----------->>>>>", imgurl);
            ProfilePicBitmap = null;
            // URL url = new URL(imgurl);
            try {
                Bitmap ProfilePicBitmap = null;
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 8;
                try {
                    ProfilePicBitmap = BitmapFactory.decodeStream(
                            (InputStream) new URL(imgurl).getContent(), null, options);

                } catch (Exception e) {

                    e.printStackTrace();
                    try {
                        ProfilePicBitmap = BitmapFactory.decodeStream((InputStream) new URL(imgurl)
                        .getContent());

                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
                ProfilePicBitmap = BitmapFactory.decodeResource(
                        getResources(), R.drawable.ic_launcher);
            }

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

    }

    @Override
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }

        try {
            if (ProfilePicBitmap != null) {
                ImageView.setImageBitmap(ProfilePicBitmap);
            } else {
                ImageView.setImageBitmap(BitmapFactory
                        .decodeResource(getResources(),
                                R.drawable.ic_launcher));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onCancelled() {

        super.onCancelled();
        progressDialog.dismiss();

    }
}
mike_m
  • 1,526
  • 4
  • 14
  • 19
Vaishali Sutariya
  • 5,093
  • 30
  • 32