I want to load images asynchronously while scrolling the recycler view. For this I used picasso to load image from an url. Also I am using a rounded image view for the image.
Now when I scroll up and down first it takes 10 - 12 seconds to load the images. And when I scroll it gives out of memory error on rounded image view.
contactAdapter:
public class ContactAdapter extends RecyclerView.Adapter<FeedListRowHolder> {
private List<FeedItem> feedItemList;
private Context mContext;
public ContactAdapter(Context context, List<FeedItem> feedItemList) {
this.feedItemList = feedItemList;
this.mContext = context;
}
@Override
public FeedListRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_layout,null);
FeedListRowHolder mh = new FeedListRowHolder(v);
return mh;
}
@Override
public void onBindViewHolder(FeedListRowHolder feedListRowHolder, int i) {
final FeedItem feedItem = feedItemList.get(i);
Log.e("Imagename",""+"http://xesoftwares.co.in/contactsapi/profile_images/85368a5bbd6cffba8a3aa202a80563a2.jpg");//+feedItem.getThumbnail());
/* Picasso.with(mContext).load("http://xesoftwares.co.in/contactsapi/profile_images/85368a5bbd6cffba8a3aa202a80563a2.jpg")
.error(R.drawable.ic_account_circle_black_24dp)
.placeholder(R.drawable.ic_account_circle_black_24dp)
.into(feedListRowHolder.thumbnail)
;*/
Picasso.with(mContext).load("http://xesoftwares.co.in/contactsapi/profile_images/85368a5bbd6cffba8a3aa202a80563a2.jpg")
.error(R.drawable.ic_account_circle_black_24dp)
.placeholder(R.drawable.ic_account_circle_black_24dp)
.into(new Target() {
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
try {
String root = Environment.getExternalStorageDirectory().getPath();
File myDir = new File(root +"/Contact");
if (!myDir.exists()) {
myDir.mkdirs();
}
// String name = new Date().toString();
String name = new Date().toString()+".jpg";
File myDir1 = new File(myDir, name);
FileOutputStream out = new FileOutputStream(myDir1);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
ImageFilePath imageFilePath1=new ImageFilePath(myDir1);
ComplexPreferences complexPreferences112 = ComplexPreferences.getComplexPreferences(mContext, "mypref112", Context.MODE_PRIVATE);
complexPreferences112.putObject("imageFilePath1", imageFilePath1);
Log.e("user2", "" + imageFilePath1);
complexPreferences112.commit();
out.flush();
out.close();
} catch(Exception e){
// some action
}
}
public void onBitmapFailed(Drawable errorDrawable) {
}
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
ComplexPreferences complexPreferences112 = ComplexPreferences.getComplexPreferences(mContext, "mypref112", mContext.MODE_PRIVATE);
ImageFilePath imageFilePath1= complexPreferences112.getObject("imageFilePath1", ImageFilePath.class);
File myDir1=imageFilePath1.getprofile();
Picasso.with(mContext).load(myDir1).into(feedListRowHolder.thumbnail);
feedListRowHolder.title.setText(Html.fromHtml(feedItem.getTitle()));
//feedListRowHolder.genre.setText(Html.fromHtml(feedItem.getGenre()));
}
@Override
public int getItemCount() {
return (null != feedItemList ? feedItemList.size() : 0);
}
}
roundedImageView :
public class RoundedImageView extends ImageView {
public RoundedImageView(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
if (drawable == null) {
return;
}
if (getWidth() == 0 || getHeight() == 0) {
return;
}
Bitmap b = ((BitmapDrawable)drawable).getBitmap() ;
if(b!=null) {
Bitmap bitmap = b.copy(Config.ARGB_4444, true); //error line 39
int w = getWidth() / 2;
Bitmap roundBitmap = getRoundedCroppedBitmap(bitmap, w);
canvas.drawBitmap(roundBitmap, 0, 0, null);
}
}
public static Bitmap getRoundedCroppedBitmap(Bitmap bitmap, int radius) {
Bitmap finalBitmap;
if(bitmap.getWidth() != radius || bitmap.getHeight() != radius)
finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius, false);
else
finalBitmap = bitmap;
Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(),
finalBitmap.getHeight(), Config.ARGB_4444);
Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, finalBitmap.getWidth(), finalBitmap.getHeight());
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.parseColor("#BAB399"));
canvas.drawCircle(finalBitmap.getWidth() / 2+0.7f, finalBitmap.getHeight() / 2+0.7f,
finalBitmap.getWidth() / 2+0.1f, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(finalBitmap, rect, rect, paint);
return output;
}
}
Error:
java.lang.OutOfMemoryError
at android.graphics.Bitmap.nativeCopy(Native Method)
at android.graphics.Bitmap.copy(Bitmap.java:556)
at com.xesc.contacts.utils.RoundedImageView.onDraw(RoundedImageView.java:39)
How can I solve this out of memory error? I tried to use .fit() in picasso but it gives illegalSateException. Also it takes at least 10 seconds to load the image how can I reduce the time of loading the image?
Thank you..
EDIT : The code after .into() dose not work, it directly jumps on complexShared preferences. And as complex sharedPreferences remains null it is throwing an null pointer exception.
This happened when I uninstalled the app and run again. Before it was running well.