作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试获取缩略图并将其放入我的布局中。我有带有 onCreateView 方法的 fragment 类。
@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" />
在我的主要 Activity 中,我将这个 fragment 放置如下:
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();
}
});
在我的日志中我有:
缩略图返回
所以我得到了一些缩略图,但什么也没有出现。
有趣的是,如果我为 fragment 添加布局并使用一些 src 图像声明我的 ImageView 并注释掉 image.setImageBitmap(bitmap) 行,我将绘制我的示例图像。在我删除评论后什么都没有出现,所以我的代码似乎绘制了一些东西,因为从布局添加的图像消失了,但没有缩略图出现。
最佳答案
MediaStore.Images.Thumbnails.getThumbnail() 已弃用。
尝试使用 loadThumbnail()。
像这样(在 Kotlin 中):
val cr = applicationContext.contentResolver
val bitmap = cr.loadThumbnail(uri,Size(25,25),null)
image.setImageBitmap(bitmap)
关于android - 使用 ContentProvider 获取缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33339890/
我有一个具有可变数量子元素的固定大小的 div。我不知道 children 的大小。目标是缩小它们以适合父级。 例子: .parent { width: 100px; height: 100p
我是一名优秀的程序员,十分优秀!