Android twitter4j - how to share a deleted image?

I have successfully configured twitter4j and can now post text updates as well as upload local media from device to twitter. But I really need to exchange images remote from the Internet - eg http://example.com/image.png

.

When I execute this code ... StatusUpdate statusUpdate = new StatusUpdate("Hello Twitter"); String imageUrl = "http://example.com/image.png"; File file = new File(imageUrl); statusUpdate.setMedia(file); twitter4j.Status status = twitter.updateStatus(statusUpdate);

... it looks like twitter4j is trying to treat the URL as local because it appears to be in front of it /

and then throws an exception saying ... /http://example.com/image.png: open failed: ENOENT (No such file or directory)

How to solve? Thank.

+3


source to share


2 answers


I found that there is another method setMedia()

that takes an input stream as one of its parameters. This input stream can be associated with a remote image, for example ...



StatusUpdate statusUpdate = new StatusUpdate("Hello Twitter");
String imageUrl = "http://example.com/image.png";
URL url = new URL(imageUrl);
URLConnection urlConnection = url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
statusUpdate.setMedia("image.png", in);
twitter4j.Status status = twitter.updateStatus(statusUpdate);
//might be a good idea to close the inputstream in a finally block

      

+6


source


I also had the same problem, the workaround I found was to download the image to my phone and then post it to twitter using the following code:



StatusUpdate status = new StatusUpdate(String status);
File file = new File(imageFilePath);
status.setMedia(file);
mTwitter.updateStatus(status);

      

0


source







All Articles