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

前端 未结 3 879
日久生厌
日久生厌 2020-11-28 10:41

I have a program that opens the camera via Intent to take a photo. That much part works fine already. However, I want it to save to a certain folder with a certain file name

3条回答
  •  感情败类
    2020-11-28 11:38

    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() ?

提交回复
热议问题