2014年8月7日 星期四

Create a file from drawable

infor from; http://stackoverflow.com/questions/5895073/create-a-file-from-drawable

You can open an InputStream from your drawable resource using following code:
InputStream is = getResources().openRawResource(id);
here id is the identifier of your drawable resource. for eg: R.drawable.abc
Now using this input stream you can create a file. If you also need help on how create a file using this input stream then tell me.
Update: to write data in a file:
try
    {
    File f=new File("your file name");
    InputStream inputStream = getResources().openRawResource(id);
    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){}
    }

沒有留言:

張貼留言