I am using picasso library for set marker with image on google map api. But i need to get loaded drawable which loaded from picasso.
I tried this but its not returning the url which loaded from picasso
Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
};
You cannot get the URL from Picasso after the drawable has loaded (Picasso.LoadedFrom will only tell you whether the image was loaded from DISK, MEMORY or NETWORK).
But since you need to specify the URL when you tell Picasso to .load() the image, you can simply store the URL somewhere and use it later on.
If your actual goal is to load a custom marker icon from a remote URL into a Google Maps marker, here is how to do it using an ad-hoc ImageView:
private void loadImageFromUrlIntoMarker(final String url, final Marker marker) {
final ImageView iv = new ImageView(context);
Picasso.with(context).load(url).into(iv, new Callback() {
#Override
public void onSuccess() {
Bitmap bm = ((BitmapDrawable) iv.getDrawable()).getBitmap();
try {
marker.setIcon(BitmapDescriptorFactory.fromBitmap(bm));
} catch (Exception e) {
Log.e(getClass().getSimpleName(), "Could not load image into marker.");
}
}
#Override
public void onError() {
Log.e(getClass().getSimpleName(), "Could not load image from " + url);
}
});
}
The try { ... } catch { ... } is important, as the marker might be invalid when the drawable becomes available.
basic usage of picasso
Picasso.with(context).load(my_url).into(my_view);
and import the library using gradle
compile 'com.squareup.picasso:picasso:2.5.2'
I dont know why you asking this question because you need an url to load image with Picasso . You can create a function basically like :
public void loadimage(final String loadingUrl){
Picasso.with(getApplicationContext()).load(loadingUrl).into(yourimageview, new Callback() {
#Override
public void onSuccess() {
Log.v("picasso","i successfully created image, here is url : "+loadingUrl);
}
#Override
public void onError() {
}
});
}
Related
I am trying to load an image:
Picasso.with(SelectActivity.this).load(picture).into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
bmp = bitmap;
findViewById(R.id.facebookButton).setEnabled(true);
continueToEditing();
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
findViewById(R.id.facebookButton).setEnabled(true);
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
picture is a valid string to a reachable, valid JPEG image. I've got everything inside a try/catch block and I've got breakpoints on onBitmapLoaded, onBitmapFailed, and try/catch's catch block.
However, none of this is called. There is also nothing in logcat related to this, too. What am I doing wrong?
Try keeping a strong reference to the Target object as a class variable and give it a try.
E.g.
Target target;// Class variable
//Now define this on your onCreate method
target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
bmp = bitmap;
findViewById(R.id.facebookButton).setEnabled(true);
continueToEditing();
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
findViewById(R.id.facebookButton).setEnabled(true);
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
//Now set the target on the Piccaso load LOC
Picasso.with(SelectActivity.this).load(picture).into(target);
I am using picasso library for loading images in my app. It is working fine in android version less than 5.1.1, but not in the 5.1.1.
Here is the sample url which is not loading
http://sexocomcafe1-teste.tempsite.ws/imagensUsuario13/avata/Atração%20PerigosaRJ_44690132.jpg
This url loads perfectly in andriod 4.2.1 and others , but not in my 5.1.1 device.
Here is the code i have tried
//Singleton instance of picasso
getPicassoInstance(){
if (picasso == null) {
picasso = new Picasso.Builder(PreferenceHelper.getContext()).executor(Executors.newSingleThreadExecutor())
.build();
}
return picasso;
}
getPicassoInstance().load(url).placeholder(R.drawable.default_image)
.into(myImageView);
Also following code by using the Target
Target targetAppLogo = new Target() {
#Override
public void onPrepareLoad(Drawable arg0) {
Logger.error("on prepare load avatar");
}
#SuppressLint("NewApi")
#Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
Logger.error("Save aavatar success ");
}
#Override
public void onBitmapFailed(Drawable arg0) {
Logger.error("Bitmap loading failed ");
}
};
getPicassoInstance().load(url).into(targetAppLogo);
Above code gives bitmap download success for 4.2.1 and gives bitmap loading failed for 5.1.1.
I have no clue what is going wrong with the code.
Any help is appreciated
Aquery is also using for image loading, download aquery jar and import it in your project and try this way
public class MainActivity extends Activity {
private ImageView img;
private AQuery aq;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aq = new AQuery(this);
img=(ImageView)findViewById(R.id.simpleLoadImg);
aq.id(R.id.simpleLoadImg).image("http://sexocomcafe1-teste.tempsite.ws/imagensUsuario13/avata/Atra%C3%A7%C3%A3o%20PerigosaRJ_44690132.jpg",false,false);
}
}
Use Html.fromHtml(String) method. This should resolve your issue.
Add following line before load the image:
url = Html.fromHtml(url).toString();
So it will look like:
url = Html.fromHtml(url).toString();
getPicassoInstance().load(url).placeholder(R.drawable.default_image)
.into(myImageView);
Following is my sample code to load your provided URL image:
String url = Html.fromHtml("http://sexocomcafe1-teste.tempsite.ws/imagensUsuario13/avata/Atra%C3%A7%C3%A3o%20PerigosaRJ_44690132.jpg").toString();
Picasso.with(this)
.load(url)
.into(img);
If you are not able to find solution, please check the below thing in your android code
final boolean[] isImageLoad = {false};
String finalUrl = url;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
while (!isImageLoad[0]) {
Picasso.get().load(finalUrl).into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.d(TAG, "store image url onBitmapLoaded ");
heatMap.setBackground(new BitmapDrawable(getActivity().getResources(), bitmap));
isImageLoad[0] = true;
}
#Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
Log.d(TAG, "store image url " + e.getMessage());
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
Log.d(TAG, "store image url onPrepareLoad ");
isImageLoad[0] = false;
}
});
}
}
}, 100);
Happy Coding!!
I need to show picture in ImageView and download that to local folder. Using Picasso library. And when image is loading I want to show ProgressBar of downloading.
Picasso.with(mContext)
.load(MYurl.BASE_URL + "/" + getItem(position).getImgThumb())
.into(viewHolder.asanaImg);
please try the following picaso callback function and save bitmap to locally folder
Picasso.with(getContext()).load(url).into(new Target() {
#Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
// cache is now warmed up
}
#Override public void onBitmapFailed(Drawable errorDrawable) { }
#Override public void onPrepareLoad(Drawable placeHolderDrawable) { }
});
I am working on demo application in which I am using Picasso library v2.5.2. It is working fine on all android operating system version, but not in lollipop.
Image whose size is 130KB which is not loading for me. Images whose size is less are loading correctly.
Here is my code for downloading bitmap and set on imageview.
target = new Target() {
#Override
public void onPrepareLoad(Drawable drawable) {}
#Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
if(bitmap != null) {
imageView.setImageBitmap(bitmap);
}
}
#Override
public void onBitmapFailed(Drawable drawable) {}
};
Picasso.with(this).load(URL).into(target);
I'm not sure what extra stuff I have to do with this so that I will work on lollipop also or this is bug in lib ?
It's a known problem. The problem is that Picasso keeps a weak reference for the Target. To get it working you need to make it strong, by storing a Target as a tag of view, for example.
target = new Target() {
#Override
public void onPrepareLoad(Drawable drawable) {}
#Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
if(bitmap != null) {
imageView.setImageBitmap(bitmap);
}
}
#Override
public void onBitmapFailed(Drawable drawable) {}
};
imageView.setTag(target);
Picasso.with(this).load(URL).into((Target) imageView.getTag());
EDIT:
I suggest you to use Glide, it's very similar to Picasso, and also recommended by Google. And as you can see in the end of this thread, the original developer solves this BitmapFactory problem by using extra buffer.
Why would you use a Target if you only need to load the image into the ImageView? Just use this:
Picasso.with(this).load(URL).into(imageView, new Callback()
{
#Override
public void onSuccess()
{
//Dimiss progress dialog here
}
#Override
public void onError()
{
//And here
}
});
For documentation look here.
Picasso.with(this).load("http://webneel.com/wallpaper/sites/default/files/images/04-2013/island-beach-scenery-wallpaper.jpg").placeholder(R.mipmap.ic_launcher).fit().into(imageView, new Callback() {
#Override public void onSuccess()
{
}
#Override public void onError()
{
}
});
fit() will help you to load image.And use android:adjustViewBounds="true" in your ImageView in xml.
I am using Picasso library to load image from url. The code I used is below.
Picasso.with(getContext()).load(url).placeholder(R.drawable.placeholder)
.error(R.drawable.placeholder).into(imageView);
What I wanna do is to get the image that loaded from url. I used
Drawable image = imageView.getDrawable();
However, this will always return placeholder image instead of the image load from url. Do you guys have any idea? How should I access the drawable image that it's just loaded from url.
Thanks in advance.
This is because the image is loading asynchronously. You need to get the drawable when it is finished loading into the view:
Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
imageView.setImageBitmap(bitmap);
Drawable image = imageView.getDrawable();
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
};
Picasso.with(this).load("url").into(target);
mImageView.post(new Runnable() {
#Override
public void run() {
mPicasso = Picasso.with(mImageView.getContext());
mPicasso.load(IMAGE_URL)
.resize(mImageView.getWidth(), mImageView.getHeight())
.centerCrop()
.into(mImageView, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
Drawable drawable = mImageView.getDrawable();
// ...
}
#Override
public void onError() {
// ...
}
});
}
});