簡體   English   中英

從相機捕獲圖像並將其設置為android中的listview

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

我在做什么::

  • 我正在從操作欄菜單中打開相機onclick項目
  • 我正在捕獲圖像並將其設置在列表視圖中

怎么了::

  • 假設我已捕獲10張圖像並將其設置在列表視圖中
  • 下次運行代碼時,我能夠找到我上次拍攝的圖像,並且它不是從零開始的

我正在嘗試做的是:

  • 說我捕獲了10張圖像並設置為列表視圖
  • 下次我啟動該應用並開始捕獲圖像時,應將新捕獲的圖像添加到列表視圖中,而不顯示舊圖像
  • 我不是在告訴我必須刪除這些圖像,但我想每次顯示新捕獲的圖像的應用程序

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;
}

請嘗試使用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