2014-03-13 43 views
0
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){ 
} 
+0

你得到了什么drawableResourceId? – Luciano

+0

@Luizje图片的ID logo_image.png – NickUnuchek

+0

好的,所以不是0.你有没有在清单中设置权限?你为什么不设置文件的目录?如果文件不存在,应该先创建文件? – Luciano

回答

2

1。检查是否有一组权限:

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

2 .SET输出文件

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