Error catching facebook profile image

1

I'm trying to get the profile image from fb but it returns the following error

java.io.FileNotFoundException: No content provider: link

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 method getUserPhoto

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

My url is that

url: link

Meditating to take 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

    
asked by anonymous 20.10.2015 / 13:57

1 answer

1

It is not recommended to use ContentResolver to get content from the internet, the correct way would be to use a URLConnection or using an image upload libraries because they eliminate boilerplate code and have all expertise to handle caching and resizing.

Looking at the behavior of the facebook url, it performs a redirect to the actual url that is behind a likely CDN. To do this, you need to handle the redirect manually.

Using 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: link

If you use a third-party library, it's a lot 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 one has a way to set up caching.

But it's a good idea to first consider whether it's worth incorporating these libraries into your project. If it is for one-time use, the overhead it brings to the size of the APK may not compensate ... It is good to check case by case.

    
20.10.2015 / 14:40