简体   繁体   English

如何从imageview获取可绘制内容并将其转换为位图?

[英]How to get drawable from imageview and convert to bitmap?

I have create app which load picture from facebook. 我已经创建了从Facebook加载图片的应用程序。 I use Picasso. 我用毕加索。 Picasso will set image result to my Imageview. 毕加索会将图像结果设置为我的Imageview。 I want to get that image convert to bitmap. 我想将图像转换为位图。

Here is my Code to get Image from Facebook: 这是我从Facebook获取图像的代码:

 URL imageURL = new URL("https://graph.facebook.com/"+id+"/picture?type=large");                
 Picasso.with(getBaseContext()).load(imageURL.toString()).into(ImageView);

Here is my Code to get Image and convert to bitmap: (not work) 这是我的获取图像并转换为位图的代码:(不起作用)

BitmapDrawable drawable = (BitmapDrawable) ImageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
bitmap = Bitmap.createScaledBitmap(bitmap, 70, 70, true);
ImageViewTest.setImageBitmap(bitmap);

ImageView.getDrawable() always return null. ImageView.getDrawable()始终返回null。

I need your help. 我需要你的帮助。 Thank you 谢谢

Does it seem like loading the image into the ImageView is working? 好像将图像加载到ImageView正常吗? What's probably happening is that you are calling ImageView.getDrawable() before Picasso has finished loading the image. 可能发生的情况是您在毕加索完成加载图像之前调用ImageView.getDrawable()。 When do you call the getDrawable code? 什么时候调用getDrawable代码? Try doing something like: 尝试做类似的事情:

Picasso.with(getBaseContext()).load(imageURL.toString())
  .into(ImageView, new com.squareup.picasso.Callback() {
      @Override
      public void onSuccess() {
          BitmapDrawable drawable = (BitmapDrawable) ImageView.getDrawable();
          ...
      }

      @Override
      public void onError() {

      }
});

You should use the Picasso API to accomplish what you are looking to do. 您应该使用Picasso API完成您想做的事情。

One option would be to provide a Target listener to perform the Bitmap manipulation before setting it on the ImageView like this: 一种选择是提供一个Target侦听器来执行Bitmap操作,然后再在ImageView上进行设置,如下所示:

Picasso.with(getBaseContext())
        .load("https://graph.facebook.com/" + id + "/picture?type=large")
        .into(new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                // Manipulate image and apply to ImageView
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {

            }
        });

Or better yet, use Picasso to perfom the resize operation and do not do any Bitmap manipulation yourself, like so: 或者更好的是,使用Picasso执行调整大小操作,并且自己不要进行任何Bitmap操作,如下所示:

Picasso.with(getBaseContext())
        .load("https://graph.facebook.com/" + id + "/picture?type=large")
        .resize(70, 70)
        .into(ImageView);
// Your imageview declaration
ImageView iv;
// Enable cache of imageview in oncreate
iv.setDrawingCacheEnabled(true);
// Get bitmap from iv
Bitmap bmap = iv.getDrawingCache();

This should be inside an AsyncTask: 这应该在AsyncTask内部:

try {
    URL url = new URL("http://....");
    Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch(IOException e) {
    System.out.println(e);
}

You can do it by the following code 您可以通过以下代码来完成

 ImageView img;
 img.setDrawingCacheEnabled(true);
 Bitmap scaledBitmap = img.getDrawingCache();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM