android File.createTempFile() 失败 Environment.getExternalStorageDirector()

android File.createTempFile() fails with Environment.getExternalStorageDirector()

我正在尝试编写一个应用程序来拍摄照片并将其保存到图库中。

我遵循了 Android 开发者指南,但它只说明了如何将相同的图像放入私有目录。

这是我用来将意图发送到相机的代码

public void dispatchTakePictureIntent(View view) {

    if (ContextCompat.checkSelfPermission(MainActivity.this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                2121);

        return;

    }

    //File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

    File image = null;
    try {
        image= File.createTempFile("IMG", ".jpg", storageDir);
        //tempImage=image;

    } catch (IOException e) {
        errorMessage.setVisibility(View.VISIBLE);
        Log.e("error", Log.getStackTraceString(e));
        return;
    }

    //photoURI = FileProvider.getUriForFile(this, "prova.fileprovider", image);

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null ) {
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }

}

当我在 Android Studio 模拟器 (Pixel_API_27) 上 运行 时,调用 File.createTempFile

时出现错误

提前感谢您的帮助。

对于 Android 6+,您必须添加代码以要求用户确认您在清单中请求的读写权限。

Google 运行时权限。

发布此问题的您是今年的#1246。

我通过解决问题找到了解决方案。 我让相机意图将图片保存到私人应用程序文件夹, 然后我将文件移动到 public 一个。这是代码

public void dispatchTakePictureIntent(View view) {

    //ask write permission
    if (ContextCompat.checkSelfPermission(MainActivity.this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                2121);

        return;

    }

    //get path to private folder
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

    //create temp-file where the image will be saved
    File image = null;
    try {
        image= File.createTempFile("IMG", ".jpg", storageDir);

    } catch (IOException e) {
        errorMessage.setVisibility(View.VISIBLE);
        Log.e("error", Log.getStackTraceString(e));
        return;
    }


    Image = image;

    //get uri of the image
    Uri photoURI = FileProvider.getUriForFile(this, "prova.fileprovider", image);

    //send the intent to the camera
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null ) {
        takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoURI);
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }

}

然后拍完照片就拍

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { //AFTER TAKING PICTURE

        File publicImage = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/" + Image.getName());
        Image.renameTo(publicImage); //here we move the file to the public directory
        Image = publicImage;

        //optionally one can update the gallery, but I don't need it
}