Failed to save image to sdcard

后端 未结 4 1865
别跟我提以往
别跟我提以往 2021-01-13 17:11

I try to download an image with this code and save it : in this line:

OutputStream os =new FileOutputStream(f);

i try it on the emulator.my

4条回答
  •  独厮守ぢ
    2021-01-13 17:57

    Try this to get the folder (and create it if it doesn't exist) and then use the f File var as you were doing in your code:

        File cacheDirectory;
    
        if ( android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED ))
            cacheDirectory = new File( android.os.Environment.getExternalStorageDirectory(), "myFolder/" );
        else
            cacheDirectory = this.getCacheDir();
    
        if( !cacheDirectory.exists() )
            cacheDirectory.mkdirs();
    
        File f = new File( cacheDirectory, "my_file.jpg" );
    

    Read the link for getCacheDir(): http://developer.android.com/reference/android/content/Context.html#getCacheDir%28%29

提交回复
热议问题