Create a file from drawable

后端 未结 4 1317
滥情空心
滥情空心 2020-12-30 07:04

I have a large number of resources in my drawable folder.All are having big size more than 500KB. I have to load all these 25 images all at once in a srollView. As usual I r

4条回答
  •  盖世英雄少女心
    2020-12-30 07:23

    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){}
        }
    

提交回复
热议问题