5

I have went through several documentation and stacks, however I'm not quite sure how do I implement this...

And help or sample codes would really help me understand more.

Here are the sets of codes that runs the camera and it's working perfectly fine, my next question is, how do I let it automatically saved into phone gallery?

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        tvCameraTiles = (TextView) findViewById(R.id.tvCameraTiles);

        tvCameraTiles.setOnClickListener(new  View.OnClickListener()
        {
             @Override
             public void onClick(View v)
             {
                 dispatchTakePictureIntent();
             }
         });
    }

    private void dispatchTakePictureIntent()
    {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        if (takePictureIntent.resolveActivity(getPackageManager()) != null)
        {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
        {
            //Get Camera Image File Data
//            Bundle extras = data.getExtras();
//            Bitmap imageBitmap = (Bitmap) extras.get("data");
//            mImageView.setImageBitmap(imageBitmap);
        }
    }
  • If it works perfectly fine then where can you see the pictures taken? – greenapps Jan 21 '17 at 10:46
  • @greenapps no where, since all it does is returns to activity result, therefore the picture wouldnt be saved –  Jan 21 '17 at 11:03
  • It is normal that it returns to on activity result. That happens always. Who tells you that the picture is not saved? That you do not see it in the Gallery app is not the same as concluding that it is not saved some where. – greenapps Jan 21 '17 at 11:38
  • Do you see the new pictures if you switch your device off/on? – greenapps Jan 21 '17 at 11:39

3 Answers3

5

have you read this https://developer.android.com/training/camera/photobasics.html ?

Especially the part:

private void galleryAddPic() {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

You don't seem to save the photo in the external storage, so it should work.

EDIT: I tried to make a really basic application following the documentation with the method galleryAddPic.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button photoButton = (Button) findViewById(R.id.photobutton);
    photoButton.setOnClickListener(new  View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            dispatchTakePictureIntent();
        }
    });
}

static final int REQUEST_IMAGE_CAPTURE = 1;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        //Bundle extras = data.getExtras();
        //Bitmap imageBitmap = (Bitmap) extras.get("data");
        //mImageView.setImageBitmap(imageBitmap);
        galleryAddPic();
    }
}

String mCurrentPhotoPath;

private File createImageFile() throws IOException {
    File storageDir = Environment.getExternalStorageDirectory();
    File image = File.createTempFile(
            "example",  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
}

private void galleryAddPic() {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

This is working for me! I take a picture using the camera app, and then it's showed in the gallery app. I modified the code regarding the FileProvider part, now it passes the Uri with the help of the method Uri.fromFile(photoFile). I save the photo in a different position too, check the code in the method createImageFile for this one.

In the Android Studio emulator is working fine, let me know about you.

Rex B
  • 51
  • 6
  • Hi, I did read this documentation, however i just can't figure out where do I get the variable -> mCurrentPhotoPath –  Jan 21 '17 at 10:01
  • mCurrentPhotoPath is a global variable. In the example given in the documentation is set when the temporary file is created. – Rex B Jan 21 '17 at 10:09
  • android:resource="@xml/file_paths" –  Jan 21 '17 at 10:22
  • where does the xml/file_paths leads to? –  Jan 21 '17 at 10:23
  • It seems you have to configure a FileProvider in your app manifest. In the same documentation there is an example for this. – Rex B Jan 21 '17 at 10:37
  • Honestly I don't know a lot about this, but in the example it says that file_paths.xml is a file in res/xml/file_paths; if it's not there, you can create it. – Rex B Jan 21 '17 at 10:40
  • my final issue is this now -> com.example.android.fileprovider –  Jan 21 '17 at 11:50
  • I edited my answer, check it out. I didn't modify anything in the manifest.xml (like FileProvider), just make sure you declare the permissions WRITE_EXTERNAL_STORAGE and CAMERA and that they are checked in the phone settings (if API=>23, permissions need to be checked run-time by the app, it's not automatic) – Rex B Jan 21 '17 at 13:23
2

I tried out Rex B code snippet, but ended up getting a error on Android Studio.

 android.os.FileUriExposedException: file:///storage/emulated/0/example6941495009290613124.jpg exposed beyond app through ClipData.Item.getUri()

If anyone got this error, I found a simple fix

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());

I added these two lines to the onClick method

  Button photoButton = (Button) findViewById(R.id.photobutton);
    photoButton.setOnClickListener(new  View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
            dispatchTakePictureIntent();
        }
    });
}

I know this is old post, but I'm sure someone will come by this one day.

0

First the file will be created and the ByteStream will be written on the file and saved. you don't do anything for this file to be present in the gallery. This will automatically refreshed by gallery.

Noorul
  • 3,259
  • 2
  • 31
  • 46
  • Mhm, well I'm using Redmi Note 4 to test my product, and it certainly don't seem to have appear on my gallery –  Jan 21 '17 at 10:02
  • post me the path where the image saved. if you saved on some temporary folder, gallery will not show. try to post the path of the image file. – Noorul Jan 21 '17 at 10:19