我正在回收者视图中显示联系人列表。我正在显示联系人资料图像,首先我从服务器加载这些图像,然后将这些图像存储在外部存储器中。
然后从外部存储装入图像。我可以看到加载的图像,但是当我滚动时,我可以看到一些图像一两秒然后它们消失了,我可以看到默认的图像图标。
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactHolder> {
private List<Contact> contactList;
File myDir1;
private Activity mContext;
private Boolean fileExists;
private File file;
private static final int MY_PERMISSIONS_REQUEST_CALL= 20;
public ContactAdapter(Activity context, List<Contact> contactList) {
this.contactList = contactList;
this.mContext = context;
}
@Override
public ContactHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_layout,null);
ContactHolder mh = new ContactHolder(v);
return mh;
}
@Override
public void onBindViewHolder(final ContactHolder contactHolder, int i) {
final Contact contact = contactList.get(i);
// Log.e("Imagename",""+"http://xesoftwares.co.in/contactsapi/profile_images/85368a5bbd6cffba8a3aa202a80563a2.jpg");//+feedItem.getThumbnail());
Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
// your code here ...
bitmap = Bitmap.createScaledBitmap(bitmap,(int)(bitmap.getWidth()*0.8), (int)(bitmap.getHeight()*0.8), true);
contactHolder.thumbnail.setImageBitmap(bitmap);
Log.e("ProfileImage", contact.getmProfileImage());
SaveImages(bitmap, contact.getmProfileImage());
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
contactHolder.thumbnail.setImageDrawable(errorDrawable);
// do error handling as required
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
contactHolder.thumbnail.setImageDrawable(placeHolderDrawable);
}
};
contactHolder.thumbnail.setTag(target);
String url = ServiceUrl.getBaseUrl() + ServiceUrl.getImageUserUrl() + contact.getmProfileImage();
Log.e("url",url);
if(contact.getmProfileImage().equals(""))
{
file = new File("");
fileExists = file.exists();
contactHolder.thumbnail.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_account_circle_black_48dp));
}
else {
file = new File(Environment.getExternalStorageDirectory() + "/ContactProfileImages/" + contact.getmProfileImage());
fileExists = file.exists();
}
if(fileExists)
{
Log.e("fileExists",file.getAbsolutePath());
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(file.getPath(), bmOptions);
contactHolder.thumbnail.setImageBitmap(bitmap);
}
else {
Log.e("Picasso",file.getAbsolutePath());
Picasso.with(mContext).load(url)
.error(R.drawable.ic_account_circle_black_24dp)
.placeholder(R.drawable.ic_account_circle_black_24dp)
.into(target);
}
contactHolder.title.setText(contact.getmUserName());
//feedListRowHolder.genre.setText(Html.fromHtml(feedItem.getGenre()));
}
@Override
public int getItemCount() {
return (null != contactList ? contactList.size() : 0);
}
public void SaveImages(Bitmap bitmap,String profileName)
{
try {
String root = Environment.getExternalStorageDirectory().getPath();
File myDir = new File(root +"/ContactProfileImages");
if (!myDir.exists()) {
myDir.mkdirs();
}
// String name = new Date().toString();=
String name = profileName;
File myDir1 = new File(myDir, name);
if(!myDir1.exists()) {
FileOutputStream out = new FileOutputStream(myDir1);
bitmap.compress(Bitmap.CompressFormat.PNG,100,out);
out.flush();
out.close();
}
} catch(Exception e){
// some action
}
//myDir1= imageFilePath1.getprofile();
}
public class ContactHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected CircleImageView thumbnail;
protected TextView title;
public ContactHolder(View view) {
super(view);
this.thumbnail = (CircleImageView) view.findViewById(R.id.thumbnail);
this.title = (TextView) view.findViewById(R.id.title);
view.setOnClickListener(this);
}
@Override
public void onClick(View v) {
final Contact contact = contactList.get(getAdapterPosition());
final Dialog dialog = new Dialog(mContext);
dialog.setCanceledOnTouchOutside(true);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom);
final Window window = dialog.getWindow();
WindowManager.LayoutParams wlp =window.getAttributes();
wlp.gravity = Gravity.CENTER_HORIZONTAL|Gravity.TOP;
wlp.y=320;
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setAttributes(wlp);
// set the custom dialog components - text, image and button
TextView txtusername = (TextView) dialog.findViewById(R.id.txtusername);
TextView txtmobile = (TextView) dialog.findViewById(R.id.txtmobile);
TextView txtemail = (TextView) dialog.findViewById(R.id.txtemail);
txtusername.setText(contact.getmUserName());
txtemail.setText(contact.getmEmailId());
txtmobile.setText(contact.getmMobileNo());
SquareImageView image = (SquareImageView) dialog.findViewById(R.id.image);
ImageView image1 = (ImageView) dialog.findViewById(R.id.image1);
ImageView image2 = (ImageView) dialog.findViewById(R.id.image2);
ImageView image3 = (ImageView) dialog.findViewById(R.id.image3);
if(contact.getmProfileImage().equals(""))
{
image.setImageDrawable(ContextCompat.getDrawable(mContext,R.drawable.profile_icon));
}
else {
File file = new File(Environment.getExternalStorageDirectory() + "/ContactProfileImages/" + contact.getmProfileImage());
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(file.getPath(), bmOptions);
image.setImageBitmap(bitmap);
}
image1.setImageResource(R.drawable.ic_call_black_24dp);
image2.setImageResource(R.drawable.ic_textsms_black_24dp);
image3.setImageResource(R.drawable.ic_email_black_24dp);
image2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uri sms_uri = Uri.parse("smsto:" + contact.getmMobileNo());
Intent sms_intent = new Intent(Intent.ACTION_SENDTO, sms_uri);
mContext.startActivity(sms_intent);
dialog.dismiss();
}
});
image1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + contact.getmMobileNo()));
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
mContext.startActivity(intent);
dialog.dismiss();
}
});
image3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{contact.getmEmailId()});
email.setType("message/rfc822");
mContext.startActivity(Intent.createChooser(email, "Choose an Email client :"));
}
});
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, view all information custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((MainActivity)mContext).finish();
Intent intent = new Intent(mContext,DetailViewActivity.class);
intent.putExtra("contact",contact);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
mContext.startActivity(intent);
dialog.dismiss();
}
});
dialog.show();
}
}
}
编辑:当我们从服务器加载图片时,我搜索了与picasso库相同的问题,为此我从SO获得了解决方案:
recyclerView.setHasFixedSize(true);
recyclerView.setItemViewCacheSize(20);
recyclerView.setDrawingCacheEnabled(true);
recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
当我从服务器加载图像时,这很有效,但是当我从外部存储器加载图像时,问题仍然存在。
有人可以帮忙吗?谢谢..
答案 0 :(得分:4)
然后从外部存储装入图像。我可以看到图像 加载,但是当我滚动时,我可以看到一些图像一两秒 它们消失了,我可以看到默认的图像图标。
这是因为你在毕加索使用Target回调。当你滚动列表时,回调被称为迟了一点。只需删除目标并将imageview用于Picasso,它应该可以正常工作。此外,您不需要自己缓存位图,因为Picasso会为您完成。
public void onBindViewHolder(final ContactHolder contactHolder, int i) {
final Contact contact = contactList.get(i);
// Log.e("Imagename",""+"http://xesoftwares.co.in/contactsapi/profile_images/85368a5bbd6cffba8a3aa202a80563a2.jpg");//+feedItem.getThumbnail());
String url = ServiceUrl.getBaseUrl() + ServiceUrl.getImageUserUrl() + contact.getmProfileImage();
Log.e("url",url);
if(contact.getmProfileImage().equals("")) {
file = new File("");
fileExists = file.exists();
contactHolder.thumbnail.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_account_circle_black_48dp));
} else {
file = new File(Environment.getExternalStorageDirectory() + "/ContactProfileImages/" + contact.getmProfileImage());
fileExists = file.exists();
}
Log.e("Picasso",file.getAbsolutePath());
Picasso.with(mContext).load(url)
.error(R.drawable.ic_account_circle_black_24dp)
.placeholder(R.drawable.ic_account_circle_black_24dp)
.into(contactHolder.thumbnail);
contactHolder.title.setText(contact.getmUserName());
//feedListRowHolder.genre.setText(Html.fromHtml(feedItem.getGenre()));
}
答案 1 :(得分:1)
在Application类中使用此代码在整个应用程序中应用设置。
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory / 8;
Picasso.Builder builder = new Picasso.Builder(getApplicationContext());
builder.memoryCache(new LruCache(cacheSize));
Picasso built = builder.build();
built.setIndicatorsEnabled(true);// it will show indicator where it downloaded from
built.setLoggingEnabled(false);
Picasso.setSingletonInstance(built);
现在将网址传递给Picasso以加载它以进行查看。如果您正在处理大图像,您也可以调整图像大小以加快加载速度。
Picasso
.with(mContext)
.load(url)
.error(R.drawable.on_error)
.placeholder(R.drawable.place_holder)
//.resize(custom_width,custom_height) put custom_height to 0 if you want to maintain aspect ratio
.into(your_view);
答案 2 :(得分:0)
我可能会误会,但您的问题可能是由于您使用布局位置从数据集中获得的Contact
引起的,但是因为您在{{1}中异步使用它回调,那个时候位置已经可以改变了。所以你需要的是使用适配器位置,如下所示:
onBitmapLoaded
我不确定它会有所帮助,但让我们试试,我希望它会是:)
答案 3 :(得分:0)
如果您只想加载图片,那么为什么需要存储在外部存储中。如果您必须存储以用于任何其他目的,那么存储它,但是当您在ImageView中显示它时,请使用任何第三方API,如{ {3}}或Picasso它会延迟加载,而且所有类型的缓存内存管理也会提供许多显示效果。
希望这会对你有所帮助
答案 4 :(得分:0)
应该在后台线程上使用AsyncTask执行加载图像等操作。
答案 5 :(得分:0)
检查每张图片 MB 的数量。例如:如果 图像是1mb或500kb,下载图像需要更多时间 背景和下载后它将存储在catche.That意味着 第一次只需要一些时间来下载图像。
第二次,来自catche.It的那些图片将显示所有 几秒钟内的图像。
使用Volley - 更快和Glide - 进行内存管理。
Glide
.with(context)
.load(rowItem.getPosteduserpostimage())
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.fitCenter()
.placeholder(R.drawable.load_image)
.error(R.drawable.cancel_image)
.into(holder.ivPostedImageNew);
参考 here 如何加载大尺寸位图。你需要缩小图像。
否则,当我上传每张图片时,我确实使用了裁剪 library.It将负责缩小大位图。例如: 5mb 图像将缩小到180 kb。
检查CropImage lib 缩小更大尺寸的图像。即使用户没有裁剪图像,也只是 在裁剪时选择整个部分,它将缩小5 mb到 180kb.Image质量也很好。
我认为没有裁剪也可以使用下面的代码缩小图像。你可以看到这个代码:
private void startCropImage() {
Intent intent = new Intent(this, CropImage.class);
intent.putExtra(CropImage.IMAGE_PATH, mFileTemp.getPath());
intent.putExtra(CropImage.SCALE, true);
intent.putExtra(CropImage.ASPECT_X, 0); //3 for aspect ratio
intent.putExtra(CropImage.ASPECT_Y, 0); //2 for aspect ratio
startActivityForResult(intent, REQUEST_CODE_CROP_IMAGE);
}