2
  • I'm getting a twitter feed in my Android Studio app, using Fabric
  • 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[]?

I have found what looks like an array of bytes, but when I attempt to decode it using bitmaps decodeByteArray, it returns 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?

4

2 回答 2

1

图片网址

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;
}
于 2015-11-15T08:38:27.187 回答
0

只有当它可用时,我才会得到图像:

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

如果您使用类型属性,您可以轻松地将图像/视频与 userTimeLine 区分开来

于 2015-11-18T07:24:02.123 回答