Dynamically populate Android ImageView with outsid

2019-02-16 02:38发布

问题:

How can I turn the static image

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.67"
        android:src="@drawable/static_image" />

into an ImageView whose source can be dynamically set to data that is not already in the res folder?

That is, my application has an icon on the screen but the actual image for the icon is downloaded from an outside server and can change dynamically. How do I update the ImageView with the desired image upon download? I want something functionally like:

Image selectedImage = //get from server

myImageView.setImage(selectedImage);

回答1:

Ur question is not clear. If u just wanna have an image(that is in some url) set to an image view,

Bitmap bmp=getBitmapFromURL(ur url here);
imgview.setImageBitmap(bmp);

and write this function:

public static Bitmap getBitmapFromURL(String src) {  
        try {

            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap mybitmap = BitmapFactory.decodeStream(input);

            return mybitmap;

        } catch (Exception ex) {

            return null;
        }


回答2:

yourImageView.setImageBitmap(bitmap);

to get Bitmap from server:

public static Bitmap loadBitmap(String url) {
    Bitmap bitmap = null;
    InputStream in = null;
    BufferedOutputStream out = null;

    try {
        in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);

        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
        copy(in, out);
        out.flush();

        final byte[] data = dataStream.toByteArray();
        BitmapFactory.Options options = new BitmapFactory.Options();
        //options.inSampleSize = 1;

        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
    } catch (IOException e) {
        Log.e(TAG, "Could not load Bitmap from: " + url);
    } finally {
        closeStream(in);
        closeStream(out);
    }

    return bitmap;
}


回答3:

As per I understand your question something like,

 ImageView selectedImage;
 selectedImage = (ImageView)findViewById(R.id.imageView1);
 Bitmap bmImg;

 downloadFile(imageUrl);

And this is downloadFile() Method...

 void downloadFile(String fileUrl){
          URL myFileUrl =null;          
          try {
               myFileUrl= new URL(fileUrl);
          } catch (MalformedURLException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
          try {
               HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
               conn.setDoInput(true);
               conn.connect();
               InputStream is = conn.getInputStream();

               bmImg = BitmapFactory.decodeStream(is);
               selectedImage.setImageBitmap(bmImg);
          } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
     }


回答4:

You need to create a custom view and over-ride onDraw(). From there you are provided the drawing Canvas and you can do whatever you like. So assuming your dynamic image can be converted to a bitmap, there are numerous drawBitmap() methods you can use from Canvas.

Note that you must also override onMeasure() which allows your view to tell the layout manager how much space you need.

There's a lot of good info here: http://developer.android.com/guide/topics/ui/custom-components.html