【发布时间】:2015-10-26 06:46:33
【问题描述】:
我尝试获取缩略图并将其放入我的布局中。我有带有 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(bitmap) 行,我将绘制我的示例图像。在我删除评论后,什么都没有出现,所以我的代码似乎画了一些东西,因为从布局添加的图像消失了,但没有出现缩略图。
【问题讨论】:
标签: android android-fragments android-contentprovider mediastore