Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take a photo, save it and get the photo in Android

I've been searching the simple example to take a photo, and save it using URI and retrieve the photo for image processing , I've tried lot of example code, but none of them went smoothly.

Is there anyone have the example code?

like image 919
user1802057 Avatar asked Jan 04 '13 09:01

user1802057


People also ask

Where do saved pics go on Android?

When you turn on back up and sync, your photos are stored in photos.google.com. Learn other ways to find your photos. Important: If your Google Photos account is inactive for 2 years or you're over your storage limit, it may impact your content. Learn how your Google storage works.

How do you save an image on an Android?

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 get an image from a camera or a gallery and save it in Android?

Run the application on an Android phone. Selecting "Take photo" will open your camera. Finally, the image clicked will be displayed in the ImageView. Selecting "Choose from Gallery" will open your gallery (note that the image captured earlier has been added to the phone gallery).


2 Answers

I had the same problem.

I was testing some codes from the internet, and couldn't find any. Then, I studied some basics codes from developer.android. After that, I mixed two different codes and my one worked! Here it goes!

public class MainActivity extends AppCompatActivity {

    static final int PICTURE_RESULT = 1;
    String mCurrentPhotoPath;
    ContentValues values;
    private Uri file;
    ImageView imageView;
    Bitmap help1;

    ThumbnailUtils thumbnail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.imageView);
        values = new ContentValues();
    }

    public void launch_camera(View v) {
        // the intent is my camera
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //getting uri of the file
        file = Uri.fromFile(getFile());

        //Setting the file Uri to my photo
        intent.putExtra(MediaStore.EXTRA_OUTPUT,file);

        if(intent.resolveActivity(getPackageManager())!=null)
        {
            startActivityForResult(intent, PICTURE_RESULT);
        }
    }

    //this method will create and return the path to the image file
    private File getFile() {
        File folder = Environment.getExternalStoragePublicDirectory("/From_camera/imagens");// the file path

        //if it doesn't exist the folder will be created
        if(!folder.exists())
        {folder.mkdir();}

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_"+ timeStamp + "_";
        File image_file = null;

        try {
            image_file = File.createTempFile(imageFileName,".jpg",folder);
        } catch (IOException e) {
            e.printStackTrace();
        }

        mCurrentPhotoPath = image_file.getAbsolutePath();
        return image_file;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == PICTURE_RESULT) {
            if(resultCode == Activity.RESULT_OK) {
                try {
                    help1 = MediaStore.Images.Media.getBitmap(getContentResolver(),file);
                    imageView.setImageBitmap( thumbnail.extractThumbnail(help1,help1.getWidth(),help1.getHeight()));
                }catch (Exception e){
                    e.printStackTrace(); 
                }
            }
        }
    }
}

the XML files just have a Button and a ImageView and do not forget to declare in your android manifest the permissions:

<uses-feature android:name="android.hardware.camera"    android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"  />

for more informations a suggest: https://developer.android.com/training/camera/photobasics.html#TaskPhotoView https://www.youtube.com/watch?v=je9bdkdNQqg

like image 112
matmany Avatar answered Nov 14 '22 00:11

matmany


there are several example to capture image and store it and open it...

1. Android Camera API - Tutorial

2. Android Developers

3. Camera Example on Github

4. Another Example

like image 32
SilentKiller Avatar answered Nov 14 '22 02:11

SilentKiller