相机意图应用程序崩溃



嘿,有人可以帮我吗?我可以拍照,它将在画廊中显示,但我无法在ImageView中设置。该应用程序崩溃。我的代码

也许在活动中有问题?

 button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                File photoFile = null;
                try {
                    photoFile = createImageFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
String authorities = getApplicationContext().getPackageName() + ".fileprovider";
                Uri imageUri = FileProvider.getUriForFile(camtest.this, authorities, photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);

                if (photoFile != null) {
                    imageUri = FileProvider.getUriForFile(camtest.this,
                            BuildConfig.APPLICATION_ID + ".fileprovider",
                            photoFile);
                    Log.i("Uri", imageUri.toString());
//                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                }
                else{
                    Log.i("Uri", "none");
                }
                sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(photoFile)));
                startActivityForResult(intent, 7);


            }
        });

文件创建方法:

 File createImageFile() throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "LetsTalk_img" + timeStamp;
        File storageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(imageFileName,".jpg", storageDirectory);
        mImageFileLocation = image.getAbsolutePath();
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(image)));
        return image;
    }

和on activity result:这是我的错误吗?

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 7 && resultCode == RESULT_OK) {

        Bitmap bitmap = (Bitmap) data.getExtras().get("data");
            imageView.setImageBitmap(bitmap);

        }
    }

预先感谢

您只将cameraIntent()方法放入按钮单击功能。

private void cameraIntent() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 0);
    }

然后在粘贴后。

    @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == Activity.RESULT_OK) {
                if (requestCode == 0){
                    onCaptureImageResult(data);
           }
            }
        }

用于在缩略图中设置的图像视图变量中的数据:

private void onCaptureImageResult(Intent data) {
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
        byteArray = bytes.toByteArray();
        encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
        File destination = new File(Environment.getExternalStorageDirectory(),
                System.currentTimeMillis() + ".jpg");
        FileOutputStream fo;
        try {
            destination.createNewFile();
            fo = new FileOutputStream(destination);
            fo.write(bytes.toByteArray());
            fo.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        imagebox.setImageBitmap(thumbnail);
    }

最新更新