【问题标题】:How to set image button resource from web url in Android? [duplicate]如何在 Android 中从 web url 设置图像按钮资源? [复制]
【发布时间】:2013-02-18 11:03:58
【问题描述】:

我有一个图片按钮,想从互联网 URL 设置背景图片。我不想将背景图片保存到 SD 卡中,而是我的图像按钮的图像需要是 URL。我如何在 Android 中做到这一点

【问题讨论】:

  • 您需要将其下载到位图变量中,然后将其设置为背景图像。 Android 不是一个浏览器,您可以在其中执行您想要实现的功能。
  • 我想你的答案在这里stackoverflow.com/questions/2471935/…
  • @MudassarShaheen 我认为一条评论就足够了相同的链接:)

标签: android imagebutton


【解决方案1】:

试试这个

        Bitmap bitmap;
class loadImage extends AsyncTask<Void , Void, Void>{
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected Void doInBackground(Void... params) {
        URL url = new URL(stringURL);
        URI uri = new URI(url.getProtocol(), url.getHost(),
                url.getPath(), url.getQuery(), null);
        HttpURLConnection connection = (HttpURLConnection) uri
                .toURL().openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();

        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];
        int len = 0;
        while ((len = input.read(buffer)) != -1) {
            byteBuffer.write(buffer, 0, len);
        }
        byte[] img = byteBuffer.toByteArray();
        byteBuffer.flush();
        byteBuffer.close();
        input.close();
        bitmap = BitmapFactory.decodeByteArray(img, 0, img.length);
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        ImageButton image_btn = (ImageButton)findViewById(R.id.your_image_button_id);
        image_btn.setImageBitmap(bitmap);
    }
}

【讨论】:

    【解决方案2】:

    首先,您必须将您的图像下载为 Drawable: Android Drawable Images from URL

    然后将其设置为背景可绘制

    button.setBackgroundDrawable(drawable)
    

    【讨论】:

      【解决方案3】:

      从未尝试过,但希望这对你有用

      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;
          }
      }
      

      对于图像视图

      ImageView IV= (ImageView)findViewById(R.id.imageId);      
      Drawable drw = ImageOperations(this,url,filename)
      IV.setBackgroundDrawable(drw)
      

      获取网址

          public Object fetch(String address) throws MalformedURLException,IOException {
      
          URL url = new URL(address);
          Object content = url.getContent();
          return content;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-05-19
        • 1970-01-01
        • 2014-03-10
        • 1970-01-01
        • 2015-12-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多