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

时间:2014-03-13 14:28:43

标签: android file drawable

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 个答案:

答案 0 :(得分:2)

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

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

2.设置输出文件的目录

3。检查文件是否存在,否则调用f.createNewFile();

相关问题