change background image of Framelayout via URL

后端 未结 4 1634
伪装坚强ぢ
伪装坚强ぢ 2020-12-21 05:46

is it possible to use a url to set background for framelayout?

Example: i want to use the URL like this as my framelayou

相关标签:
4条回答
  • 2020-12-21 06:11

    You may to use WebView to display web-site content. There is also wonderful tutorial of using WebView on android developers web site

    0 讨论(0)
  • 2020-12-21 06:23
    private RelativeLayout myRelativeLauout;
    
    myRelativeLauout = (RelativeLayout)findViewById(R.id.rlBg);  
    new LoadBackground("http://www.tmonews.com/wp-content/uploads/2012/10/androidfigure.jpg",
                "androidfigure").execute();
    
    
    private class LoadBackground extends AsyncTask<String, Void, Drawable> {
    
        private String imageUrl , imageName;
    
        public LoadBackground(String url, String file_name) {
            this.imageUrl = url;
            this.imageName = file_name;
        }
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
    
        @Override
        protected Drawable doInBackground(String... urls) {
    
            try {
                InputStream is = (InputStream) this.fetch(this.imageUrl);
                Drawable d = Drawable.createFromStream(is, this.imageName);
                return d;
            } catch (MalformedURLException e) {
                e.printStackTrace();
                return null;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        private Object fetch(String address) throws MalformedURLException,IOException {
            URL url = new URL(address);
            Object content = url.getContent();
            return content;
        }
    
    
        @Override
        protected void onPostExecute(Drawable result) {
            super.onPostExecute(result);
            myRelativeLauout.setBackgroundDrawable(result);
        }
    }
    
    0 讨论(0)
  • 2020-12-21 06:26

    Something interested, (I never try this but it should work)

    FrameLayout fm = (FrameLayout)findViewById(R.id.FrameLayout01);      
    Drawable drw = ImageOperations(this,url,filename)
    fm.setBackgroundDrawable(drw)
    

    Convert your image url in Drawable using

    private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
            try {
                InputStream is = (InputStream) this.fetch(url);
                Drawable d = Drawable.createFromStream(is, saveFilename);
                return d;
            } catch (MalformedURLException e) {
                e.printStackTrace();
                return null;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    
        public Object fetch(String address) throws MalformedURLException,IOException {
            URL url = new URL(address);
            Object content = url.getContent();
            return content;
        }
    

    Try this and let me know what happen..

    0 讨论(0)
  • 2020-12-21 06:32

    here is the code by you can get image in bitmap from URL

     Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
    

    and Useing This you can convert bitmap to drawable ...

    and after getting drawable apply in layout like

    framelyt.setBackgroundDrawable(d);
    
    0 讨论(0)
提交回复
热议问题