beans
beans

Reputation: 1765

Write a file in android?

I get my raw resource:

InputStream myRawResource = context.getActivity().getResources().openRawResource(myID);

How can I write this to a file? An MP3 file on the device.

Upvotes: 2

Views: 584

Answers (2)

Dave Newton
Dave Newton

Reputation: 160170

Use a FileOutputStream and normal Java file ops.

Upvotes: 0

user370305
user370305

Reputation: 109237

try
  {
  File f=new File("myFile.mp3");
  InputStream myRawResource = context.getActivity().getResources().openRawResource(myID);   
  OutputStream out=new FileOutputStream(f);
  byte buf[]=new byte[1024];
  int len;
  while((len=myRawResource.read(buf))>0)
  out.write(buf,0,len);
  out.close();
  myRawResource.close();
  System.out.println("\nFile is created........
...........................");
  }
  catch (IOException e){}
  }

Upvotes: 1

Related Questions