Android: Use Glide to load Facebook Photo URL

2019-09-16 08:48发布

问题:

I'm building an Android app in which I'm trying to let the user bring in photos from Facebook. After I grab the URLs from a GraphRequest, I'm trying to use Glide to load the image into an ImageButton but I keep getting the error D/skia: --- skImageDecoder::Factory returned null. Here's my code:

GraphRequest request = GraphRequest.newMeRequest(
        AccessToken.getCurrentAccessToken(),
        new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject object, GraphResponse response) {
                try {
                    String url = object.getJSONObject("photos").getJSONArray("data").getJSONObject(0).getString("link");
                    Glide.with(UserSettingsActivity.this).load(url).into(userOne);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
Bundle parameters = new Bundle();
parameters.putString("fields","id,photos{link}");
request.setParameters(parameters);
request.executeAsync();

EDIT - Sorry, I realized right after posting that I didn't actually ask a question. Does anyone know what the correct way to load Facebook photos is? Thanks!

回答1:

I was able to solve the problem by switching photos{link} to photos{source} which returns the actual source URLs. Here's the revised code:

GraphRequest request = GraphRequest.newMeRequest(
        AccessToken.getCurrentAccessToken(),
        new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject object, GraphResponse response) {
                try {
                    String url = object.getJSONObject("photos").getJSONArray("data").getJSONObject(0).getString("source");
                    Glide.with(UserSettingsActivity.this).load(url).into(userOne);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
Bundle parameters = new Bundle();
parameters.putString("fields","id,photos{source}");
request.setParameters(parameters);
request.executeAsync();