3

I have tried almost everything mentioned anywhere but still i am unable to get the profile pic of the logged in user in my android app. Following is the code which I am using :-

new AsyncTask<String, Void, Bitmap>() {

        @Override
        protected Bitmap doInBackground(String... params) {
            try {
                URL url = new URL(params[0]);
                return BitmapFactory.decodeStream(url.openConnection().getInputStream());
            } catch (Exception e) {
                Log.d("ERROR", "Unable to get Image");
            }
            return null;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if(saveFlag) {
                saveImage(bitmap);
            }
            imageView.setImageBitmap(bitmap);
        }
    }.execute(url);

Url for the image is :- http://graph.facebook.com/{ID}/picture. I am able to see the image when I go the url from browser. I have tried both http and https.

Someone help.

Edit: I dont want to use any other api other than fb or normal android's.

deep
  • 1,586
  • 1
  • 18
  • 29

1 Answers1

0

you can use picasso and set image using picasso like this:

URL url = new URL(params[0]);

Picasso.with(context).load(url.toString()).into(imageView);

http://square.github.io/picasso/

OR

call the below method in async task and set the returned bitmap as image.

private Bitmap getImage(String imageUrl, int desiredWidth, int desiredHeight)
    {   
           private Bitmap image = null;
           int inSampleSize = 0;


            BitmapFactory.Options options = new BitmapFactory.Options();

            options.inJustDecodeBounds = true;

            options.inSampleSize = inSampleSize;

            try
            {
                URL url = new URL(imageUrl);

                HttpURLConnection connection = (HttpURLConnection)url.openConnection();

                InputStream stream = connection.getInputStream();

                image = BitmapFactory.decodeStream(stream, null, options);

                int imageWidth = options.outWidth;

                int imageHeight = options.outHeight;

                if(imageWidth > desiredWidth || imageHeight > desiredHeight)
                {   
                    System.out.println("imageWidth:"+imageWidth+", imageHeight:"+imageHeight);

                    inSampleSize = inSampleSize + 2;

                    getImage(imageUrl);
                }
                else
                {   
                    options.inJustDecodeBounds = false;

                    connection = (HttpURLConnection)url.openConnection();

                    stream = connection.getInputStream();

                    image = BitmapFactory.decodeStream(stream, null, options);

                    return image;
                }
            }

            catch(Exception e)
            {
                Log.e("getImage", e.toString());
            }

        return image;
    }
Aakash
  • 5,181
  • 5
  • 20
  • 37
  • 1
    I don't want to use any other api for this. Can't I do it with basic code and Facebook api ? – deep Oct 02 '15 at 20:16
  • it is not api, it is a library which allows you to cache images, make different shapes with images, allows you to place placeholders and much more, it make image handling much easier. – Aakash Oct 02 '15 at 21:11
  • 1
    I tried you code and even Picasso library. I am able to get image from google profile which i was able to get even before but still unable to get image through facebook. – deep Oct 03 '15 at 06:40