相机:API 24以下的设备不返回任何OnActivityResult

时间:2019-03-06 13:44:13

标签: android bitmap camera android-camera android-fileprovider

我在这里开始拍照活动

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                            File photoFile = null;
                            try {
                                photoFile = createImageFile();
                            } catch (IOException ex) {
                            }
                            if (photoFile != null) {
                                if (Build.VERSION_CODES.N <= android.os.Build.VERSION.SDK_INT) {

                                    photoURI = FileProvider.getUriForFile(getApplicationContext(),
                                            "za.co.keyfleet.driverapp.fileprovider",
                                            photoFile);

                                } else {

                                    photoURI = Uri.fromFile(photoFile);

                                }

                                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                                startActivityForResult(takePictureIntent, 1);
                            }
                        }

这称为OnActivityResult

File file = new File(mCurrentPhotoPath);
                        String filePath= file.getAbsolutePath();
                        Bitmap bitmap = BitmapFactory.decodeFile(filePath);


                        if (bitmap != null) {
                            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

                            Matrix matrix = new Matrix();
                            matrix.postRotate(0);
                            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),
                                    matrix, true);
                            bitmap.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream);
                            byteArray = byteArrayOutputStream.toByteArray();
                            String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
                            if (byteArray != null) {
                                mCheck.setChecked(true);
                            }
                        }else
                        {
                            Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
                        }

问题是它可以与API 24及更高版本完美配合,但降低它只会返回任何内容。我能够同时打开相机并返回没有错误,但只有“较新”的手机会返回位图,而较旧的手机会返回位图只是空的。我需要能够使用所有android设备拍照并返回照片。

0 个答案:

没有答案