I am gathering data from youtube’s gdata API and would like to use the video’s thumbnail. I already have the urls for each of the thumbnails, however I am not able to get the images to show up in my listview. The bitmap is currently not returning null, however I am not able to call the imageview.setImageBitmap(bitmap) method with my current implementation (that I know of?). Please help if you know of a better way to do this! Thanks
public class ResultListActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String searchquery = null;
Intent starter = getIntent();
Bundle extras = starter.getExtras();
if (extras != null) {
searchquery = extras.getString("search");
}
final String URL = "http://gdata.youtube.com/feeds/api/videos?max-results=5&alt=json&author=user&q=" + searchquery;
final ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>();
final QueryYoutube query;
query = new QueryYoutube();
boolean success = query.searchYoutube(URL);
if (success == false) {
Toast.makeText(this, "Sorry, no matching results found.", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(ResultListActivity.this, YoutubeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
finish();
}
int size = query.getNumberResults();
HashMap<String, Object> row = new HashMap<String, Object>();
for (int i=0; i<size; i++) {
Bitmap bitmap = null;
Drawable d = null;
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(query.getThumbnail(i)).getContent());
bitmap.setDensity(Bitmap.DENSITY_NONE);
d = new BitmapDrawable(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
row = new HashMap<String, Object>();
row.put("Thumbnail", d);
row.put("Title", query.getTitle(i));
row.put("Description", query.getDescription(i));
data.add(row);
}
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.row,
new String[] {"Thumbnail","Title","Description"},
new int[] {R.id.thumb, R.id.title, R.id.description});
setListAdapter(adapter);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent lVideoIntent = new Intent(null, Uri.parse("ytv://"+query.getVideoID(position)),
ResultListActivity.this, OpenYouTubePlayerActivity.class);
startActivity(lVideoIntent);
}
});
}
}
The way I would recommend doing this is that you should extend the
ArrayAdapterclass then in thegetViewmethod you fire off anAsyncTaskto download the thumbnail in thread outside of the UI thread, then set the bitmap to the imageview inonPostExecute.You should use something like
HttpUrlConnectionto get theInputStreamfor the image to decode into aBitmap. If you are unsure about the dimensions of theBitmapit is important that you check the size of the image before you actually create aBitmapobject that causes anOutOfMemoryErrorso beforehand setBitmapFactory.OptionsinJustDecodeBoundstotrueand then get the dimensions of the image then choose a sufficientinSampleSizeand decode again to retrieve aBitmapthat is not too large.You should also have some sort of
Bitmapcache so that you don’t have to always retrieve from the web.