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?

Natalia Sharon
  • 216
  • 2
  • 17

2 Answers2

1

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;
}
user3024760
  • 176
  • 2
  • 13
  • 1
    thanks very much! This was returning null until I realised I need to get tweet.entities.media.get(0).mediaurl; instead of just the url - but it works :) – Natalia Sharon Nov 15 '15 at 15:19
0

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

Dhruv Raval
  • 4,946
  • 3
  • 29
  • 34