尝试写入外部SD时出现android错误

尝试写入外部SD时出现android错误,android,Android,我正在尝试将jpg图像写入外部SD卡。但是,我得到的是System.errFileNotFoundException:/mnt/sdcard/test.images/temp/savedImage(没有这样的文件或目录)。创建目录似乎也失败了,在LogCat中给出了一个false,我在查看SD卡时也看不到文件夹 我的代码如下: if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

我正在尝试将jpg图像写入外部SD卡。但是,我得到的是System.err
FileNotFoundException:/mnt/sdcard/test.images/temp/savedImage(没有这样的文件或目录)
。创建目录似乎也失败了,在LogCat中给出了一个
false
,我在查看SD卡时也看不到文件夹

我的代码如下:

if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {   
        File folder = new File(Environment.getExternalStorageDirectory() + "/test.images/temp");
        try {
            if(!folder.exists()){
                boolean dir = new File(Environment.getExternalStorageDirectory() + "/test.images/temp").mkdir();
                Log.v("creating directory", Boolean.toString(dir));
            }
            File imageOutputFile = new File(Environment.getExternalStorageDirectory() + "/test.images/temp", "savedImage");
            FileOutputStream fos = new FileOutputStream(imageOutputFile);
            Image.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
 }
我有权限

在清单中,并已清理和重建

使用mkdirs()而不是mkdir()


Guido在评论中发布了一个适合我的解决方案。我将重复它以确保它是一个答案。

`File imageOutputFile=new File(Environment.getExternalStorageDirectory()+“/test.images/temp/”,“savedImage.jpg”)`