【发布时间】:2017-01-27 20:56:20
【问题描述】:
我想在滚动回收站视图时异步加载图像。为此,我使用 picasso 从 url 加载图像。此外,我正在为图像使用圆形图像视图。
现在,当我先向上和向下滚动时,加载图像需要 10 到 12 秒。当我滚动它时,它会在圆形图像视图上出现内存不足错误。
联系人适配器:
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;
}
}
错误:
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)
如何解决这个内存不足错误?我尝试在 picasso 中使用 .fit(),但它给出了非法状态异常。另外加载图像至少需要 10 秒,如何减少加载图像的时间?
谢谢你..
编辑:.into() 之后的代码不起作用,它直接跳转到 complexShared 首选项。并且由于复杂的 sharedPreferences 保持为空,它会引发空指针异常。
这发生在我卸载应用程序并再次运行时。在它运行良好之前。
【问题讨论】:
-
您的图像尺寸必须比您为 ImageView 指定的尺寸大。在服务器上增加尺寸或减小图像的大小。
-
调整大小怎么样?您是否尝试过毕加索的调整大小功能,如果没有,请尝试一下
-
如何调整大小?你能展示一下吗? @乔治·托马斯
-
只需在您的毕加索调用中添加 .resize(100,100) 即可。将 100 * 100 替换为您所需的图像尺寸。您还可以在 picasso 上调用 .onlyScaleDown() 来指示它仅在实际图像大小大于 resize 方法中指定的宽度和高度时才调整图像大小。这么小的图片不会被调整大小
-
.onlyScaleDown() 在毕加索中找不到。 @GeorgeThomas
标签: android out-of-memory picasso