我的Android应用程序的一段代码遇到了一些麻烦.我NullPointerException在尝试设置ImageView对象的背景时不断得到.
这是代码:
public View getView(int position, View view, ViewGroup parent) {
ImageView imageView;
if (view == null) {
imageView = new ImageView(mContext);
} else {
imageView = (ImageView) view;
}
imageView.setTag(position);
return imageView;
}
private OnItemClickListener itemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
ImageView imageView;
//Variable i, here, is from a for loop.
imageView = (ImageView)v.findViewWithTag(i);
//I get a NullPointerException at the next line, "Log.d"
Log.d("View …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个可以从Android手机上的目录中获取图像并在布局中显示它们的应用程序.我似乎正朝着一种倒退的方式努力.我知道用来view.setBackgroundDrawable(Drawable.createFromPath(String pathName));显示图像,但我不知道如何从目录中获取图像的路径.
我对如何做有一个模糊的想法,但希望澄清这个问题.我认为下一步是使用:
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
File file[] = Environment.getExternalStorageDirectory().listFiles();
}
Run Code Online (Sandbox Code Playgroud)
但是如何过滤文件以便只将图像文件存储在我的文件中file[]呢?比如.png或.jpg/.jpeg文件?在那之后,我应该使用files[].getPath或files[].getAbsolutePath?我会把结果存储在一个String[].
我主要要求的是验证上述代码是否有效.而且,我怎么可能过滤仅存储图像文件,如.png,.jpg和.jpeg.
感谢您的时间.
android ×2