I want to write an application that will: fill in the list view with image files from the SD card and b: display the image when you select an option from the list.
However, it hangs with the above exception. I do not understand why...? Please, help!
Primary activity:
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("._") ); } } }
Image Type:
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); } }
source share