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

前端 未结 3 878
日久生厌
日久生厌 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:37

    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.

提交回复
热议问题