【问题标题】:Creating Bitmap using Picasso使用毕加索创建位图
【发布时间】:2015-12-18 11:38:43
【问题描述】:

我正在使用以下代码创建位图:

public 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 bitmap;
    } catch (Exception e) {
        // Log exception
        return null;
    }

但是,加载需要更多时间。 我想知道毕加索图书馆有可能吗?

到目前为止,我已经尝试过如下使用毕加索:

public Bitmap getBitmapFromURL(String src) {
    try {
        Picasso.with(this)
                .load(src)
                .placeholder(R.mipmap.ic_launcher).into(image);
        image.setDrawingCacheEnabled(true);
        image.buildDrawingCache();
        Bitmap bitmap = image.getDrawingCache();
        return bitmap;
    } catch (Exception e) {
        // Log exception
        return null;
    }

在哪里,

image 是我在 xml 布局中的 imageview。 问题是我不必显示或显示 ImageView,如果可能的话,我必须使用 Picasso 库直接为其生成 Bitmap。

还有一件事要说的是,我无法通过 Picasso 使用上述方式执行代码。它给了我错误非法状态异常。

【问题讨论】:

    标签: android android-studio bitmap imageview picasso


    【解决方案1】:

    请检查一下

    private Target target = new Target() {
          @Override
          public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 
           //get bitmap
          }
          @Override
          public void onBitmapFailed() {
          }
    }
    
    private void someMethod() {
       Picasso.with(this).load("url").into(target);
    }
    
    @Override 
    public void onDestroy() {  // could be in onPause or onStop
       Picasso.with(this).cancelRequest(target);
       super.onDestroy();
    }
    

    【讨论】:

      【解决方案2】:
      target = new Target() {
          @Override
          public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
              // bitmap instance and use it for your own
          }
      
          @Override
          public void onBitmapFailed() {
      
          }
      };
      
      Picasso.with(this).load(yourImageURL).into(target);
      

      使用上面的代码来获取这个。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-21
        • 2018-03-02
        • 2014-11-24
        • 2016-02-02
        • 1970-01-01
        • 1970-01-01
        • 2018-12-29
        • 1970-01-01
        相关资源
        最近更新 更多