简体   繁体   English

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

[英]Android Studio - Get tweet image from twitter - Fabric

  • I'm getting a twitter feed in my Android Studio app, using Fabric我在我的 Android Studio 应用程序中使用 Fabric 获得了一个 twitter 提要
  • for each tweet that has an image attactched, I wish to display the image对于每个附加了图像的推文,我希望显示图像
  • how can I extract either a url to the image or a byte[]?如何提取图像的 url 或字节 []?

I have found what looks like an array of bytes, but when I attempt to decode it using bitmaps decodeByteArray, it returns null我发现了一个看起来像字节数组的东西,但是当我尝试使用位图 decodeByteArray 对其进行解码时,它返回 null

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

can anybody help me find a way to retrieve the image so I can display it?谁能帮我找到一种方法来检索图像,以便我可以显示它?

Image url图片网址

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

Decode the image解码图像

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;
}

i am getting image only if it is available like:我只在图像可用时获取图像,例如:

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);
         }

If u using type attributes u can easily differentiates image/video from userTimeLine如果您使用类型属性,您可以轻松地将图像/视频与 userTimeLine 区分开来

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM