0

I'm making an android application, when clicking on a button it opens the camera however after taking the picture the image doesn't save. I want to be able to save it into my gallery album.

public void onClickbtnCamera(View v){

    Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    Uri uriSavedImage=Uri.fromFile(new File("/storage/emulated/0/DCIM/Camera/"));
    intent1.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
    startActivityForResult(intent1,3);

}
Craig Gallagher
  • 235
  • 1
  • 5
  • 19

1 Answers1

0

You are passing in a directory in EXTRA_OUTPUT. EXTRA_OUTPUT needs to point to a file:

package com.commonsware.android.camcon;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import java.io.File;

public class CameraContentDemoActivity extends Activity {
  private static final int CONTENT_REQUEST=1337;
  private File output=null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File dir=
        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

    output=new File(dir, "CameraContentDemo.jpeg");
    i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));

    startActivityForResult(i, CONTENT_REQUEST);
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode,
                                  Intent data) {
    if (requestCode == CONTENT_REQUEST) {
      if (resultCode == RESULT_OK) {
        Intent i=new Intent(Intent.ACTION_VIEW);

        i.setDataAndType(Uri.fromFile(output), "image/jpeg");
        startActivity(i);
        finish();
      }
    }
  }
}

(from this sample project)

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • It's letting me take the photo but for some reason it still isn't saving it in the Gallery folder – Craig Gallagher Oct 26 '15 at 19:53
  • @CraigGallagher: There is no "Gallery folder". If you mean that some gallery-type app on your device is not showing the image, that is because the camera app did not ask the `MediaStore` to index the file. Use `MediaScannerConnection` and its `scanFile()` method to do that yourself, as part of `onActivityResult()`. – CommonsWare Oct 26 '15 at 19:54