简体   繁体   English

从相机捕获图像并将其设置为android中的listview

[英]capturing images from camera and setting into listview in android

What i am doing:: 我在做什么::

  • I am opening a camera onclick of item from actionbar menu 我正在从操作栏菜单中打开相机onclick项目
  • I am capturing the image and setting it in a listview 我正在捕获图像并将其设置在列表视图中

What is happening:: 怎么了::

  • Say i have captured 10 images and set it in listview 假设我已捕获10张图像并将其设置在列表视图中
  • next time i run my code, i am able to find the images i took last time, and it dosen't start from groundup 下次运行代码时,我能够找到我上次拍摄的图像,并且它不是从零开始的

What i am trying to do: 我正在尝试做的是:

  • say i captured 10 images and set in listview 说我捕获了10张图像并设置为列表视图
  • next time i start the app and start capturing the image it should add freshly captured images to listview and not display the old images 下次我启动该应用并开始捕获图像时,应将新捕获的图像添加到列表视图中,而不显示旧图像
  • i am not telling i have to delete these images but i the app i want to show newly captured images everytime 我不是在告诉我必须删除这些图像,但我想每次显示新捕获的图像的应用程序

MainActivity.java 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 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;
}

try this, using file.lastmodified() method. 请尝试使用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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM