[英]Load bitmap with picasso into ImageView
我正在構建一個文件資源管理器應用程序,並且我試圖加載大量圖像(超過5000個...),我正在嘗試使用畢加索,但是它仍然很慢,因此我試圖使用畢加索,而不是整個圖像文件。
....
ImageItem item = data.get(position);
ImageView imageView = holder.image;
Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(item.getImage().getPath()), 100, 100);
Picasso.with (context) .load (ThumbImage ) .into (imageView); /*/ doesnt work.. cant load ThumbImage with Picasso
Picasso.with (context) .load (item.getImage()) .into (imageView); //work, but too slow
我無法弄清楚如何用Picasso將Thumbimage加載到ImageView中。為什么有這樣做呢? 另一種加載圖像更快的方法?
首先,我建議使用RecyclerView
而不是GridView
,因為它可以為您處理很多內存管理。 不必一次加載所有圖像(或自行管理),它只會加載適合屏幕顯示的圖像。 而且,您可以使用GridLayoutManager
使其像在GridView
。
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler);
recyclerView.setLayoutManager(new GridLayoutManager(context, 4);
recyclerView.setAdapter(new MyRecyclerViewAdapter(context, data);
另外,讓Picasso為您處理圖像轉換,這樣您就不必使用ThumbnailUtils
。 Picasso使用resize()
函數處理圖像大小調整和內存管理:
String uri = data.get(position);
Picasso.with(context).load(new File(uri)).resize(100, 100)
.centerCrop().into(imageView)
您可以在RecyclerView.Adapter
的onBindViewHolder()
方法中執行此操作,以便僅在必須顯示View
時才加載每個圖像。
我不確定您當前如何獲取圖像,但是如果您不知道如何獲取圖像的URI,則應該可以:
String[] projection = {"*"};
List<String> data = new ArrayList<>();
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
if(cursor.moveToFirst()) {
do {
data.add(cursor.getString(cursor
.getColumnIndex(MediaStore.Images.Media.DATA)));
} while(cursor.moveToNext());
}
我不知道這是否可以解決Picasso不加載圖像的問題,但是如果它不發布堆棧跟蹤信息,我們可以嘗試解決。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.