2014-03-13 123 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();