从相机应用程序中获取全尺寸图像

Taking the full-size image from a camera app

我的应用程序有一个选项可以从相机拍摄照片。我正在使用 phone 的默认相机。 问题是当我拍照时,在我的 onActivityResult 中,我从 intent(data) 中得到 null。所以它显示消息未拍摄照片!”。

调用 Intent 的代码:

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // create Intent to take a picture and return control to the calling application
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            // Create a File reference to access to future access
            photoFile = getPhotoFileUri(photoFileName);

            // wrap File object into a content provider
            // required for API >= 24
            // See https://guides.codepath.com/android/Sharing-Content-with-Intents#sharing-files-with-api-24-or-higher
            Uri fileProvider = FileProvider.getUriForFile(MainActivity.this, "com.example.android.fileprovider", photoFile);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileProvider);

            // If you call startActivityForResult() using an intent that no app can handle, your app will crash.
            // So as long as the result is not null, it's safe to use the intent.
            if (intent.resolveActivity(getPackageManager()) != null) {
                // Start the image capture intent to take photo
                startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
            }
        }
    });
}

onActivityResult:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK && data != null) {

// by this point we have the camera photo on disk
            Bitmap takenImage = BitmapFactory.decodeFile(String.valueOf(photoFile));
            // RESIZE BITMAP, see section below
            // Load the taken image into a preview
            img.setImageBitmap(takenImage);
        } else { // Result was a failure
            Toast.makeText(this, "Picture wasn't taken!", Toast.LENGTH_SHORT).show();
        }
    }

为照片制作文件:

public File getPhotoFileUri(String fileName) {
    // Only continue if the SD Card is mounted
    if (isExternalStorageAvailable()) {
        // Get safe storage directory for photos
        // Use `getExternalFilesDir` on Context to access package-specific directories.
        // This way, we don't need to request external read/write runtime permissions.
        File mediaStorageDir = new File(
                getExternalFilesDir(Environment.DIRECTORY_PICTURES), APP_TAG);

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()){
            Log.d(APP_TAG, "failed to create directory");
        }

        // Return the file target for the photo based on filename
        File file = new File(mediaStorageDir.getPath() + File.separator + fileName);

        return file;
    }
    return null;
}

AndroidManifest.xml

中的提供商标签
        <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.example.android.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"/>
    </provider>

file_paths.xml

    <?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="Android/data/com.example.houman.myapplication/files/Pictures" />
</paths>

"Taking Photos Simply | Android Developers"

之后

对我帮助不大,我试过了Accessing the Camera and Stored Media | CodePath

但现在我遇到了这个问题。

I get null from intent(data)

您没有使用 data,在 EXTRA_OUTPUT 场景中,相机应用无需在结果中提供 Intent。因此,通过替换:

来取消该检查
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK && data != null) {

与:

if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {