When the ImagePager load first time, some times, Picasso call onError, showing the .error drawable. If I press on back button and go back to the Activity that has the ImagePager, Picasso load the picture correctly. If the ImagePager has two or more pictures and I swipe between the pictures, those are loaded correctly some times without exit and reenter to the ImagePager.
It correctly downloads other images from web. The issue arises when I tried to download from hosted company server.
I am using Picasso 'com.squareup.picasso:picasso:2.5.0'.
I also referred to below question but it doesn't help.
First time error loading picture with Picasso
Below is my MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
configureToolbar(R.string.select_task);
init();
}
@Override
protected void init() {
//TODO: For QA Testing Purpose, Remove after Testing
mWidthField =findViewById(R.id.edt_txt_1);
mHeightField =findViewById(R.id.edt_txt_2);
mImage=findViewById(R.id.image_view_2);
mImageLoadButton=findViewById(R.id.image_load_button);
item=new Item();
item.setPrimaryImageURL("https://cdn.cnn.com/cnnnext/dam/assets/190119161516-01-trump-government-shutdown-0119-exlarge-169.jpg");
item.setUpc("0001111086751");
Log.d("ImageManager","Main Activity");
new ImageManager(getApplicationContext()).downloadImage(item.getPrimaryImageURL(),item.getUpc()+".jpeg",imageDownloadedCallBack);
mImageLoadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadResizedImage();
}
});
}
//TODO: For QA Testing Purpose, Remove after Testing
ImageManager.ImageDownloadedCallBack imageDownloadedCallBack=new ImageManager.ImageDownloadedCallBack() {
@Override
public void imageDownloadComplete(final Bitmap bitmap, boolean status) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("ImageManager","Image Download CallBack in Main Activity");
mImage.setImageBitmap(bitmap);
}
});
}
};
Below is my ImageManager.java
public class ImageManager {
private final Context mContext;
private int mWidth;
private int mHeight;
public ImageManager(Context mContext){
this.mContext=mContext;
}
public interface ImageDownloadedCallBack {
void imageDownloadComplete(Bitmap bitmap,boolean status);
}
private Target picassoImageTarget(Context context, final String imageDir, final String imageName,final ImageDownloadedCallBack imageDownloadedCallBack) {
ContextWrapper cw = new ContextWrapper(context);
final File directory = cw.getDir(imageDir, Context.MODE_PRIVATE); // path to /data/data/yourapp/app_imageDir
return new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
Thread thread=new Thread(new Runnable() {
@Override
public void run() {
final File myImageFile = new File(directory, imageName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myImageFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
Log.d("ImageManager","Image DownLoad CallBack");
imageDownloadedCallBack.imageDownloadComplete(bitmap,true);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
thread.start();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
Log.d("ImageManager","Bitmap Failure");
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
if (placeHolderDrawable != null) {}
}
};
}
public void downloadImage(String url, String id,ImageDownloadedCallBack imageDownloadedCallBack){
// this.imageDownloadedCallBack=imageDownloadedCallBack;
Log.d("ImageManager","Download Image function");
Picasso.with(mContext).load(url).into(picassoImageTarget(mContext,"imageDir", id ,imageDownloadedCallBack));
}
}
Any help would be appreciated.