android how to save captured image into phones gallery

54,896

Solution 1

See http://blog.kupriyanov.com/2010/02/solved-android-save-image-to-media.html to save to gallery, and see Get/pick an image from Android's built-in Gallery app programmatically for displaying the gallery image.

Solution 2

This is the way I did it. The image is saved by minutes+seconds+.jpg on the SDCard:

final static private int NEW_PICTURE = 1;
private String mCameraFileName;


ImageButton Edit = (ImageButton) findViewById(R.id.internetbrowser4);

    Edit.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent();
            // Picture from camera
            intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);

            // This is not the right way to do this, but for some reason, having
            // it store it in
            // MediaStore.Images.Media.EXTERNAL_CONTENT_URI isn't working right.

            Date date = new Date();
            DateFormat df = new SimpleDateFormat("-mm-ss");

            String newPicFile = "Bild"+ df.format(date) + ".jpg";
            String outPath = "/sdcard/" + newPicFile;
            File outFile = new File(outPath);

            mCameraFileName = outFile.toString();
            Uri outuri = Uri.fromFile(outFile);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outuri);


                startActivityForResult(intent, NEW_PICTURE);

        }
    });

  public void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        if (requestCode == NEW_PICTURE) 
        {
            // return from file upload
            if (resultCode == Activity.RESULT_OK) 
            {
                Uri uri = null;
                if (data != null) 
                {
                    uri = data.getData();
                }
                if (uri == null && mCameraFileName != null) 
                {
                    uri = Uri.fromFile(new File(mCameraFileName));
                }
                File file = new File(mCameraFileName);
                if (!file.exists()) {
                    file.mkdir();
                }
            }
        }} 
}

Solution 3

try this..

String path = Environment.getExternalStorageDirectory() + "/CameraImages/example.jpg";
                            File file = new File(path);
                            Uri outputFileUri = Uri.fromFile( file );
                            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
                            intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );

                            startActivityForResult( intent, CAPTURE_IMAGE );

your image will be save at this location "sdcard/CameraImages/example.jpg"

Share:
54,896
Jyosna
Author by

Jyosna

Works at Amadeus Labs

Updated on March 06, 2020

Comments

  • Jyosna
    Jyosna about 4 years

    I have two activities. In one activity, I have an ImageView and an camera button. When I press camera button it goes to other activity where two buttons are there Capture and another is Select button. When I press capture it captures an image. But question is how to save this capture image to gallery. And after pressing the Select button the captured image should display on 1st activity's ImageView. How can I do that.

  • cogg
    cogg over 9 years
    That blog moved to blog.kupriyanov.com/2010/02/…
  • McLan
    McLan over 7 years
    Cannot figure out where this code goes to ?! please ehlp
  • McLan
    McLan over 7 years
    Sorry, but it doesn't work on Nexus 7 (Cyanogenmod) !