I'm developing a simple app where it displays pictures just like tumblr and 9gag. I couldn't exactly get it done, i hope someone can help me figure this out, i'm trying to display the pictures from eversorandom.com in a single line without a title or description.
have a try at this:
add these permissions:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
here's code:
List<Bitmap> items = new ArrayList<Bitmap>();
private class ImageGetter extends AsyncTask<String, Void, Document> {
@Override
protected Document doInBackground(String... params) {
try {
Document doc = Jsoup.connect(params[0]).get();
for (int i=0; i<doc.select("div.tw_post*").size(); i++) {
// Normal size:
String normal = doc.select("div.tw_post*").get(i).select("a").select("img").attr("src").toString());
// Large size:
String hiRes = doc.select("div.tw_post*").get(i).select("a").select("img").attr("data-hi-res-src").toString());
// Decodes Normal size image:
BufferedInputStream in = new BufferedInputStream(new URL(normal).openStream());
// Puts in "items" array list:
items.add(BitmapFactory.decodeStream(in));
// Closes stream:
in.close();
}
return doc;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Document result) {
super.onPostExecute(result);
if (result == null) {
Toast.makeText(getApplicationContext(), "Error!", Toast.LENGTH_SHORT).show();
} else {
// what to do when some images are found
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// what to do before everything is done
}
}
use in onCreate()
method or anywhere else:
new ImageGetter().execute("http://eversorandom.com");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.