我想编写一个应用程序,其中包括:使用sdcard中的图片文件填充列表视图,b:从列表中选择选项时显示图像.
但是,它与上述例外有关.我看不出为什么……?请帮忙!
主要活动:
public class ProjektJimmuActivity extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); navigateTo("/sdcard/"); } public void navigateTo(String dir){ setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,getListOfFiles(dir))); getListView().setTextFilterEnabled(true); } public String[] getListOfFiles(String dir){ File file = new File(dir); String[] files = file.list(new Filter()); return files; } @Override protected void onListItemClick(ListView l,View v,int position,long id) { super.onListItemClick(l,v,position,id); Object item = l.getItemAtPosition(position); Toast.makeText(this,"Selection: "+ item.toString(),Toast.LENGTH_SHORT).show(); setContentView(new PictureView(this,item.toString())); } private class Filter implements FilenameFilter{ public boolean accept(File dir,String filename) { return (( filename.endsWith("jpg") || filename.endsWith("png") || filename.endsWith("gif") ) && !filename.startsWith("._") ); } } }
图片视图:
public class PictureView extends View{ private Bitmap bitmap; int width,height; private Canvas canvas; public PictureView(Context context,String pictureFile) { super(context); setFocusable(true); bitmap = BitmapFactory.decodeFile("/sdcard/"+pictureFile); height = bitmap.getHeight(); width = bitmap.getWidth(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(0xFFCCCCCC); canvas.drawBitmap(bitmap,null,null); } }