android - 如何从Facebook获取图像并在 ImageView 中显示

标签 android facebook

loginBtn.setUserInfoChangedCallback(new UserInfoChangedCallback() {
            @Override
            public void onUserInfoFetched(GraphUser user) {
                if (user != null) {
                    userName.setText("Hello, " + user.getName());

                    Bitmap bitmap1 = getFacebookProfilePicture(user.getId());

                    imgview.setImageBitmap(bitmap1);
                } else {
                    userName.setText("You are not logged");
                }
            }
        });
public Bitmap getFacebookProfilePicture(String userID) {

        try {
            URL imageURL = new URL("https://graph.facebook.com/" + userID
                    + "/picture?type=large");
            bitmap = BitmapFactory.decodeStream(imageURL.openConnection()
                    .getInputStream());

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return bitmap;

    }

这是我的代码,我正在尝试从 Facebook 获取个人资料图片并将其显示在我的应用程序的 ImageView 中,我能够从 Facebook 获取姓名,但是当我输入获取个人资料图片的代码时,我无法获取图像在 android 中请告诉我哪里做错了给我解决方案如何设置此代码。

最佳答案

使用该方法从Url中获取Bitmap。

 /**
     * Download image from server url and return bitmap
     *
     * @param stringUrl Imaage download url
     * @return Bitmap receieved from server
     */
    private Bitmap downloadImage(String stringUrl) {
        URL url;
        Bitmap bm = null;
        try {
            url = new URL(stringUrl);
            URLConnection ucon = url.openConnection();
            InputStream is;
            if (ucon instanceof HttpURLConnection) {
                HttpURLConnection httpConn = (HttpURLConnection) ucon;
                int statusCode = httpConn.getResponseCode();
                if (statusCode == 200) {
                    is = httpConn.getInputStream();
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = 8;
                    BufferedInputStream bis = new BufferedInputStream(is, 8192);
                    ByteArrayBuffer baf = new ByteArrayBuffer(1024);
                    int current = 0;
                    while ((current = bis.read()) != -1) {
                        baf.append((byte) current);
                    }
                    byte[] rawImage = baf.toByteArray();
                    bm = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length);
                    bis.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bm;
    }

像这样将 Facebook 个人资料图片 url 传递给此方法,并将下载的位图设置为 imageview。

 URL imageURL = new URL("https://graph.facebook.com/" + userID
                    + "/picture?type=large");

 imgview.setImageBitmap(downloadImage(imageURL));

希望对您有所帮助!

关于android - 如何从Facebook获取图像并在 ImageView 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32047569/

相关文章:

android - 在android中的SD卡上创建一个文件

android - 相机上的谷歌地图以循环方式变化

android - 使用 facebook sdk 自动登录

TableViewCells 中的 iOS Facebook SDK 配置文件图片

PHP + Facebook : how to upload photo on the wall?

iOS FBSDKShare API shareWithContent 发布失败

Android Studios RuntimeException : Unexpected exception in dex writer thread

android - 创建Activity对象时是否调用onCreate?

facebook - GitHub 社交媒体图像在更新时未刷新

facebook - 简单应用程序的异地数据存储,或类似的支持持久性机制?