FileUriExposedException when create separate folder for captured images

后端 未结 2 1088
礼貌的吻别
礼貌的吻别 2021-01-29 14:07

I am trying to store camera captured images at separate folder using below code but when i execute this code i am getting exception and i tried lot for getting solution but no u

2条回答
  •  盖世英雄少女心
    2021-01-29 14:43

    First just capture the image then in onActivityResult get the image as bitmap then save that to the path you want to save.

    private void openCamera()
    {
            // Start the camera and take the image
            // handle the storage part in on activity result
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAPTURE_IMAGE_REQUEST_CODE);
    }
    

    And inside on activity result method write the following code.

    if (requestCode == CAPTURE_IMAGE_REQUEST_CODE)
    {
         if (resultCode == RESULT_OK)
         {
             Bitmap imgBitmap = (Bitmap) data.getExtras().get("data");
             File sd = Environment.getExternalStorageDirectory();
             File imageFolder = new File(sd.getAbsolutePath() + File.separator +
                            "FolderName" + File.separator + "InsideFolderName");
    
             if (!imageFolder.isDirectory())
             {
                  imageFolder.mkdirs();
             }
    
             File mediaFile = new File(imageFolder + File.separator + "img_" +
                                System.currentTimeMillis() + ".jpg");
    
             FileOutputStream fileOutputStream = new FileOutputStream(mediaFile);
             imgBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fileOutputStream);
             fileOutputStream.close();
         }
    }
    

    This works for me in 7.1.1 and lower versions as well.

提交回复
热议问题