Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take a picture to show in a `ImageView` and save the picture?

I need to take a picture with the camera, save the picture, show in ImageView and when I click the Imageview show in fullscreen mode .

In the future will need to send the picture to the internet.

This is what I've done :

public void captureImage(View v) {
    Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(camera_intent, CAMERA_PIC_REQUEST);

}

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

    imgView = (ImageView) findViewById(R.id.formRegister_picture);
    imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);

    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode){
        case CAMERA_PIC_REQUEST:
            if(resultCode==RESULT_OK){
                Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
                imgView.setImageBitmap(thumbnail);
            }
    }
}
like image 523
admqueiroga Avatar asked Jul 16 '15 04:07

admqueiroga


People also ask

How do I save a picture to my gallery?

Touch and hold the image. Select a save option (e.g., Save attachment, Save to SD card, etc.). Unless otherwise specified, the image is saved to the default picture/video location (e.g., Gallery, Photos, etc.).

How do you open camera through intent and display captured image?

This is done as follows: Intent camera_intent = new Intent(MediaStore. ACTION_IMAGE_CAPTURE); startActivityForResult(camera_intent, pic_id); Now use the onActivityResult() method to get the result, here is the captured image.


1 Answers

You can invoke camera Activity by adding these lines in your code :

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);              
private static int RESULT_IMAGE_CLICK = 1;

                cameraImageUri = getOutputMediaFileUri(1);

                // set the image file name
                intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraImageUri);
                startActivityForResult(intent, RESULT_IMAGE_CLICK);

Now create file Uri because in some android phones you will get null data in return

so here is the method to get the image URI :

 /** Create a file Uri for saving an image or video */
        private static Uri getOutputMediaFileUri(int type) {

            return Uri.fromFile(getOutputMediaFile(type));
        }

        /** Create a File for saving an image or video */
        private static File getOutputMediaFile(int type) {

            // Check that the SDCard is mounted
            File mediaStorageDir = new File(
        Environment.getExternalStorageDirectory(), Environment.DIRECTORY_PICTURES);

            // Create the storage directory(MyCameraVideo) if it does not exist
            if (!mediaStorageDir.exists()) {

                if (!mediaStorageDir.mkdirs()) {

                    Log.e("Item Attachment",
                            "Failed to create directory MyCameraVideo.");

                    return null;
                }
            }
java.util.Date date = new java.util.Date();
        String timeStamp = getTimeStamp();

        File mediaFile;

        if (type == 1) {

            // For unique video file name appending current timeStamp with file
            // name
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +abc+ ".jpg");

        } else {
            return null;
        }

        return mediaFile;
    }

For retrieving clicked image :

@Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == RESULT_OK) {
                if (requestCode == RESULT_IMAGE_CLICK) {


    // Here you have the ImagePath which you can set to you image view
                    Log.e("Image Name", cameraImageUri.getPath());

         Bitmap myBitmap = BitmapFactory.decodeFile(cameraImageUri.getPath()); 

            yourImageView.setImageBitmap(myBitmap);



// For further image Upload i suppose your method for image upload is UploadImage
File imageFile = new File(cameraImageUri.getPath());
                uploadImage(imageFile);

                        }




            }
        }
like image 166
Azfaar kabir Siddiqui Avatar answered Sep 30 '22 18:09

Azfaar kabir Siddiqui