0

I just want to save a picture in my Imagefolder in my phone. I have got 2 examples which I tried.

1. Example

My app crashes when I activate the onClick Method:

public void onClick(View arg0) {

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

        startActivityForResult(cameraIntent, 1337);
}});

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

     if( requestCode == 1337)
            {
                Bitmap thumbnail = (Bitmap) data.getExtras().get("data");

MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());

            }
            else 
            {
                Toast.makeText(AndroidCamera.this, "Picture NOt taken", Toast.LENGTH_LONG);
            }
            super.onActivityResult(requestCode, resultCode, data);
        }

2. Example

Before I saved my taken Picture with Uri. But it saved my picture in a folder, which I can only access on my PC or with a FileApp. I don´t know how I can change the Path direction with Uri to my existing default image folder in my phone.

Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,  new ContentValues());
csnewb
  • 1,190
  • 2
  • 19
  • 37

1 Answers1

1

This is how I manage with saving images to specified imagefolder

When starting camera intent I define path and directory, where my image should be saved, and pass this as intetn extra when starting camera:

    private void startCameraIntent() {
        //create file path
        final String photoStorePath = getProductPhotoDirectory().getAbsolutePath();

        //create file uri
        final Uri fileUri = getPhotoFileUri(photoStorePath);

        //create camera intent
        final Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        //put file ure to intetn - this will tell camera where to save file with image
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        // start activity
        startActivityForResult(cameraIntent, REQUEST_CODE_PHOTO_FROM_CAMERA);

        //start image scanne to add photo to gallery
        addProductPhotoToGallery(fileUri);
    }

And here are some of helper methods used in code above

    private File getProductPhotoDirectory() {
        //get directory where file should be stored
        return new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES),
                "myPhotoDir");
    }

    private Uri getPhotoFileUri(final String photoStorePath) {

        //timestamp used in file name
        final String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
              Locale.US).format(new Date());

        // file uri with timestamp
        final Uri fileUri = Uri.fromFile(new java.io.File(photoStorePath 
              + java.io.File.separator + "IMG_" + timestamp + ".jpg"));

        return fileUri;
    }

    private void addProductPhotoToGallery(Uri photoUri) {
        //create media scanner intetnt
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

        //set uri to scan
        mediaScanIntent.setData(photoUri);
        //start media scanner to discover new photo and display it in gallery
        this.sendBroadcast(mediaScanIntent);
   }
Filip Zymek
  • 2,626
  • 4
  • 22
  • 31
  • Ty for your answer. That just opened the default camera. I´ve got a preview camera with an overlay, which dissappears by taking a photo. I don´t know exactly what the Intent camera is doing, but I think, that I have to do it without it? :) – csnewb Aug 07 '13 at 21:53
  • yes, if your application is managing the camera, there is no need to launch default application. – Filip Zymek Aug 09 '13 at 12:11