我只想将预定文件中的照片添加到GridView
,我无法弄清楚如何执行此操作。我有一个Adapter
处理我从 this example学到的GridView
,我无法弄清楚如何将mThumbIds数组设置为文件中的图像。我想我必须使用类似ImageView.setImageUri(Uri.fromFile(new File("myPath")));
而不是setImageResource
的内容,但我可能错了。这是我第一次做这样的事情,真的很感激任何帮助!
修改
当前代码:(有了这个,我得到一个NullPointer:for(File f : dir.listFiles()){
在文档中它表示如果它不是目录则返回null。新文件(...)中的代码是同样我用来创建目录,所以我知道路径是正确的,当应用程序启动它创建目录,文件夹中没有图片,直到用户拿一个。我当时认为这是问题,但它仍然关闭,当有目录中的照片。)
public static class HomeFragment extends Fragment{
public HomeFragment(){
}
View rootView;
GridView gridView;
List<Drawable> list;
File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "MyAppFolder");
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup contatiner,
Bundle savedInstanceState){
for(File f : dir.listFiles()){
Bitmap original = BitmapFactory.decodeFile(f.getAbsolutePath());
Drawable drawable = new BitmapDrawable(getResources(), original);
list.add(drawable);
}
rootView = inflater.inflate(R.layout.home_layout,
contatiner, false);
gridView = (GridView) rootView.findViewById(R.id.homeGridView);
gridView.setAdapter(new HomeAdapter(getActivity(), list));
return rootView;
}
}
public class HomeAdapter extends BaseAdapter {
private Context mContext;
private List<Drawable> mPictures;
public HomeAdapter(Context c, List<Drawable> list) {
mContext = c;
mPictures = list;
}
public int getCount() {
return mPictures.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(110, 110));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageDrawable(mPictures.get(position));
return imageView;
}
}
答案 0 :(得分:2)
您可以使用BitmapFactory.decodeFile(filePath)
获取位图:
Bitmap original = BitmapFactory.decodeFile(filePath);
// Scale the Bitmap if needed
Bitmap scaled = Bitmap.createScaledBitmap(original, newWidth, newHeight, true);
从此位图创建Drawable:
// Use mContext.getResources if you are doing this in the Adapter
Drawable drawable = new BitmapDrawable(getResources(), scaled);
更改type
的{{1}}:
mThumbIds
使用private Drawable[] mThumbIds = { drawable1, drawable2, drawable3....... };
:
ImageView.setImageDrawable(Drawable)
更好的方法是在Activity中创建imageView.setImageDrawable(mThumbIds[position]);
数组,并在初始化mThumbIds
时将其作为参数传递。您需要更改ImageAdapter
构造函数。
修改1:
您可以使用ImageAdapter's
来克服未知大小问题。我将假设您能够创建List<Drawable>
个对象并使用上述信息对其进行初始化:
Drawable
将您的Drawable d1 = ....
Drawable d2 = ....
Drawable d3 = ....
....
....
List<Drawable> list = new ArrayList<Drawable>();
list.add(d1);
list.add(d2);
....
gridview.setAdapter(new ImageAdapter(this, list));
代码更改为:
ImageAdapter's
在public class ImageAdapter extends BaseAdapter {
private Context mContext;
private List<Drawable> mThumbIds;
public ImageAdapter(Context c, List<Drawable> list) {
mContext = c;
mThumbIds = list;
}
public int getCount() {
return mThumbIds.size();
}
....
....
:
ImageAdapter's getView()
卸下:
imageView.setImageDrawable(mThumbIds.get(position));