Android Studio - 从推特获取推文图片 - Fabric

Android Studio - Get tweet image from twitter - Fabric

我发现了一个看起来像字节数组的东西,但是当我尝试使用位图 decodeByteArray 对其进行解码时,它 returns null

                            String mediaString = t.entities.media.toString();
                            String[] mediaArray = mediaString.split("(?=@)");
                            byte[] mediaBytes = mediaArray[1].getBytes();

任何人都可以帮我找到一种方法来检索图像以便我可以显示它吗?

图片url

String mediaImageUrl = tweet.entities.media.get(0).url;
Bitmap mediaImage = getBitmapFromURL(mediaImageUrl);
Bitmap mImage = null;

解码图像

private Bitmap getBitmapFromURL(final String mediaImageUrl) {

    try {
        Thread t = new Thread() {
            public void run() {
                try {
                    URL url = new URL(mediaImageUrl);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setDoInput(true);
                    connection.connect();
                    InputStream input = connection.getInputStream();
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inScaled = false;
                    mImage = BitmapFactory.decodeStream(input, null, options);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        t.start();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return mImage;
}

只有当图像可用时我才会获取图像,例如:

String mediaImageUrl = null;
         if (tweet.entities.media != null) {
              String type = tweet.entities.media.get(0).type;

                    if (type.equals("photo")) {
                       mediaImageUrl = tweet.entities.media.get(0).mediaUrl;
                    }
                    else {
                       mediaImageUrl = "'";
                    }
                            System.out.println("mediaImageUrl" + mediaImageUrl);
         }

如果你使用 type 属性你可以轻松区分 image/video 来自 userTimeLine