I want to load the images while recycler view is scrolling. For this I am using picasso.
I am creating a file to store the image inside the picasso. But it is not getting created. When I tried to debug I found that it dose not run the code after .into(), it directly goes down to the complesPreferences code.
So as complexPreferences left null it is giving the null pointer exception.
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(feedItem.getprofileImage())
.error(R.drawable.ic_account_circle_black_24dp)
.placeholder(R.drawable.ic_account_circle_black_24dp)
.resize(100,100)
.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);
}
}
Can anyone help with this? Why it's not working.. Thank you..
EDIT : I tried like this:
Still its not getting called I think because still getting null pointer exception on complexPreferences.
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());
Target target = new Target () {
// your code here ...
@Override
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
}
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
feedListRowHolder.thumbnail.setTag(target);
Picasso.with(mContext).load(feedItem.getprofileImage())
.error(R.drawable.ic_account_circle_black_24dp)
.placeholder(R.drawable.ic_account_circle_black_24dp)
.resize(100,100)
.into(target);
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);
}
}