decodeStream 不使用位图图像填充 recyclerView
decodeStream not populating recyclerView with Bitmap image
我想用小的 (60x60) 专辑封面位图填充我的 recyclerview,但我似乎无法得到它。我已经尝试了所有我知道的,请帮忙。
我用来缩小图像的代码:
public static int calculateSize(BitmapFactory.Options opt, int reqHeight, int reqWidth){
int height = opt.outHeight;
int width = opt.outWidth;
int inSampleSize = 1;
if(height > reqHeight || width > reqWidth){
final int halfWidth = width/2;
final int halfHeight = height /2;
while((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth){
inSampleSize *= 2;
}
}
return inSampleSize;
}
public Bitmap decodeSampleBitmapFromArray(InputStream stream, Rect rect, int reqHeight, int reqWidth){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bit = BitmapFactory.decodeStream(stream, rect, options);
//Calculate sample size
options.inSampleSize = calculateSize(options, reqHeight, reqWidth);
//Decode bitmap with insamplesize set false
options.inJustDecodeBounds = false;
return bit;
}
然后我在我的 onCreate 方法中调用了 decodeSampleBitmapFromArray 函数来填充我的 recyclerview 适配器:
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
...
adapter = new CustomRecycler(paths);
recyclerView = (RecyclerView)v.findViewById(R.id.ryc);
lManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(lManager);
recyclerView.setHasFixedSize(true);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
metaRetriever.setDataSource(path);
byte[] b = metaRetriever.getEmbeddedPicture();
if (b != null) {
InputStream in = new ByteArrayInputStream(b);
paths.add(decodeSampleBitmapFromArray(in,new Rect(-1,-1,1,1), 60, 60));
}else{
Bitmap altIcon = BitmapFactory.decodeResource(getResources(), R.drawable.beat);
paths.add(altIcon);
}
}
然后我的 RecyclerAdapter 接收到位图并使用 ImageView 显示它
holder.artImage.setImageBitmap(mImage.get(position));
完成所有这些操作后,图像仍未填充到回收站视图中。我做错了什么?提前致谢
当您设置 options.inJustDecodeBounds = true
时,确实会发生这种情况 - 它只是解码边界。位图总是返回 Null。这个想法是,一旦知道边界,就可以将 options.inSampleSize
设置为适当的值并再次解码图像数据。
仔细查看此开发者页面中的方法 decodeSampleBitmapFromResource
:https://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap
BitmapFactory.decodeResource
被调用 两次 ,首先使用 options.inJustDecodeBounds = true
获取尺寸,然后使用 options.inSampleSize
设置生成按比例缩放的位图你想要的方式。
您的代码正在做正确的事情来获取 options.inSampleSize
的值,除了您永远不会使用此值再次调用 BitmapFactory.decodeStream()
来检索缩放的位图。
我想用小的 (60x60) 专辑封面位图填充我的 recyclerview,但我似乎无法得到它。我已经尝试了所有我知道的,请帮忙。
我用来缩小图像的代码:
public static int calculateSize(BitmapFactory.Options opt, int reqHeight, int reqWidth){
int height = opt.outHeight;
int width = opt.outWidth;
int inSampleSize = 1;
if(height > reqHeight || width > reqWidth){
final int halfWidth = width/2;
final int halfHeight = height /2;
while((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth){
inSampleSize *= 2;
}
}
return inSampleSize;
}
public Bitmap decodeSampleBitmapFromArray(InputStream stream, Rect rect, int reqHeight, int reqWidth){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bit = BitmapFactory.decodeStream(stream, rect, options);
//Calculate sample size
options.inSampleSize = calculateSize(options, reqHeight, reqWidth);
//Decode bitmap with insamplesize set false
options.inJustDecodeBounds = false;
return bit;
}
然后我在我的 onCreate 方法中调用了 decodeSampleBitmapFromArray 函数来填充我的 recyclerview 适配器:
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
...
adapter = new CustomRecycler(paths);
recyclerView = (RecyclerView)v.findViewById(R.id.ryc);
lManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(lManager);
recyclerView.setHasFixedSize(true);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
metaRetriever.setDataSource(path);
byte[] b = metaRetriever.getEmbeddedPicture();
if (b != null) {
InputStream in = new ByteArrayInputStream(b);
paths.add(decodeSampleBitmapFromArray(in,new Rect(-1,-1,1,1), 60, 60));
}else{
Bitmap altIcon = BitmapFactory.decodeResource(getResources(), R.drawable.beat);
paths.add(altIcon);
}
}
然后我的 RecyclerAdapter 接收到位图并使用 ImageView 显示它
holder.artImage.setImageBitmap(mImage.get(position));
完成所有这些操作后,图像仍未填充到回收站视图中。我做错了什么?提前致谢
当您设置 options.inJustDecodeBounds = true
时,确实会发生这种情况 - 它只是解码边界。位图总是返回 Null。这个想法是,一旦知道边界,就可以将 options.inSampleSize
设置为适当的值并再次解码图像数据。
仔细查看此开发者页面中的方法 decodeSampleBitmapFromResource
:https://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap
BitmapFactory.decodeResource
被调用 两次 ,首先使用 options.inJustDecodeBounds = true
获取尺寸,然后使用 options.inSampleSize
设置生成按比例缩放的位图你想要的方式。
您的代码正在做正确的事情来获取 options.inSampleSize
的值,除了您永远不会使用此值再次调用 BitmapFactory.decodeStream()
来检索缩放的位图。