Checking if imageView is empty with Picasso - android

I have database table, there is a column "image_link", so when I open activity, class Picasso get this image_link and show me image. But now I also may have path to image in my column instead of imageLink, but I need show image in any case, I think that it will work if I will do something like that:
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
Picasso.get().load(favouriteTemplates.get(position).getImageLink()).into(holder.icon);
if(holder.icon.getDrawable() == null){
Picasso.get().load(new File(favouriteTemplates.get(position).getImageLink())).into(holder.icon);
}
}
But I know, that it takes some time to download image into imageview with Picasso, so my "if" always is true. How can I solve it?

Picasso.get()
.load(favouriteTemplates.get(position).getImageLink())
.into(holder.icon),
new Callback() {
#Override
public void onSuccess() {
//Do Nothing
}
#Override
public void onError(Exception e) {
Picasso
.get()
.load(newmFile(favouriteTemplates.get(position).getImageLink()))
.into(holder.icon);
}
}
);

Related

How to Load many images in Recycle View without Problem in android

im using Glide library to load my images from storage to imageView using RecyclerView
all thing good
but when number of images be alot app crashed or should be wait so much to activity load completely
this is my code
public static File DIR_IMG = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "MyHairSamples");
private void readExistingImages(){
File[] images;
try {
images= DIR_IMG.listFiles();
for (int i = 0; i < images.length; i++) {
if (images[i].isFile() && images[i].length() > 50L) {
Bitmap b = BitmapFactory.decodeFile(images[i].getAbsolutePath());
addImage(b, "5", images[i].getName());
}
else{
images[i].delete();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
and i using this method in onCreate method
and this is my Adapter Code
#Override
public void onBindViewHolder(#NonNull MyViewHolder viewHolder, int i) {
viewHolder.title.setText(arrayList.get(i).getImageTitle());
viewHolder.thumbnail.setLabelFor(i);
Glide.with(context)
.load(arrayList.get(i).getImage())
.into(viewHolder.thumbnail);
}
When the images resolution is big, loading will be slow and even your application will crash.
So you can resize your image set like this
Glide.with(context)
.load(arrayList.get(i).getImage())
.apply(new RequestOptions().override(400, 400))
.into(viewHolder.thumbnail);
EDIT
RequestOptions reqOpt = RequestOptions
.fitCenterTransform()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.override(300,300)
Load images inside the uithread, it is not interrupt the main thread.
Use below code.
context.runOnUiThread(new Runnable() {
#Override
public void run() {
Glide.with(context)
.load(arrayList.get(i).getImage())
.into(viewHolder.thumbnail);
}
});

How can I keep pictures in memory with Picasso?

I'm viewing some pictures using recyclview with Picasso.
These images are a bit too much to recompile in recyclerview and when I go up again, it reloads the pictures, and if the pictures are too big, or the network access is bad, it does not look nice.
I wonder how can I cache the pictures I called with loadUrl with Picasso. Is this possible with Picasso?
As an example I can give Instagram. Every time I go back to another activity, it loads the picture again and affects the performance of the application. If you teach me how to keep the pictures in memory, or if you show me a source, I'm happy. Thanks in advance.
My Code:
adapter = new FirebaseRecyclerAdapter<WallPaperItem, ListWallPaperViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull final ListWallPaperViewHolder holder, int position, #NonNull final WallPaperItem model) {
Picasso.with(getBaseContext())
.load(model.getImageLink())
.networkPolicy(NetworkPolicy.OFFLINE)
.into(holder.wallpaper, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Picasso.with(getBaseContext())
.load(model.getImageLink())
.error(R.drawable.ic_wallpaper_black_24dp)
.into(holder.wallpaper, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Log.e("ERROR","None");
}
});
}
});
holder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position) {
Intent intent = new Intent(ListWallPaper.this,ViewWallPaper.class);
Common.selected_background = model;
Common.select_background_key = adapter.getRef(position).getKey();
startActivity(intent);
}
});
why you use
.into(holder.wallpaper, new Callback() {}
and check new callback
try this
Picasso.get().load(model.getImageLink())
.networkPolicy(NetworkPolicy.OFFLINE)
.placeholder(R.drawable.user_placeholder)
.error(R.drawable.ic_wallpaper_black_24dp)
.into(holder.wallpaper);
maybe it help

Picasso Not Loading Images From Server

My problem statement is to load the images from the server.
Now I have used picasso inside the custom adapter.
The image which I am downloading its size is approximately 100kb
not more than that.But instead of displaying the image white background is
been displayed.
To check error I have used callback but somehow the callback is not working
I mean the callback is never getting called.
Below is my code
#Override
public void onBindViewHolder(FolderListAdapter.Viewholder holder, int position) {
userDetails=userList.get(position);
//Context context=);
if(userDetails.getLogo()!=null && !userDetails.getLogo().isEmpty()) {
Context context=holder.cardProfilePic.getContext();
Picasso.with(context)
.load(userDetails.getLogo())
.fit()
.into(holder.cardProfilePic, new Callback() {
#Override
public void onSuccess() {
Log.d("succes","success");
}
#Override
public void onError() {
Log.d("failure","failure");
}
});
}
if(userDetails.getBanner()!=null && !userDetails.getBanner().isEmpty()) {
Picasso.with(context)
.load(userDetails.getBanner())
.resize(300,300)
.into(holder.cardBackgroundPic);
}
if(userDetails.getPersonName()!=null && !userDetails.getPersonName().isEmpty()) {
holder.username.setText(userDetails.getPersonName());
}
if(userDetails.getDesignation()!=null && !userDetails.getDesignation().isEmpty()) {
holder.userRole.setText(userDetails.getDesignation());
}
}

Images are not stored in the cache

I have this method, everything is worked perfectly but images always got from server and not load from cache! what happened ?
public static void makeImageRequest(String Unique_ID, final View parentView, final int id) {
String url = FILE_UPLOAD_FOLDER + Unique_ID + ".png";
final int defaultImageResId = R.drawable.user;
// Retrieves an image specified by the URL, displays it in the UI.
ImageCacheManager.getInstance().getImage(url, new ImageListener() {
#Override
public void onErrorResponse(VolleyError error) {
ImageView imageView = (ImageView) parentView.findViewById(id);
imageView.setImageResource(defaultImageResId);
}
#Override
public void onResponse(ImageContainer response, boolean isImmediate) {
if (response.getBitmap() != null) {
ImageView imageView = (ImageView) parentView.findViewById(id);
imageView.setImageBitmap(response.getBitmap());
} else if (defaultImageResId != 0) {
ImageView imageView = (ImageView) parentView.findViewById(id);
imageView.setImageResource(defaultImageResId);
}
}
});
}
Just use Picasso instead ImageCacheManager. Picasso is a powerful image downloading and caching library for Android. Images add much-needed context and visual flair to Android applications. Picasso allows for hassle-free image loading in your application—often in one line of code!
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Here also can manage whether the image is successfully downloaded or it fails:
Picasso.with(context)
.load("http://i.imgur.com/DvpvklR.png")
.into(imageView, new Callback() {
#Override
public void onSuccess() {
// your code
}
#Override
public void onError() {
// your code
}
});
You should only add this line in your gradle:
compile 'com.squareup.picasso:picasso:2.5.2'
Hope it helps!

Picasso not loading bitmap into imageview with target

I'm trying to load an url into an ImageView but Target doesn't seem to work like what I found told me. Below is my code:
ImageView methodButton= (ImageView) View.inflate(context, R.layout.view_home_scroll_view_image, null);
setMethodPicture(sectionModel.getContent().get(0).getThumbnail(), methodButton);
methodsContainer.addView(methodButton);
and
private void setMethodPicture(final String methodPicture, final ImageView methodButton){
methodButton.setBackgroundColor(0x000000);
if (!StringUtils.isEmpty(methodPicture)) {
Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
methodButton.setImageBitmap(bitmap);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
#Override
public boolean equals(Object o) {
return methodPicture.equals(o);
}
#Override
public int hashCode() {
return methodPicture.hashCode();
}
};
Picasso.with(context).load(methodPicture).into(target);
}
}
This doesn't load the picture to the imageView, but when i do this,
ImageView methodButton= (ImageView) View.inflate(context, R.layout.view_home_scroll_view_image, null);
methodButton.setBackgroundColor(0x000000);
Picasso.with(context).load(sectionModel.getContent().get(0).getThumbnail()).into(methodButton);
methodsContainer.addView(methodButton);
it loads the picture.
I want to do the first one so I can change the Bitmap I get before I put it in the ImageView, like changing the width and height, but basing on the original dimensions.
I found the issue, Picasso holds a Weak Reference to the Target. The correct answer can be found here: onBitmapLoaded of Target object not called on first load
Use Glide instead, it provide predefine constrain options which you can add with it
https://github.com/bumptech/glide

Categories

Resources