0

I am working on API. We have provided Url of Images. I have to show image from that Url in my list Activity for which custom Adapter is used. How can I set image throw url? I have spent almost two days in it. I used image view in my Activity.

RAS
  • 8,100
  • 16
  • 64
  • 86
  • 1
    read this: http://www.helloandroid.com/tutorials/how-download-fileimage-url-your-device – waqaslam May 09 '12 at 12:14
  • possible duplicate of [How to load an ImageView by URL in Android?](http://stackoverflow.com/questions/2471935/how-to-load-an-imageview-by-url-in-android) – waqaslam May 09 '12 at 12:15
  • i want to show image in ImageView which is in listview from server.not to want download it. –  May 09 '12 at 12:32
  • you cant show an online image without downloading it first – waqaslam May 09 '12 at 13:23

3 Answers3

2

try below code in your activity, you will get Bitmap object, using setImageBitmap(Bitmap bmt) you can do.

Method:

public Bitmap getBitmap(String url) {
            Log.d("getBitmap", "getBitmap");
            Bitmap bm = null;
            try {
                URL aURL = new URL(url);
                URLConnection conn = aURL.openConnection();
                conn.connect();
                InputStream is = conn.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);
                bm = BitmapFactory.decodeStream(new FlushedInputStream(is));
                bis.close();
                is.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {

            }
            return bm;
        }

inner class:

class FlushedInputStream extends FilterInputStream {
        public FlushedInputStream(InputStream inputStream) {
            super(inputStream);
        }
    }
Bhaskar Reddy
  • 480
  • 5
  • 13
0

see this complete example

MAC
  • 15,799
  • 8
  • 54
  • 95
0

Use this code to get bitmap image from file ,

Bitmap bitmapImage = BitmapFactory.decodeFile(//File path);
    imageView.setImageBitmap(bitmapImage);

and you can get file from uri through the below method ,

private File getFileFromUri(Uri uri) throws IOException {
    Cursor cursor = managedQuery(uri, null, null, null, null);
    if (cursor.getCount() == 0) {
        throw new IOException(String.format("cannot find data from %s",
                uri.toString()));
    } else {
        cursor.moveToFirst();
    }

    String filePath = cursor.getString(cursor
            .getColumnIndex(Video.VideoColumns.DATA));

    File file = new File(filePath);
    cursor.close();
    return file;
}

Hope this will be helpful to you

user
  • 323
  • 6
  • 11