打开相机意图后,设备屏幕将变黑

时间:2019-02-13 11:35:08

标签: android android-camera-intent

[已解决]我不知道为什么要这么做,但是我要做的就是重启手机,然后手机又开始工作了。祝大家编码愉快! :)

我一直在开发一个应用程序,您可以在其中捕获图像并将其发布。相机意图以前一直很好,但是今天,当我在手机上构建应用程序并尝试在完全不同的活动中进行了几次更改(完全不会影响相机)后,打开相机意图时,整个应用程序冻结并且几秒钟不执行任何操作后,它显示了一个黑屏,在屏幕底部我什至看不到手机的“家”,“后退”和“关闭应用”按钮。该应用程序不会崩溃,也不会给我任何错误消息,但是在按下手机主页按钮应位于的位置并随后关闭该应用程序之前,黑屏并没有消失。

我已请求允许使用相机,并且可以正常使用,但随后出现黑屏。就像说的那样,相机在开发的许多星期中一直在工作,但直到今天它才开始这样做。

这是我的相机意图代码:

    //ADD NEW SPOT
public void addNewSpot(View v)
{
    //CHECKING PERMISSION FOR CAMERA
    if (checkCallingOrSelfPermission(android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
    {
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.CAMERA}, MY_CAMERA_PERMISSION_CODE);
    }
    else
    {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null)
        {
            File photoFile = null;
            try
            {
                photoFile = createImageFile();
            }
            catch (IOException ex)
            {
                ex.printStackTrace();
            }
            if (photoFile != null)
            {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
                StrictMode.setVmPolicy(builder.build());
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
    }
}


//CHECK FOR THE RESULT FROM CAMERA ACTIVITY
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == MY_CAMERA_PERMISSION_CODE)
    {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
        {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (takePictureIntent.resolveActivity(getPackageManager()) != null)
            {
                File photoFile = null;
                try
                {
                    photoFile = createImageFile();
                }
                catch (IOException ex)
                {
                    ex.printStackTrace();
                }
                if (photoFile != null)
                {
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
                    StrictMode.setVmPolicy(builder.build());
                    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
                }
            }
        }
    }
}


//CREATE FOLDER FOR THE IMAGE
String imageFilePath;

private File createImageFile() throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    String imageFileName = "IMG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    imageFilePath = image.getAbsolutePath();
    return image;
}


//GET TAKEN IMAGE, DOWNSCALE IT AND SEND IT TO CROPPING
File compressedImageFile;

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
    {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(imageFilePath);
        try
        {
            compressedImageFile = new Compressor(this).compressToFile(f);
        }
        catch (Exception e)
        {
            String message = e.getMessage();
            Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        }
        Uri contentUri = Uri.fromFile(compressedImageFile);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);

        CropImage.activity(contentUri)
                .setGuidelines(CropImageView.Guidelines.ON)
                .setAspectRatio(1, 1)
                .start(this);
    }

    //CROP THE IMAGE
    if(requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
    {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);

        if(resultCode == RESULT_OK)
        {
            Uri resultUri = result.getUri();

            Intent addPostIntent = new Intent(MainActivity.this, add_new_spot.class);
            addPostIntent.putExtra("imagePath", resultUri.toString());
            startActivity(addPostIntent);
        }
        else
        {
            Toast.makeText(this, "Image can't be cropped, please try again", Toast.LENGTH_SHORT).show();
        }
    }
}

我希望这种意图可以正常工作,因为它以前一直可以运行,但是相反,它会使应用程序冻结在黑屏中,并迫使用户重新启动应用程序。预先谢谢你。

0 个答案:

没有答案