保存相机图像时遇到问题

时间:2019-11-09 03:05:08

标签: image camera save

我浏览了所有内容,似乎找不到解决我问题的答案。
我的应用程序中有一个图像文件,还有一个按钮...我正在遵循android开发人员https://developer.android.com/training/camera/photobasics#java上的教程,但是在保存文件时遇到了问题。有一次,我能够保存到缓存,但是我宁愿保存到图片中的文件夹。 这是我的代码...

    static final int REQUEST_IMAGE_CAPTURE = 1;

    private void dispatchTakePictureIntent() {

        Intent dispatchTakePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (dispatchTakePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            photoFile = createImageFile();
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.example.ConfinedSpaceManagement",
                        photoFile);
                dispatchTakePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(dispatchTakePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            imageView.setImageBitmap(imageBitmap);

        }
    }

    String currentPhotoPath;

    private File createImageFile(){
        // Create an image file name
        File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        if (!storageDir.exists()) {
            if (!storageDir.mkdirs()) {
                Log.d("Confined_Space", "failed to create Directory");
                return null;
            }
        }
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        return new File(storageDir.getPath() + File.separator + "ConSp" + timeStamp + ".jpg");
    }
    //String imageFileName = "JPEG_" + timeStamp + "_";
    ////////////////////////////////////////////////////////////////
    static final int REQUEST_TAKE_PHOTO = 1;
}

这是我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.confinedspacemanagement">

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

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:maxSdkVersion="18" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <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">
        <meta-data
            android:name="com.google.android.actions"
            android:resource="@xml/file_paths" />

        <activity android:name=".Signature" />
        <activity android:name=".AuditWork" />
        <activity android:name=".DisplayIndividual" />
        <activity android:name=".SecondActivity" />
        <activity android:name=".Definition" />
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

      <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.example.confinedspacemanagement.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"/>
        </provider>
    </application>

</manifest>

这是我的xml.file_path

<?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.package.name/files/Pictures" />
</paths>

该活动是一个sqlite表单页面。我正在尝试拍照,将其保存到指定的文件中,然后将文件路径保存到数据库中。

有任何帮助吗???我应该使用camera2吗?

1 个答案:

答案 0 :(得分:0)

我在https://www.youtube.com/watch?v=VqgZxiU2knM找到了PRABEESH R K先生的答案。

图像保存得很好,但是我正在从不是主要活动的活动中拍摄照片。当我返回结果时,页面返回到主要活动,而不是最初请求照相机的页面。有什么办法可以将其保留在发送页面上?