Error while picking facebook profile picture

Asked

Viewed 151 times

1

Guys I’m trying to get the fb profile image but returns the following error

java.io.Filenotfoundexception: No content Provider: https://graph.facebook.com/--Aqui--esta--o--id--/picture?width=150&height=150

My code is like this

  Bitmap tempBitmap;
        tempBitmap = Util.getBitmapFromURL(Preference.getUserPhoto(getApplicationContext()));
        Drawable drawable = new BitmapDrawable(getResources(), tempBitmap);
        System.out.println("TempBitMap "+ tempBitmap );
        System.out.println("drawable "+ drawable );
        aux.setIcon(drawable);

Class preference metodo getUserPhoto

 public static String getUserPhoto(Context c){
    SharedPreferences prefs = c.getSharedPreferences("myPref", 0);
    return prefs.getString("url", "url");
}

My url eh this

url: https://graph.facebook.com/IDESTAACQUI/picture?width=150&height=150

Medoto to get the photo

 public static Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setDoInput(true);
        connection.connect();

        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);

        return myBitmap;
    } catch (Exception e) {
        // Log exception
        return null;
    }
}

Bitmap and drawable result

Tempbitmap null drawable android.graphics.drawable.Bitmapdrawable@22b0eb58

  • Is the profile photo you are trying to get private? (Photo privacy is only me or only friends who can view)?

  • Not this as private, the strange thing is that when I play the link in the browser appears the photo but in my code when I try to convert to bitmap it gives that error

1 answer

1

It is not recommended to use ContentResolver to obtain content from the internet, the correct way would be to use a URLConnection or by using an image uploading library, because they eliminate code Boilerplate and has all the expertise to handle image caching and resizing.

Observing the behavior of the facebook url, it redirects to the actual url that is behind a likely CDN. For this, you need to handle the redirect manually.

Using the URLConnection:

public static Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setDoInput(true);
        connection.connect();

        conn.setInstanceFollowRedirects(true);  //you still need to handle redirect manully.
        HttpURLConnection.setFollowRedirects(true);

        // normally, 3xx is redirect
        int status = conn.getResponseCode();
        boolean redirect = false;

        if (status != HttpURLConnection.HTTP_OK) {
            if (status == HttpURLConnection.HTTP_MOVED_TEMP
                || status == HttpURLConnection.HTTP_MOVED_PERM
                || status == HttpURLConnection.HTTP_SEE_OTHER)
                redirect = true;
        }

        if (redirect) {
            return getBitmapFromUrl(conn.getHeaderField("Location"));
        }

        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);

        return myBitmap;
    } catch (IOException e) {
        // Log exception
        return null;
    }
}

Source: http://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/

If you use a library third party, is much simpler:

Using Glide:

Glide.with(context)
     .load(url)
     .into(imageView);

// ou
Glide
    .with(context)
    .load(url)
    .asBitmap()
    .into(new SimpleTarget<Bitmap>(100,100) {
        @Override
        public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
            // Bitmap disponível
        }
    });

Using Picasso

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

// ou 

private Target target = new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {       
        // Usar o bitmap
    }

    @Override
    public void onBitmapFailed() {
        // Erro
    }
}

Picasso
    .with(context)
    .load(url)
    .into(target);

Of course each of them has a way of configuring cache.

But it is good to evaluate before whether it is worth incorporating these libraries into your project. If it is for a punctual use, the overhead that it brings to APK size may not compensate... Ai it is good to check case by case.

  • Wakim vlw by help, I used your first example Httpurlconnection to not need to use libs but this returning null to my tempBitMap.

  • Show, are you giving any error in the request that generated any Exception? You can check if you had any error in the console?

  • I will update my question with the code I’m using now, but there was no error in the console but returns null

  • The url is unchecked? Opening the image by Browser gave an error.

  • So this url I posted I took the id but if I pass the id it opens the normal image in the browser

  • @Roque, from what I understand, there are some redirects when accessing the url. For this you have to handle it manually. I will update my reply soon.

  • Blz, if you’ll help, this is the full url https://graph.facebook.com/806488489468071/picture?width=150&height=150

  • I tried to handle this redirect but it didn’t go very well, it seems that it has an encryption

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.