Dynamically populate Android ImageView with outside resources

感情迁移 提交于 2019-12-03 23:09: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



来源:https://stackoverflow.com/questions/8585493/dynamically-populate-android-imageview-with-outside-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!