How to make Android Camera Intent return full size picture

前端 未结 2 1099
终归单人心
终归单人心 2020-12-09 13:33

I am using the jengelsma code from this post Capture Image from Camera and Display in Activity to get an image from the camera.

When I get the returned image it is o

2条回答
  •  甜味超标
    2020-12-09 14:02

    You have to do a bit extra to get a fullsize picture, it doesn't pass it in the Intent, but stores it in a file.

    To start the camera for this, add an extra to your Intent:

    Intent intent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE );
    
    fileUri = getOutputMediaFileUri();
    intent.putExtra( MediaStore.EXTRA_OUTPUT, fileUri );
    

    getOutputMediaFileUri looks like:

    /** Create a file Uri for saving an image or video */
    private static Uri getOutputMediaFileUri(){
      return Uri.fromFile(getOutputMediaFile());
    }
    
    /** Create a File for saving an image or video */
    private static File getOutputMediaFile(){
        // To be safe, you should check that the SDCard is mounted
        // using Environment.getExternalStorageState() before doing this.
    
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "MyCameraApp");
        // This location works best if you want the created images to be shared
        // between applications and persist after your app has been uninstalled.
    
        // Create the storage directory if it does not exist
        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }
    
        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile = new File(mediaStorageDir.getPath() + File.separator +
            "IMG_"+ timeStamp + ".jpg");
    
        return mediaFile;
    }
    

    Naturally you can modify the method that creates the file however you like to modify the naming.

    Then in your onActivityResult you can get the URI to the picture by simply calling getData() on the Intent.

提交回复
热议问题