无法将可绘制资源转换为文件


I'm trying to convert file *logo_image.png* in `drawable/` folder to a `File`. 

以下代码失败,因为fnull;

int drawableResourceId = 
     context.getResources().getIdentifier("logo_image", 
                                          "drawable", 
                                           context.getPackageName());                 
File f=new File("logo_image"+".png");
try
{         
     InputStream inputStream = (InputStream) 
                                  context.getResources()
                                            .openRawResource(drawableResourceId);
     OutputStream out=new FileOutputStream(f);
     byte buf[]=new byte[1024];
     int len;
     while((len=inputStream.read(buf))>0)
         out.write(buf,0,len);
     out.close();
     inputStream.close();
}
catch (IOException e){
}  

1。检查是否设置了权限:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

2 .设置输出文件的输出目录

3。

调用f.createNewFile();

相关内容

  • 没有找到相关文章

最新更新