Fady Samy
Fady Samy

Reputation: 35

Android Camera to open and then open the captured image in a new activity

I want to make an app, when i press on a certain button i want to open the cam then capture the image and after i capture it.

I want to open this image in a new activity, putting this image in this new activity in addition to two buttons one to delete it and the other one to save it in a certain directory of the tablet.

i use the code to open the cam:

    Open_CAM.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v)  {

            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
           startActivityForResult(cameraIntent, CAMERA_REQUEST);  

I don't know what can i do after that?

Any help please...

Upvotes: 1

Views: 2647

Answers (2)

Hardik Nadiyapara
Hardik Nadiyapara

Reputation: 2436

bellow code will help you it will take a picture from camera and set image in to next activity

private void takePicture() {
     cameraIntent = new Intent(
     android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
     startActivityForResult(cameraIntent, IMAGE_CAPTURE);
}

// Receive the result from the start Activity
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Log.e("onActivityResult", "we r in onActivityResult");

        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {

            case IMAGE_CAPTURE:

                File dir = Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
                File output = new File(dir, "camerascript.png");
                cPath = output.getAbsolutePath();
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(output));
                Intent capIntent = new Intent(yourcurrentActivity.this,
                        yournextActivity.class);
                capIntent.putExtra("gallery", cPath);
                startActivity(capIntent);
                break;
            default:
                break;
            }
        }
    }

After that get the intnet extra data in to yournextAactivity where you would like to set a captured image.

        ImageView imageView = (ImageView)findViewById(R.id.imgView);
    String fileString = getIntent().getStringExtra("gallery");
    imageView.setImageBitmap(BitmapFactory.decodeFile(fileString));

Upvotes: 2

Santosh
Santosh

Reputation: 611

Open_CAM.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v)  {
                Intent photoPickerIntent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempFile());
                photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
                photoPickerIntent.putExtra("return-data", true);
                startActivityForResult(photoPickerIntent,TAKE_PICTURE);}}

 private Uri getTempFile() 
 {
      File root = new File(Environment.getExternalStorageDirectory(), "ServiceMySigns");
      if (!root.exists()) 
      {
          root.mkdirs();
      }
     final Calendar c = Calendar.getInstance();
     int y = c.get(Calendar.YEAR);
     int m = c.get(Calendar.MONTH);
     int d = c.get(Calendar.DAY_OF_MONTH);

     int h = c.get(Calendar.HOUR_OF_DAY);
     int mi = c.get(Calendar.MINUTE);

      //String filename=""+y+"-"+"-"+(m+1)+"-"+d+" "+h+":"+mi;
      String filename=""+System.currentTimeMillis();
      File file = new File(root,filename+".jpeg" );
      muri = Uri.fromFile(file);
      selectedImagePath=muri.getPath();
      Log.v("take picture path",selectedImagePath);
      return muri;
  }

  public void onActivityResult(int requestcode,int resultcode ,Intent data)
  {
      switch(requestcode)
      { 
      case TAKE_PICTURE: 
          if(resultcode==RESULT_OK)
          { 
              BitmapFactory.Options o = new BitmapFactory.Options();
              o.inSampleSize=8;
              Bitmap newImage = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(selectedImagePath,o), 
                                                         150, 
                                                         150, 
                                                         false);}}}

After you got bitmap in onActivityResult you can send that bitmap to another activity through intent.

Upvotes: 5

Related Questions