【问题标题】:Taking the full-size image from a camera app从相机应用程序获取全尺寸图像
【发布时间】:2018-01-11 20:34:11
【问题描述】:

我的应用程序有一个从相机拍照的选项。我正在使用手机的默认摄像头。 问题是当我拍照时,在我的 onActivityResult 中,我从意图(数据)中得到空值。所以它显示消息图片未拍摄!”。

调用 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

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

【问题讨论】:

    标签: java android camera android-camera-intent


    【解决方案1】:

    我从意图(数据)得到 null

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

    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK && data != null) {
    

    与:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-21
      相关资源
      最近更新 更多