gpt4 book ai didi

android - 使用 ContentProvider 获取缩略图

转载 作者:行者123 更新时间:2023-12-03 21:46:24 26 4
gpt4 key购买 nike

我尝试获取缩略图并将其放入我的布局中。我有带有 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;
}
所以在这里我得到缩略图。我在 list 文件中设置了权限:

<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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com