Android - Taking photos and saving them with a custom name to a custom destination via Intent

 ̄綄美尐妖づ 提交于 2019-11-26 22:25:39
Razgriz

Here's the code that made it work:

//camera stuff
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

//folder stuff
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();

File image = new File(imagesFolder, "QR_" + timeStamp + ".png");
Uri uriSavedImage = Uri.fromFile(image);

imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(imageIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

It opens the camera and takes exactly one shot (it goes back to the main activity after the user saves the image that was taken. It saves the image to the specified folder.

Dharmendra

You can get your photo in your folder but for that you will have to pass the filename with the intent using below code

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File out = Environment.getExternalStorageDirectory();
out = new File(out, imagename);
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
startActivityForResult(i, CAMERA_RESULT);

The out file contains the full path and the file name.

Now handle the onActivityResult() method and check the status of the result if the result is RESULT_OK then use the file name which you have supplied with the intent to access the photo.

You can refer How to get camera result as a uri in data folder? for more options.

Edit

You are getting data.getData() to null because it will be null if you pass the file url with the intent. Indeed you have already file path which you are passing with the Intent so why you are trying to fetch it from the bundle in onActivityResult() ?

sachin

this may help u

File file=new File(Environment.getExternalStorageDirectory(),"file name");

button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        opencam();
    }
});

protected void opencam() {
    Intent in = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    
    in.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(file));
    startActivityForResult(in,1);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!