我尝试获取缩略图并将其放入我的布局中。我有带有onCreateView方法的片段类。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
cr = getActivity().getContentResolver();
image = new ImageView(getActivity().getApplicationContext());
String[] mProjection = {
MediaStore.Images.Thumbnails._ID
};
thumbnails = cr.query(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
mProjection,
null,
null,
null);
if (thumbnails==null){
Log.d("yerchik/fragment", "error");
}else if (thumbnails.getCount() <1){
Log.d("yerchik/fragment", "nothing returned");
}else {
Log.d("yerchik/fragment", "thumbnails returned");
thumbnails.moveToFirst();
int index = thumbnails.getColumnIndex(MediaStore.Images.Thumbnails._ID);
Log.d("yerchik/fragment", "index: " + index);
if (thumbnails.moveToNext()){
Random r = new Random();
int rand = r.nextInt(thumbnails.getCount());
thumbnails.moveToPosition(rand);
bitmap = MediaStore.Images.Thumbnails.getThumbnail(
cr,
thumbnails.getInt(index),
MediaStore.Images.Thumbnails.MICRO_KIND,
null);
image.setImageBitmap(bitmap);
}
}
return image;
}所以这里我得到了缩略图。我在清单文件中设置了权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
在我的主要活动中,我把这个片段放在下面:
Button btn = (Button)findViewById(R.id.addBtn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fm = getFragmentManager();
ft = fm.beginTransaction();
BlankFragment fragment = new BlankFragment();
ft.add(R.id.thumbnailsGridLayout, fragment);
ft.commit();
}
});
在我的日志中我有:
返回的缩略图
所以我找到了一些缩略图,但是什么也没有显示出来。
有趣的是,如果我为片段添加布局,并用一些src图像声明我的ImageView,然后注释掉image.setImageBitmap(位图)行,我将绘制我的样本图像。删除注释后什么也不会出现,所以我的代码似乎绘制了一些东西,因为从布局添加的图像消失了,但没有缩略图出现。
发布于 2019-12-26 14:18:18
MediaStore.Images.Thumbnails.getThumbnail()已被弃用。
尝试使用loadThumbnail()。
像这样(在Kotlin中):
val cr = applicationContext.contentResolver
val bitmap = cr.loadThumbnail(uri,Size(25,25),null)
image.setImageBitmap(bitmap)https://stackoverflow.com/questions/33339890
复制相似问题