如何使用Android Camera API以原始大小保存图像

时间:2018-03-11 15:03:09

标签: android android-camera-intent

我已经在片段中编写了一些代码来打开相机应用程序,一旦拍完照片,就会保存照片。我无法保存完整尺寸的图像,节省了较低的res版本。谁能发现我出错的地方?

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
            mNewPhotoPath = getContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES) + "/" +
                    photoFile.getName();
        } catch (IOException ex) {
            // Error occurred while creating the File
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(getContext(),
                    "com.example.mmackenz.myholidays.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);

        }
    }
}

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

这是我在onActivityResult中的相关情况:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        File newImage = new File(mNewPhotoPath);
        images.add(newImage);
        adapter.notifyDataSetChanged();
        Photo newPhoto = new Photo(newImage.getAbsolutePath(), newImage.getName(), -1, -1);
        DatabaseHandler db = DatabaseHandler.getInstance(getContext());
        db.addPhoto(newImage.getAbsolutePath(), newImage.getName(), -1,-1);
    }

}

它将新文件添加到数组列表中,该数组列表显示在回收器视图中,还有照片表中文件的信息。

由于

0 个答案:

没有答案