0

I've really been stumped by this problem. My app uses Facebook SDK and has a fragment (PersonalFragment) attached to the Main Activity, part of which displays the current user's name in a Text View (R.id.textView), and the current user's image in an ImageView (R.id.imageView)

My problem is I use the following logic to get the profile picture URI, and then use verified code to get a Bitmap from a URI. The following code results in a simple: "e\FNF Exception: Profile Picture" being written into the Log.

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v=inflater.inflate(R.layout.fragment_personals, container, false);


    ImageView image =(ImageView) (v.findViewById(R.id.imageView));
    if(Profile.getCurrentProfile()!=null)
    {
          try {

          Uri uri = (Profile.getCurrentProfile().getProfilePictureUri(100, 150));
              Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);
              image.setImageBitmap(bitmap);
              image.setScaleType(ImageView.ScaleType.FIT_XY);
          }
        catch(FileNotFoundException f)
        {
            Log.e("FNF Exception","Profile Picture");
        }
        catch(IOException i)
        {
          Log.e("IO Exception", "Profile Picture");
        }
    }
    ((TextView)v.findViewById(R.id.textView)).setText(Profile.getCurrentProfile().getName());

As one can see, the try-catch is within the if statement, so Profile.getCurrentProfile() is certainly not null. Furthermore, the code correctly inputs the user's name into the textview. Only the profile picture code throws a FileNotFoundException.

Suggestions?

  • Did you see the `uri` at the line `Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);` gives right uri address? Facebook may or may not give it by the user permission. – Youngjae May 31 '15 at 04:40

1 Answers1

1

The uri parameter in the line

MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);

is meant to reference images that are on the device or are part of your app's package using a file:// or content:// prefix. It can't be used to load image from the internet.

You could instead use something like this:

URL url = new URL(Profile.getCurrentProfile().getProfilePictureUri(100, 150).toString());
Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());

An alternative, easier way of displaying a profile picture would be to use the ProfilePictureView included in the Facebook SDK:

 <com.facebook.login.widget.ProfilePictureView
   android:id="@+id/profilePicture"
   android:layout_height="wrap_content"
   android:layout_width="wrap_content" />

and

((ProfilePictureView)findViewById(R.id.profilePicture)).setProfileId(
  Profile.getCurrentProfile().getId()
);
subeeshb
  • 478
  • 4
  • 10
  • Works but you might get a `StrictMode` Exception. Solution here: http://stackoverflow.com/questions/22395417/error-strictmodeandroidblockguardpolicy-onnetwork – Alaa M. Oct 11 '16 at 14:05