Picasso Library Not Loading Images in Android 5.0 - android

I have a ListView extending ArrayAdapter. I am using a view holder patter for the getView and here is a part of my getView method which loads images using picasso. The following code is expected to load images into my image view inside every list item.
Picasso.with(mContext).load(imageURL).fit().into(holder.myImageView,
new Callback(){
#Override
public void onError() {
holder.myImageView.setVisibility(View.INVISIBLE);}
#Override
public void onSuccess() {}});
So here is the problem: this works fine for OS < Android 5.0, but in the case of Android 5.0 (Lollipop), it looks like Picasso is fetching these images when my app is installed and run for the very first time, but when I launch the app again, the images don't load. Not at all sure what the problem is. I am not loading huge images, you can assume all the images that I am loading are only of the size of a small icon/thumbnail (around 120X120). I am using Picasso 2.4.0 for my application and the phone that I am using for testing is the Nexus 4.
Open Issue as of this edit: https://github.com/square/picasso/issues/633
Alternative:
I struggled to find an answer for very long time yesterday night. I explored Picasso really intimately trying several stuff. None did work. So right now what I am doing is:
if(api>=21) //Keeping in mind about any updates beyond 21
Use Android-Universal-Image-Loader
else
Use Picasso
For more information on Andorid Universal Image Loader.
Please visit https://github.com/nostra13/Android-Universal-Image-Loader
Its awesome with its cusotmization.
Agian, dont get me wrong, I am 100% +ve there is a solution with Picasso, I am sure I will find it someday, I will post it here then, but for anyone who have problems like me, I think the above is the way to go. Please suggest if you have anuy other better way.
************************ ISSUE FIXED **************************
https://github.com/square/picasso/issues/632
https://github.com/square/picasso/pull/860
************************** ISSUE FIXED ****************************

Related

How to best load images from URL in Android ListView

So I have been struggling with the best way to load images in a ListView in Android for a while.
The images come from a server, so can take some time to load. From what I understand there are only 2 ways to implement this:
1 - Load the images on the main thread - This leads to have the correct images display immediately when the view displays and scrolls, but gives poor scrolling performance and can lead to the app hanging and crashing.
2 - Load the images in an AsyncTask. This means the images will be blank when the list display or scroll, but eventually display. Because of caching done by the list, you can also get the wrong image for an item. But this gives good performance and scrolling, and does not hang/crash.
Seems like neither solution works correctly. There must be a solution that works?? I have seen other posts like this, but the answer seems to always be 1 or 2, but neither is a good solution...
My code for the list adapter is below.
The HttpGetImageAction.fetchImage() method either executes an async task, or fetches the image on the main thread depending on a flag I set. I also cache the images locally and on disk once they have been loaded, so the issue mainly occur the first time (but the cache is only for 100 images, and the list has 1000s)
public class ImageListAdapter extends ArrayAdapter<WebMediumConfig> {
Activity activity;
public ImageListAdapter(Activity activity, int resourceId, List<WebMediumConfig> items) {
super(activity, resourceId, items);
this.activity = activity;
}
class ImageListViewHolder {
ImageView imageView;
TextView nameView;
TextView descriptionView;
TextView statView;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageListViewHolder holder = null;
WebMediumConfig config = getItem(position);
LayoutInflater mInflater = (LayoutInflater)this.activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.image_list, null);
holder = new ImageListViewHolder();
holder.imageView = (ImageView)convertView.findViewById(R.id.imageView);
holder.nameView = (TextView) convertView.findViewById(R.id.nameView);
holder.descriptionView = (TextView)convertView.findViewById(R.id.descriptionView);
holder.statView = (TextView)convertView.findViewById(R.id.statView);
convertView.setTag(holder);
} else {
holder = (ImageListViewHolder) convertView.getTag();
}
holder.nameView.setText(Utils.stripTags(config.name));
holder.descriptionView.setText(Utils.stripTags(config.description));
holder.statView.setText(config.stats());
if (MainActivity.showImages) {
HttpGetImageAction.fetchImage(this.activity, config.avatar, holder.imageView);
} else {
holder.imageView.setVisibility(View.GONE);
}
return convertView;
}
}
You should never load images on the main thread. Any network calls should be done asynchronously and should not be done on the main thread. Your images might load quickly (on the main thread) because you may have a good internet connection, have small images and/or have few images. But imagine what would happen if someone using your app has a slower internet and then one day your list grows to hundreds!
The approach below is a well accepted practice for loading scrollview content
a) load your text content as you load the scrollview. Again the loading should be done async but you can show a loading view until the download completes
b) Show image
placeholders while the image loads on each cell
c) Load the images
asynchronously
Using a library such as Picasso would help immensely because there is a lot of boilerplate code which you'd need to handle otherwise such as
cancelling image download when a cell is reused and
starting a new download for a reused cell
caching
retrying a failed download
Hope this helps.
Use Glide [https://github.com/bumptech/glide] or Picasso [http://square.github.io/picasso/] - both libraries are very similar, and easy to use, have caching, work in background, allow placeholders ets.
Usage is as simple as:
Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(imageView);
Simple Solution: Glide.
Glide is a fast and efficient open source media management and image
loading framework for Android that wraps media decoding, memory and
disk caching, and resource pooling into a simple and easy to use
interface. Glide supports fetching, decoding, and displaying video
stills, images, and animated GIFs.
You can load the image like:
GlideApp.with(context)
.load("http://via.placeholder.com/300.png")
.into(imageView);
Refer for more info:
https://guides.codepath.com/android/Displaying-Images-with-the-Glide-Library
https://github.com/bumptech/glide
Use Glide library.. Its recommended by Google.
Step 1:
Use latest Glide Version 4 dependency (https://github.com/bumptech/glide)
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
Step 2:
Glide.with(MainActivity.this)
.load(url)
.apply(new RequestOptions().placeholder(R.drawable.placeholder).error(R.drawable.error_image))//this line optional - you can skip this line
.into(imageview);
use picasso or Glide Library
What these library do , is that they image url from u and download them and save it in cache so that whenever you open the same image another time it will load faster and it prevents the usage of the network
Picasso
http://square.github.io/picasso/
Glide
https://github.com/bumptech/glide
You can try UnivarsalImageLoader which is quite Fast and Optimised.
It supports Multithread image loading (async or sync) and high level of customization.
https://github.com/nostra13/Android-Universal-Image-Loader
Here's how I do it
First I always create the RecyclerView.ViewHolder to which I pass a Func<int, Bitmap> image_provider (or you can pass some sort of lamda?) which when called will retrieve the image for a specific Id, if it can't it will show the default loading image
Everything is stored in a fragment (which I call a DataFragment) which has retainstate set to true.
First load a list of json objects that tell me which images I must show, this happens inside the data fragment so it will go on if there is a configuration change
I send that info to the adapter using a method like SetItems(MyImages)
Then I start a new thread that will load the images (while allowing the user to work)
I do this with the TPL library, the closest approximation for Android is Anko Async but you can also do it with AsyncTask, the problem is that I send a lot of messages to the main thread, so to do it with AsyncTask you have to give it a handler to the main thread which will send messages.
In the thread I loop through all the images I have to download, and download them and send a message to the DataFragment which sends it to the currently attached Activity which triggers the NotifyItemChanged method in the adapter
The adapter on creation receives a Func<int, Bitmap> image_provider which it invokes and retrieves the image from memory
I know it sounds a bit messy, so if you want code examples I can give them , but they are in C#
In your app level gradle implement below repository:
implementation 'com.squareup.picasso:picasso:2.5.2'
Put below line where you want to load image
Picasso.with(getApplicationContext()).load(config.avatar).into(imageview);
just use Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(imageView); or
Glide.with(getApplicationContext()).load("image_url").asBitmap().centerCrop().into(new BitmapImageViewTarget(imageView)
in adapter like any view and done no extra efforts required

How download all pictures at the start of mediaManager?

I have been working with DJI Mobile SDK for Android, making my own application by following this tutorial: https://developer.dji.com/mobile-sdk/documentation/android-tutorials/MediaManagerDemo.html
But i have to download all pictures at once at the start of my gallery, and i coudn't manage how to do it.
Is there any example showing how to do it?
I think it should happen at MainActivity.java from the MediaManagerDemo tutorial. Should i create getFiles() and put it alongside getThumbnails and getPreviews (on line 317)?
scheduler.resume(new CommonCallbacks.CompletionCallback() {
#Override
public void onResult(DJIError error) {
if (error == null) {
getThumbnails();
getPreviews();
}
}
});
Should i create a Callback for this? What else should i look out?
Thank you all in advance!
EDIT: I stopped and thinked calmly about this, and managed to solve this by creating a downloadAllFiles() method after getPreviews(), where it checks if the connection was succeed and calls downloadFileByIndex for each one of the items. I also implemented a way to check if there is already a file with the exact size on internal storage, to avoid unnecessary downloads.
If anyone is interested, heres my code on Github
Have you looked at the section in the tutorial on Downloading and Editing the Media files?

JavaFX ScrollPane on Android - Too Slow

I have just coded a simple example of using a scrolling pane in JavaFX. Just one ScrollPane placed in the Scene, and the ScrollPane holds a Label component with a large text. Using gradle I have uploaded the app on a Nexus 4 android device. As you can see from the video I have uploaded, the scrolling is way too slow. I am sure others have experienced this. Any suggestion of how this can be changed to the native speed scroll is really much appreciated.
Source code of the app can be downloaded from here.
AndroidFX.java
public class AndroidFX extends Application{
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Parent mySearchListFXML = getFXMLPane("/fxml/ScrollPaneWithLabel.fxml");
primaryStage.setScene(new Scene(mySearchListFXML));
primaryStage.setWidth(Screen.getPrimary().getVisualBounds().getWidth()); primaryStage.setHeight(Screen.getPrimary().getVisualBounds().getHeight());
primaryStage.show();
}
public static Parent getFXMLPane(String url) throws IOException {
URL location = AndroidFX.class.getResource(url);
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(location);
Parent pane = fxmlLoader.load();
return pane;
}
}
ScrollPaneWithLabel.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<ScrollPane fx:id="scrollPane" fitToWidth="true" hbarPolicy="NEVER" pannable="true" style="-fx-background: white;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<content>
<Label text="<VERY LARGE TEXT HERE>" wrapText="true" />
</content>
</ScrollPane>
Faced similar issues. The problem is worse on hidpi devices, disabling the hipdpi setting in the properties file helps but you will have to do any scaling your self for hidpi screens.
Another thing that might help is caching the fonts. I believe the end result of a javafx ported application is more like a big image with hot spots. Scrolling I think is just telling the application to redraw the whole image every time. Just a theory really. Caching does help but can make your text look kinda fuzzy. Although I had some success in scaling the font large, setting the cache to true and then scale down, normally x2 and .5 gives pretty good results. Using em for the fonts helps the fuzzy text too.
If you need to show a huge amount of text and it must be scrolled, you will probably get much better results displaying it through a webview. I haven't ever really used it to be honest, but I would imagine that it is replaced when ported with the native browser window so performance should be pretty good I think. I guess it is possible they wrote a html renderer in javafx and then you would run into the same pitfalls.
http://www.gluonhq.com is offering something called the charm, down, and connect package that is supposed to mimic native apps. It sounds promising and I am currently trying to get more info to see if they have solutions to these types of issues. I will add to the post if I receive any more info about charm.

Images randomly break in PhoneGap/WebView on Android 4.4

I am experiencing a strange bug in PhoneGap on Android 4.4, for which I couldn't find any solution online. In my app, I am loading a lot of different images from a remote server, and as the user navigates back and forth, new images are loaded on each page (4 at a time, to be specific, through jQuery-generated html). After having navigated back and forth for a little while, some images will randomly not show up and instead show the typical "broken image" icon.
Now, here comes the strange part: I have been following the instructions at jQuery/JavaScript to replace broken images and done a few tests of my own. In conclusion, the naturalWidth and naturalHeight parameters report the right sizes of the images, and complete reports true for all images. Therefore, the solutions mentioned in the above SO thread don't work at all. Changing the image src doesn't help, either with or without a setTimeout (I tried adding the current timestamp as a parameter to the image path as well).
Did anyone else encounter this issue at all, or am I going crazy here? :)
EDIT: By the way, no error is ever reported. Therefore, no error handler is called when loading the image, making it useless to solve the problem with the already suggested methods (see the link above).
This is how i handle error images,
<img src="images/imageName.jpg" onError="onErrorFunc(this);" alt=" " />
function onErrorFunc(elem){
var imgUrl = "https://alternative-image";
elem.onerror = function (){
elem.src='images/noimage.jpg';
}
elem.src=imgUrl;
}
Hope it helps!

Android Picasso - How to obtain stats on cache hits and downloads

I'm currently using Picasso 2.0.1(also tried 1.0.2 before) and obtaining bitmaps from pictures on the web.
All is working great, I've seen improvements in loading the images ... at least it seems faster.
My question is, how can I get statistics from the activities done by PICASSO? I wanted to know if the picture was obtained from the cache or downloaded ...
I'm trying to obtain information with com.squareup.picasso.StatsSnapshot, however it doesn't seem to get updated... or I'm not using it correctly.
Picasso pi = Picasso.with(getActivity().getApplicationContext());
Bitmap bitmap = pi.load(url.toString()).get();
Log.d(this.getClass().getSimpleName(),"Cache hits:" + pi.getSnapshot().cacheHits + " Cache misses:" + pi.getSnapshot().cacheMisses);
adding a log before and / or after the load call always return the same result
Cache hits:0 Cache misses:0
What am I doing wrong or how can I obtain this information?
Thanks in advance!
Marc
To get the colored triangle that David Hewitt is describing, you actually have to use setIndicatorsEnabled like so
Picasso.with(mContext).setIndicatorsEnabled(true);
You can get stats in your logs for Picasso by using setLoggingEnabled like so
Picasso.with(mContext).setLoggingEnabled(true);
You can search the log with a "Picasso" filter and see where Picasso gets the image and how long it takes. Very handy!
According to the website here:
http://square.github.io/picasso/
You can do setDebugging(true) and it will place coloured triangles in the corner or each image that will indicate whether they were loaded from the web, the disk or memory. I can't find any specific reference to the functions you were using on the website, but this may meet your needs instead.
You can call this from the onStop() or onPause of your Activity or Fragment
StatsSnapshot picassoStats = Picasso.with(context).getSnapshot();
Log.d("Picasso stats ", picasspStats.toString());
then from the Android logcat select verbose and filter by Picasso.
You will get a log something like the following:
Picasso stats: [main] StatsSnapshot{maxSize=76695844, size=75737296, cacheHits=656, cacheMisses=1091, downloadCount=8, totalDownloadSize=213376, averageDownloadSize=26672, totalOriginalBitmapSize=437547196, totalTransformedBitmapSize=609434304, averageOriginalBitmapSize=527801, averageTransformedBitmapSize=735143, originalBitmapCount=829, transformedBitmapCount=826, timeStamp=1484426664382}
'get()' is synchronous and skips the cache. Use one of the 'load()' methods to harness Picasso's full powers.

Categories

Resources