为什么相机拍摄后图像无法保存到外部SD卡

时间:2018-07-02 16:19:31

标签: android

我正在使用相机意图将图像捕获到我的应用程序中。但是,使用摄像头功能后,图像未显示在imageView中。

我的应用程序的详细信息

最小SDK版本API25 Android 4.0.3 目标SDK版本API28

Android Studio版本3.1.2

清单

 <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/MyMaterialTheme"
        android:screenOrientation="portrait">

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.austurn.keikonew.keiko"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepath"></meta-data>
        </provider>

</application>

文件路径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.austurn.keikonew.keiko/files/Pictures" />
</paths>

相机点击事件

imgcam.setOnClickListener(new View.OnClickListener() {


            public void onClick(View v) {
                int permissionCheck =ContextCompat.checkSelfPermission(getActivity(),Manifest.permission.CAMERA);
                if (permissionCheck == PackageManager.PERMISSION_GRANTED){


                    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();
                        } catch (IOException ex) {
                            // Error occurred while creating the File

                        }
                        // Continue only if the File was successfully created
                        if (photoFile != null) {
                            photoURI = FileProvider.getUriForFile(getActivity(),
                                    "com.austurn.keikonew.keiko",
                                    photoFile);
                            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
                        }
                    }


                } else {
                    String[]permissionrequest ={Manifest.permission.CAMERA};
                                        ActivityCompat.requestPermissions(getActivity(), new String[] {Manifest.permission.CAMERA}, requestCode);
                }
          }

    });

创建图片

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

关于活动结果

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){

    try {

            if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
                Uri uri = photoURI;
                Bitmap myImg = BitmapFactory.decodeFile(photoFile.getAbsolutePath());
                Matrix matrix = new Matrix();
                matrix.postRotate(90);
                int width = myImg.getWidth();
                int height=myImg.getHeight();
                Bitmap scaledBitmap = Bitmap.createScaledBitmap(myImg, width, height, true);
                Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
                camview.setImageBitmap(rotatedBitmap);
            }
    }catch(Exception e){
        Toast.makeText(this.getActivity(), "Something went wrong", Toast.LENGTH_LONG).show();
    }
}

0 个答案:

没有答案