Capture Image from Camera and Display in Activity

前端 未结 16 1061
悲&欢浪女
悲&欢浪女 2020-11-21 06:43

I want to write a module where on a click of a button the camera opens and I can click and capture an image. If I don\'t like the image I can delete it and click one more i

16条回答
  •  自闭症患者
    2020-11-21 07:24

    Capture photo + Choose from Gallery:

            a = (ImageButton)findViewById(R.id.imageButton1);
    
            a.setOnClickListener(new View.OnClickListener() {
    
                @Override
    
                public void onClick(View v) {
    
                    selectImage();
    
                }
    
            });
        }
        private File savebitmap(Bitmap bmp) {
          String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
          OutputStream outStream = null;
         // String temp = null;
            File file = new File(extStorageDirectory, "temp.png");
          if (file.exists()) {
           file.delete();
           file = new File(extStorageDirectory, "temp.png");
    
          }
    
          try {
           outStream = new FileOutputStream(file);
           bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream);
           outStream.flush();
           outStream.close();
    
          } catch (Exception e) {
           e.printStackTrace();
           return null;
          }
          return file;
         }
        @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;
        }
         private void selectImage() {
    
    
    
                final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };
    
    
    
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    
                builder.setTitle("Add Photo!");
    
                builder.setItems(options, new DialogInterface.OnClickListener() {
    
                    @Override
    
                    public void onClick(DialogInterface dialog, int item) {
    
                        if (options[item].equals("Take Photo"))
    
                        {
    
                            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    
                            File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
    
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                            //pic = f;
    
                            startActivityForResult(intent, 1);
    
    
                        }
    
                        else if (options[item].equals("Choose from Gallery"))
    
                        {
    
                            Intent intent = new   Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    
                            startActivityForResult(intent, 2);
    
    
    
                        }
    
                        else if (options[item].equals("Cancel")) {
    
                            dialog.dismiss();
    
                        }
    
                    }
    
                });
    
                builder.show();
    
            }
    
    
    
            @Override
    
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
                super.onActivityResult(requestCode, resultCode, data);
    
                if (resultCode == RESULT_OK) {
    
                    if (requestCode == 1) {
                        //h=0;
                        File f = new File(Environment.getExternalStorageDirectory().toString());
    
                        for (File temp : f.listFiles()) {
    
                            if (temp.getName().equals("temp.jpg")) {
    
                                f = temp;
                                File photo = new File(Environment.getExternalStorageDirectory(), "temp.jpg");
                               //pic = photo;
                                break;
    
                            }
    
                        }
    
                        try {
    
                            Bitmap bitmap;
    
                            BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    
    
    
                            bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
    
                                    bitmapOptions); 
    
    
    
                            a.setImageBitmap(bitmap);
    
    
    
    
                            String path = android.os.Environment
    
                                    .getExternalStorageDirectory()
    
                                    + File.separator
    
                                    + "Phoenix" + File.separator + "default";
                            //p = path;
    
                            f.delete();
    
                            OutputStream outFile = null;
    
                            File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
    
                            try {
    
                                outFile = new FileOutputStream(file);
    
                                bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
        //pic=file;
                                outFile.flush();
    
                                outFile.close();
    
    
                            } catch (FileNotFoundException e) {
    
                                e.printStackTrace();
    
                            } catch (IOException e) {
    
                                e.printStackTrace();
    
                            } catch (Exception e) {
    
                                e.printStackTrace();
    
                            }
    
                        } catch (Exception e) {
    
                            e.printStackTrace();
    
                        }
    
                    } else if (requestCode == 2) {
    
    
    
                        Uri selectedImage = data.getData();
                       // h=1;
        //imgui = selectedImage;
                        String[] filePath = { MediaStore.Images.Media.DATA };
    
                        Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null);
    
                        c.moveToFirst();
    
                        int columnIndex = c.getColumnIndex(filePath[0]);
    
                        String picturePath = c.getString(columnIndex);
    
                        c.close();
    
                        Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
    
    
                        Log.w("path of image from gallery......******************.........", picturePath+"");
    
    
                        a.setImageBitmap(thumbnail);
    
                    }
    
                }
    

提交回复
热议问题