保存后如何在Uri之后访问全尺寸照片

问题描述 投票:0回答:1

正如文档中描述的Documentation我试图通过意图来访问全尺寸图像。据我所知,我已经按照文档中的每一步进行操作。现在我想知道如何访问该文件onActivityResult()。因为该方法的Intent为null。

activity.Java

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(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.example.chathurangashan.cameraintent",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }

    }

    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 = 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;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE) {
            if (resultCode == RESULT_OK) {
                if (data.hasExtra(MediaStore.EXTRA_OUTPUT)) {
                    Uri uri = (Uri) data.getParcelableExtra(MediaStore.EXTRA_OUTPUT);
                }
            }
        }
    }

AndroidManifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.chathurangashan.cameraintent">

    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-feature android:name="android.hardware.camera" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.example.chathurangashan.cameraintent"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"/>
        </provider>


        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

file_path.chml

<?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.chathurangashan.cameraintent/files/Pictures" />
</paths>

file_path.xml保存在'res'的资源文件夹下

PS:我知道我可以直接在onActivityResult()中获取位图,因为文档描述在“获取缩略图”但它没有返回完整尺寸的图像。它是捕获图像的低质量版本。

android android-camera
1个回答
0
投票

你的dispatchTakePictureIntent()应该返回一个intent,这样你就可以使用MediaStore.EXTRA_OUTPUT作为键来获取Uri路径。

    private Intent dispatchTakePictureIntent() {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            // Ensure that there's a camera activity to handle the intent
            if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                // Create the File where the photo should go
                File photoFile = null;
                try {
                    photoFile = createImageFile();
                } catch (IOException ex) {
                    // Error occurred while creating the File
                }
                // Continue only if the File was successfully created
                if (photoFile != null) {
                    Uri photoURI = FileProvider.getUriForFile(this,
                            "com.example.chathurangashan.cameraintent",
                            photoFile);
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                }
            }
           return takePictureIntent;
        } 

现在你应该像这样开始你的意图并将它存储在全球某个地方以便以后使用。

intentImage = dispatchTakePictureIntent();
startActivityForResult(intentImage, REQUEST_IMAGE_CAPTURE); 

现在在你的onActivityResult做这样的事情。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE) {
        if (resultCode == RESULT_OK) {
            if (intentImage.hasExtra(MediaStore.EXTRA_OUTPUT)) {
                //your uri from saved intent 
                Uri uri = Uri.parse(intentImage.getStringExtra(MediaStore.EXTRA_OUTPUT));
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.