从相机捕获图像并在android中设置为listview

时间:2014-12-27 12:00:52

标签: java android image listview

我在做什么::

  • 我正在打开操作栏菜单
  • 中的项目
  • 我正在捕捉图像并将其设置在listview中

发生了什么::

  • 假设我已捕获了10张图片并将其设置为listview
  • 下次我运行我的代码时,我能够找到我最后拍摄的图像 时间,而且它不是从一开始就
  • 开始的

我想做什么:

  • 说我拍摄了10张图片并在listview中设置
  • 下次启动应用程序并开始捕获它应该添加的图像 新鲜捕获的图像到列表视图而不显示旧图像
  • 我不是说我必须删除这些图像,但我想要的应用程序 每次显示新捕获的图像

MainActivity.java

public class MainActivity extends ListActivity {

    private static final int CAMERA_CAPTURE = 20;
    ArrayList<String> listOfImages;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DisplayCapturedImagesFromCamera();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_camera) {
            startCameraCapture();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void startCameraCapture() {

        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);     
         if (cameraIntent.resolveActivity(getPackageManager()) != null) {

             File photoFile = null;
                try {
                     photoFile = CreateImageFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                if(photoFile != null)
                {
                    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                    startActivityForResult(cameraIntent, CAMERA_CAPTURE);                                   
                }               
            }       
    }

    private File CreateImageFile() throws IOException
    {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "Image_" + timeStamp + "_";

        File storageDirectory = getExternalFilesDir("");    
        File image = File.createTempFile(imageFileName, ".jpg",storageDirectory);       
        return image;

    }

    @Override
    public void onActivityResult(final int requestCode, int resultCode, Intent data) {

        switch(requestCode)
        {
        case CAMERA_CAPTURE:
            if(resultCode == RESULT_OK)
            {
                DisplayCapturedImagesFromCamera();              
            }
            break;
        }

    }

    private void DisplayCapturedImagesFromCamera() {
        // TODO Auto-generated method stub
        File myPath = getExternalFilesDir(null);
        listOfImages = new ArrayList<String>();

         try
         {

        for(File f: myPath.listFiles()) {
            listOfImages.add(f.getAbsolutePath());
        }

        AdptAddjobsGallery adapter = new AdptAddjobsGallery(MainActivity.this,listOfImages);
        setListAdapter(adapter);
         }
         catch(Exception ex)
         {
             Log.w("Error", ex.getMessage());
         }

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);

        // custom dialog
                    final Dialog dialog = new Dialog(MainActivity.this);
                    dialog.setContentView(R.layout.cust_dialog);
                    dialog.setTitle("Image ");

                    Bitmap bitmap = BitmapFactory.decodeFile(listOfImages.get(position));

                    // set the custom dialog components - text, image and button
                    ImageView image = (ImageView) dialog.findViewById(R.id.image);
                    image.setImageBitmap(bitmap);

                    Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
                    // if button is clicked, close the custom dialog

                    dialogButton.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View arg0) {

                            dialog.dismiss();

                        }
                    });

                    dialog.show();  
    }

}

AdptAddjobsGallery.java

public class AdptAddjobsGallery extends ArrayAdapter<String> {

    private final Activity context;

    private final ArrayList<String> listOfImages;

    public AdptAddjobsGallery(Activity context, ArrayList<String> listOfImages) {
        super(context, R.layout.adpt_addjobs_gallery, listOfImages);
        // TODO Auto-generated constructor stub

        this.context=context;
        this.listOfImages = listOfImages;

    }

    public View getView(int position,View view,ViewGroup parent) {

        ViewHolder holder;

        if(view == null)
        {       
        LayoutInflater inflater=context.getLayoutInflater();
        view =inflater.inflate(R.layout.adpt_addjobs_gallery, null,true);

        holder = new ViewHolder();
        holder.imageView = (ImageView) view.findViewById(R.id.selfie);
        holder.txtTitle = (TextView) view.findViewById(R.id.fileName);
        view.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) view.getTag();
        }


        Bitmap bitmap = BitmapFactory.decodeFile(listOfImages.get(position));       
        File f = new File(listOfImages.get(position));  

        holder.txtTitle.setText(f.getName());
        holder.imageView.setImageBitmap(bitmap);

        return view;

    };
}

 class ViewHolder {
     TextView txtTitle;
     ImageView imageView;
}

1 个答案:

答案 0 :(得分:0)

使用file.lastmodified()方法尝试此操作。

private ArrayList<String> getRecentImages(long from, long to,
            ArrayList<String> list) {
        ArrayList<String> sortedList = new ArrayList<String>();
        for (int i = 0; i < list.size(); i++) {
            File file = new File(list.get(i));
            long modified = file.lastModified();
            if (modified > from && modified <= to) {
                sortedList.add(list.get(i));
            }
        }
        return sortedList;
    }