2

I have a String[] with contains ten image url sources, and I'm trying to get ImageView to show them. I have to show each image with its corresponding text next to it, and even though the texts show the images just won't work. I've been trying all solutions I could find around, but nothing seems to work. I would greatly appreciate some guidance.

This is the solution I'm trying atm:

Method I call from the class that extends ListActivity (both titles and imageurls are String[], imageurls contains the image url sources and titles contains the text to be shown next to the images)

setListAdapter(new MyPerformanceArrayAdapter(this, titles, imageurls)); 

And this is the code I have for MyPerformanceArrayAdapter:

public class MyPerformanceArrayAdapter extends ArrayAdapter<String> { private final Activity context; private final String[] names; private final String[] images; public MyPerformanceArrayAdapter(Activity context, String[] names, String[] images) { super(context, R.layout.rowlayout, names); this.context = context; this.names = names; this.images = images; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = context.getLayoutInflater(); View rowView = inflater.inflate(R.layout.rowlayout, null, true); TextView textView = (TextView) rowView.findViewById(R.id.label); ImageView imageView = (ImageView) rowView.findViewById(R.id.icon); String s = names[position]; textView.setText(s); Drawable drawable = LoadImageFromWebOperations(images[position]); imageView.setImageDrawable(drawable); return rowView; } private Drawable LoadImageFromWebOperations(String url) { try { InputStream is = (InputStream) new URL(url).getContent(); Drawable d = Drawable.createFromStream(is, "src name"); return d; } catch (Exception e) { System.out.println("Exc="+e); return null; } } } 

Thank you very much!

1
  • Q: Have you stepped through the code under the debugger? Do you know if it's even REACHING LoadImageFromWebOperations? If so, is the URL being passed to it correct? Can you load the same URL yourself (for example, cutting/pasting the URL into a web browser)? My first guess is maybe your app is getting "permission denied". Definitely step through under the debugger, and definitely use LogCat. Commented Dec 24, 2011 at 20:19

2 Answers 2

1

If you are using path from sdcard, below code will work,else if you are using images from drwable- res folder, create a static Bitmap array like you did for String array

@Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = context.getLayoutInflater(); View rowView = inflater.inflate(R.layout.rowlayout, null, true); TextView textView = (TextView) rowView.findViewById(R.id.label); ImageView imageView = (ImageView) rowView.findViewById(R.id.icon); String s = names[position]; textView.setText(s); 

// changes done here

 Bitmap bmp = LoadImageFromWebOperations(images[position]); imageView.setImageBitmap(bmp); return rowView; } 

//

private Drawable LoadImageFromWebOperations(String url) { try { // use bitmap instead of drawable Bitmap bmp=BitmapFactory.decodeStream(getContentResolver().openInputStream(capturedImage)); return bmp; } catch (Exception e) { System.out.println("Exc="+e); return null; } } 
Sign up to request clarification or add additional context in comments.

1 Comment

@antak, if any suggestion helped you, please do accept the answer which you feel like best in all. so another person who has same query can know which one to try.
1

For this I always use Bitmaps instead of drawables, and so far it has worked perfectly.

Give this a try:

 @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = context.getLayoutInflater(); View rowView = inflater.inflate(R.layout.rowlayout, null, true); TextView textView = (TextView) rowView.findViewById(R.id.label); ImageView imageView = (ImageView) rowView.findViewById(R.id.icon); String s = names[position]; textView.setText(s); Bitmap image = LoadImageFromWebOperations(images[position]); imageView.setImageBitmap(image); return rowView; } private Bitmap LoadImageFromWebOperations(String url) { try { URL aURL = new URL(url); URLConnection conn = aURL.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); /* Buffered is always good for a performance plus. */ BufferedInputStream bis = new BufferedInputStream(is); /* Decode url-data to a bitmap. */ Bitmap bm = BitmapFactory.decodeStream(bis); bis.close(); is.close(); /* Apply the Bitmap to the ImageView that will be returned. */ return bm; } catch (Exception e) { System.out.println("Exc="+e); return null; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.