如何使用相机中的图像填充ListView

时间:2014-02-17 09:22:32

标签: android listview android-listview

我可以用我的相机中的图像URI填充列表项。我可以将精确的图像显示为列表项。

以下是我正在使用的列表视图(列表项是文本视图)。我想用图像做同样的事情。

ListView listview ;
ArrayList<String> list = new ArrayList<String>();
private File getOutputMediaFile(){

    File mediaStorageDir = new File(imgLink);

    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");}

public void onPictureTaken(byte[] data, Camera camera) {

        File pictureFile = getOutputMediaFile();
        if (pictureFile == null){
            Log.d(TAG, "Error creating media file, check storage permissions ");
            return;
        }
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
            } catch (FileNotFoundException e) {
            Log.d(TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d(TAG, "Error accessing file: " + e.getMessage());
            }

        if (list.contains(pictureFile.toString())) 
        {
            System.out.println(pictureFile+ " -> already present");
        }
        else
        {
             list.add(pictureFile.toString());
             System.out.println(pictureFile + " added");
        }
        polulateList();}

public void polulateList()
{

    ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,R.layout.list_item, list);
    listview.setAdapter(listAdapter);

}

3 个答案:

答案 0 :(得分:0)

将项目插入列表时,只需致电listAdapter.notifyDataSetChanged();

答案 1 :(得分:0)

protected void populateMyList(File pictureFile) {
    // TODO Auto-generated method stub


        String[] s = pictureFile.toString().split("/");
        String caption = s[7];
        mylist.add(new PopulateList(Uri.fromFile(pictureFile), caption));
        listAdapter = new MyListAdapter();
        listview.setAdapter(listAdapter);


}
public class MyListAdapter extends ArrayAdapter<PopulateList> {

    public MyListAdapter(){
        super(CameraActivity.this, R.layout.img_list,mylist);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        //inflate the view
        View myView = convertView;
        if (myView == null){
            myView = getLayoutInflater().inflate(R.layout.img_list, parent , false);
        }

        //populate the list
        PopulateList imageTaken = mylist.get(position);

        ImageView imgView = (ImageView)myView.findViewById(R.id.img);

        imgView.setImageURI(imageTaken.getLink());
        System.out.println(imgLink+imageTaken.getCaption());
        /*TextView txtView = (TextView)findViewById(R.id.caption);
        txtView.setVisibility(View.VISIBLE);
        txtView.setText(imageTaken.getCaption());*/

        return myView;

    }       

答案 2 :(得分:0)

        @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        // inflate the view
        View myView = convertView;
        if (myView == null) {
            myView = getLayoutInflater().inflate(R.layout.img_list, parent,
                    false);
        }
        // populate the list
        PopulateList imageTaken = mylist.get(position);
        ImageView imgView = (ImageView) myView.findViewById(R.id.img);
        imgView.setImageURI(imageTaken.getLink());
        System.out.println(imgLink + imageTaken.getCaption());
        /*
         * TextView txtView = (TextView)findViewById(R.id.caption);
         * txtView.setVisibility(View.VISIBLE);
         * txtView.setText(imageTaken.getCaption());
         */

        return myView;

    }