android File.createTempFile()失败,带有Environment.getExternalStorageDirector()

时间:2018-04-07 16:31:29

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

我正在尝试编写一个带照片的应用并将其保存到图库中。

我跟随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时出错了

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

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

Google获取运行时权限。

今年你是#1246谁发布了这个问题。

答案 1 :(得分:0)

我通过解决问题找到了解决方案。 我让相机意图将图片保存到私人app文件夹, 然后我将文件移动到公共文件。这是代码

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
}