截取的图片可以保存到Images文件夹,但不能保存到app文件夹

Captured image can be saved to Images folder but cannot be saved to app folder

我可以将捕获的图像保存到图片文件夹,但是我不能将它保存到应用程序文件夹。我授予相机权限并动态写入权限。我在 manifests.xml 中写了读写相机权限。我在调试模式下检查了权限。权限没有问题

相机 activity 启动,我拍照并单击“确定”。然后在 onActivityResult() 中我检查了图像文件的 size.It 的零字节。图片文件存在但长度为零。

这是我检索图像路径的方法:

public static File getImageFile(Context context, int food_id) {
    try {
        //File storageDir = new File(context.getFilesDir().getAbsolutePath() + File.separator + IMAGES_DIRECTORY);    // not works !!!!!!!!!
        File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + File.separator + IMAGES_DIRECTORY); // works
        if (!storageDir.exists()) {
            storageDir.mkdirs();
        }

        File photoFile = new File(storageDir.getAbsolutePath() + File.separator + food_id + ".jpg");

       /* if(!photoFile.exists())
            photoFile.createNewFile();*/

        return photoFile;
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

if (Build.VERSION.SDK_INT >= 23) {
    hasPermissionCamera = ContextCompat.checkSelfPermission(FoodDetailsActivity.this, Manifest.permission.CAMERA);

    if (hasPermissionCamera != PackageManager.PERMISSION_GRANTED) {
        if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) { 
            getErrorDialog(getString(R.string.permission_error_dialog_camera), FoodDetailsActivity.this, true).show();
        } else { 
            requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_CODE_ASK_PERMISSIONS_CAMERA);
        }
    } else { // open camera
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (cameraIntent.resolveActivity(getPackageManager()) != null)  // intent düzgün mü diye kontrol eder.
        {

            File photoFile = AppUtil.getImageFile(FoodDetailsActivity.this,food_id);  

            if (photoFile != null) {
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
                StrictMode.setVmPolicy(builder.build());
                startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
            }
        } else {

        }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intentx) {
    super.onActivityResult(requestCode, resultCode, intentx);
   if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        File imageFile = AppUtil.getImageFile(this,food_id);
        try {
            mImageBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());  // mImageBitmap is null here. imageFile exists.
            Log.d("eeffs","size : " + imageFile.length() + " - exists() : " + imageFile.exists()); exists return true. length is zero

            int widthBitmap = mImageBitmap.getWidth();  // exception here because bitmap is null
            ...
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

i cannot save it to app folder

我猜你的意思是:

File storageDir = new File(context.getFilesDir().getAbsolutePath() + File.separator + IMAGES_DIRECTORY);    // not works !!!!!!!!!

第三方应用无法写入您应用的 internal storage 部分,您正在通过 ACTION_IMAGE_CAPTURE.

调用第三方相机应用

您可以使用 FileProvider 及其 getUriForFile() 方法来提供对您应用的内部存储部分的选择性访问。 This sample app 演示了该技术,我还写入了 getFilesDir().

内的一个位置

作为奖励,使用 FileProvider 可以让你摆脱那个丑陋的 StrictMode 黑客,你试图通过 file [=18] 的禁令=]计划。