Reputation: 97
I have create app which load picture from facebook. I use Picasso. Picasso will set image result to my Imageview. I want to get that image convert to bitmap.
Here is my Code to get Image from 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.
I need your help. Thank you
Upvotes: 0
Views: 1456
Reputation: 2833
This should be inside an AsyncTask:
try {
URL url = new URL("http://....");
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch(IOException e) {
System.out.println(e);
}
Upvotes: 1
Reputation: 24847
You should use the Picasso
API to accomplish what you are looking to do.
One option would be to provide a Target
listener to perform the Bitmap
manipulation before setting it on the ImageView
like this:
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.with(getBaseContext())
.load("https://graph.facebook.com/" + id + "/picture?type=large")
.resize(70, 70)
.into(ImageView);
Upvotes: 1
Reputation: 225
You can do it by the following code
ImageView img;
img.setDrawingCacheEnabled(true);
Bitmap scaledBitmap = img.getDrawingCache();
Upvotes: 0
Reputation: 3249
// Your imageview declaration
ImageView iv;
// Enable cache of imageview in oncreate
iv.setDrawingCacheEnabled(true);
// Get bitmap from iv
Bitmap bmap = iv.getDrawingCache();
Upvotes: 0
Reputation: 257
Does it seem like loading the image into the ImageView is working? What's probably happening is that you are calling ImageView.getDrawable() before Picasso has finished loading the image. When do you call the getDrawable code? 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() {
}
});
Upvotes: 1