安卓工作室 - 一秒钟后相机冻结

时间:2017-03-31 02:36:38

标签: android android-studio android-intent android-camera freeze

相机似乎启动约1-2秒,然后冻结,仿真器关闭。我似乎无法弄清问题是什么。这是API 23 Marshmallow。

我有

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

在我的AndroidManifest.xml上。

    takePhotoButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (ContextCompat.checkSelfPermission(ProfileCreationActivity.this, Manifest.permission.CAMERA)
                    == PackageManager.PERMISSION_GRANTED) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Camera_Photo");
                if (!mediaStorageDir.exists()){
                    mediaStorageDir.mkdirs();
                }
                File mediaFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Camera_Photo");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mediaFile));
                startActivityForResult(intent, CAM_REQUEST);

            } else {
                requestPermissions(new String[]{ Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_CAM_REQUEST);
            }

        }
    });


    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if (requestCode == CAM_REQUEST) {
        if (resultCode == RESULT_OK) {
            String filename = "IMG_test1.jpg";
            photoIV.setImageURI(file);

        }
    }

}

谢谢。

0 个答案:

没有答案