I have an async task that download data from webpage using HTTPClient. The problem is while the Picasso is downloading image, the async task must wait for the image downloading to be done.
How could I make the async task run before the image downloading?
I tried this but it doesn't work:
protected Void doInBackground() {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
private Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
//Set the Bitmap into any views if you want.
// Start AsyncTask Here.
}
#Override
public void onBitmapFailed() {
}
}
private void someMethod() {
Picasso.with(this).load("url").into(target);
}
Related
I have a BackgroundImageActivity that load background image using Picasso. But when I go back to home (onStop() called in BackgroundImageActivity) and go into another instance of this activity, which is supposed to load another background image, but at first 2 seconds, it still shows the image from previous BackgroundImageActivity. Is this some kind of cache?
How I can clear this image so that whenever I go into a new instance of BackgroundImageActivity, i don't see the image from previous one?
public class BackgroundImageActivity extends Activity {
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initBackground();
}
#Override
protected void onStop() {
super.onStop();
Picasso.invalidate(imageUrl, getApplicationContext());
}
private void initBackground() {
...
}
private void setBg(final String imageUrl, final int bg) {
this.imageUrl = imageUrl;
final RequestCreator picassoRequest = Picasso.load(imageUrl, bg).memoryPolicy(MemoryPolicy.NO_CACHE);
targetReference = new Target() {
#Override
public void onBitmapLoaded(final Bitmap bitmap, final Picasso.LoadedFrom from) {
...
}
#Override
public void onBitmapFailed(final Drawable errorDrawable) {
...
}
#Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
...
}
};
picassoRequest.placeholder(bg).error(bg).into(targetReference);
}
}
Thanks!!!
Use this piece of code while loading the image.
Picasso.with(getContext())
.load(data.get(pos)
.getFeed_thumb_image())
.memoryPolicy(MemoryPolicy.NO_CACHE)
.into(image);
Picasso set the image to ImageView directly without caching in the background. This makes the app lightweight also.
I want before render MainActivity check what all pictures is loaded:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
getBitmapsByURLs(urls);
}
public void getBitmapsByURLs(List<String> urls) {
final List<Target> targets = new ArrayList<>();
for (int i = 0; i < urls.size(); i++) {
final int k = i;
Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.i("TEMP", "Loaded: " + k);
targets.remove(this);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
targets.remove(this);
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
Log.i("TEMP", "Preparing: " + k);
}
};
targets.add(target);
Picasso.with(this)
.load(urls.get(i))
.memoryPolicy(NO_CACHE, NO_STORE)
.into(target);
}
}
How I can check inside onCreate what all pictures is loaded?
If I write while on method onCreate (after call getBitmapsByURLs) then it will be called before all pictures are loaded.
You must be known that when you use
Picasso.with(this)
.load(urls.get(i))
.memoryPolicy(NO_CACHE, NO_STORE)
.into(target);
to load a picture,it is a asynchronized task.First picasso download the picture from internet.After downloading is finished,the picture will be loaded into target.Of course,when downloading,the main thread will never be waiting.Picasso use something like Observer-Subcriber or interface Future to make the main thread informed that downing is over.Above all,no matter what you write after getBitmapsByURLs(urls),it will be called before picasso finishes loading pictures.(Because it usually costs some more time to download pictures and load them into targets).
What's more.This is not a recommended way to do such a time-costing work in onCreate().Because onCreate() is called in main thread.If you do something costs much time here,then you will get an Application Not Responding error.
I'm trying to use an IntentService for processing and uploading images that's running in a different process to have more memory. I'm Using also Picasso to load the Image. When the Image is small the bitmap is loaded successfully and uploaded, however if the image is big the IntentService is terminated before Picasso is done loading It.
Picasso have to run on UIThread
Here is the code.
private void downloadImage(File file) {
final Uri uri = Uri.fromFile(file);
Handler uiHandler = new Handler(Looper.getMainLooper());
uiHandler.post(new Runnable() {
#Override
public void run() {
Picasso.with(NewImageProcessingService.this).load(uri).transform(new ImageLoadingUtil.DecreaseQualityTransformation(imageQuality)).into(NewImageProcessingService.this);
}
});
}
#Override
protected void onHandleIntent(Intent intent) {
File file = (File) intent.getSerializableExtra(KEY_IMAGE_FILE);
imageQuality = ImagesUtils.IMAGE_QUALITY
.values()[intent.getIntExtra(IMAGE_QUALITY, ImagesUtils.IMAGE_QUALITY.DEFAULT.ordinal())];
downloadImage(file);
}
This question is quite old, but if anyone steps by. The Target is getting garbage collected before it can show the bitmap.
Use it like this
public class BitmapLoader {
public static Target getViewTarget(final OnImageLoadingCompleted onCompleted) {
return new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
onCompleted.imageLoadingCompleted(bitmap);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
}
}
You need to have a strong reference to the Target so have a field in your IntentService holding it e.g.
private Target viewTarget;
viewTarget = BitmapLoader.getViewTarget(bitmap -> {
// do stuff with the bitmap
});
new Handler(Looper.getMainLooper()).post(() -> Picasso.with(getApplicationContext()).load(object.getImageUrl()).into(viewTarget));
I want to run 2 method simultaneously. However it show only the output of second method. Can you help me?
ImageView imgView;
ImageView imgView2;
public void loadImageToImageView(){ imgView.setImageBitmap(currentBitmap);}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void DrawLetter() {
new AsyncTask<Void, Void, Bitmap>() {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Bitmap doInBackground(Void... params) {
Draw();
Draw2();
return currentBitmap;
}
#Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if(bitmap!=null) {
loadImageToImageView();
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
I'm assuming you're running this task twice. But the two times are going to load the result into the same image view. That means whichever finishes first will be overwritten by the one that finishes second. If you want to see both, you need to use two separate image views.
I'm trying to clear the cache memory of Picasso via Android coding.
Can anyone please help me in this issue..?
I have tried using the following code, but this was not useful in my case:
Picasso.with(getActivity()).load(data.get(pos).getFeed_thumb_image()).skipMemoryCache().into(image);
Use this instead :
Picasso.with(getContext()).load(data.get(pos).getFeed_thumb_image()).memoryPolicy(MemoryPolicy.NO_CACHE).into(image);
Remove cache of Picasso like this.
public class Clear {
public static void clearCache (Picasso p) {
p.cache.clear();
}
}
This util class can clear the cache for you. You just have to call it:
Clear.clearCache(Picasso.with(context));
EDIT:
The class Clear must be in the package :
package com.squareup.picasso;
Because cache is not accessible from outside that package.
Like in this answer: https://stackoverflow.com/a/23544650/4585226
if you are trying to load an image through Json(from db) try clearing the networkCache for a better result.
Picasso.with(context).load(uri).networkPolicy(NetworkPolicy.NO_CACHE)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.placeholder(R.drawable.bv_logo_default).stableKey(id)
.into(viewImage_imageView);
Instead of clearing the complete cache if one wants to refresh the image with the given Uri. try this Picasso.with(context).invalidate(uri); it internally removes the key from the cache maintained by Picasso.
Excerpt from Picasso.java
/**
* Invalidate all memory cached images for the specified {#code uri}.
*
* #see #invalidate(String)
* #see #invalidate(File)
*/
public void invalidate(Uri uri) {
if (uri == null) {
throw new IllegalArgumentException("uri == null");
}
cache.clearKeyUri(uri.toString());
}
When activity destroy, unfortunately bitmap was not recycled if we're using Picasso. I try to programmatically recycle bitmap, what's loaded in to image view. There is a way to reference to loaded bitmap by using Target.
Target mBackgroundTarget = new Target() {
Bitmap mBitmap;
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
if (bitmap == null || bitmap.isRecycled())
return;
mBitmap = bitmap;
mBgImage.setImageBitmap(bitmap);
mHandler.post(new Runnable() {
#Override
public void run() {
// Do some animation
}
});
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
recycle();
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
/**
* Recycle bitmap to free memory
*/
private void recycle() {
if (mBitmap != null && !mBitmap.isRecycled()) {
mBitmap.recycle();
mBitmap = null;
System.gc();
}
}
};
And when Activity destroy, I call onBitmapFailed(null) to recycle loaded bitmap.
#Override
protected void onDestroy() {
super.onDestroy();
try {
if (mBackgroundTarget != null) {
mBackgroundTarget.onBitmapFailed(null);
Picasso.with(context).cancelRequest(mBackgroundTarget);
}
} catch (Exception e) {
e.printStackTrace();
}
}
But remember, DON'T CACHE IMAGE IN MEMORY by this case, It will cause Use recycled bitmap exception.
Picasso.with(context)
.load(imageUrl)
.resize(width, height)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.into(mBackgroundTarget);
Hope this help.
If you keep reference of your custom Downloader implementation you can clear cache.
public class PicassoUtil {
private static Picasso sInstance;
private static OkHttp22Downloader sDownloader;
public static Picasso getPicasso(Context context){
if(sInstance == null) {
sDownloader = new OkHttp22Downloader(context)
Picasso.Builder builder = new Picasso.Builder(context);
builder.downloader(sDownloader);
sInstance = builder.build(sDownloader);
}
return sInstance;
}
public static void clearCache(){
if(sDownloader != null){
sDownloader.clearCache();
}
}
}
It is important to have access to your http client and its Cache. In my implementation there is access to the cache, hence clearing cache with clearCache() method.
i had the same problem.
It worked for me.
I used Picasso in RecycleView inside a dialog. When i closed dialog, picasso doesnt clear cache. But while you are using the dialog it clears image cache. However there is some cache that is not cleared. Maybe the cache that was not cleared is the last you seen in dialog before dialog.dismiss().
use this
memoryPolicy(MemoryPolicy.NO_CACHE,MemoryPolicy.NO_STORE)
Picasso.with(activity).load(file).resize(100,100).centerCrop().memoryPolicy(MemoryPolicy.NO_CACHE,MemoryPolicy.NO_STORE).into(contactImage, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
}
});
Picasso.with(this.getContext()).load(gamePlayer.getPlayerProfileUrl()).skipMemoryCache().into(iv);
This also works