0

I got this code to copy file from App directory to folder on SD card and use that file to set as ringtone. But there is something that I don't understand. How can I determine which file to take? Let's assume I have 1 file named fusrodah in raw folder. How can I make my app to pick that file and copy it to sdcard folder?

private int size;

private static final int BUFFER_LEN = 1024;


private void copyFile(AssetManager assetManager, String fileName, File out) throws FileNotFoundException, IOException {
    size = 0;
    FileOutputStream fos = new FileOutputStream(out);
    InputStream is = assetManager.open(fileName);       
    int read = 0;
    byte[] buffer = new byte[BUFFER_LEN];
     while ((read = is.read(buffer, 0, BUFFER_LEN)) >= 0) {
            fos.write(buffer, 0, read);
            size += read;
      }
    fos.flush();    
    fos.close();
    is.close();
}
public void onClick(View arg0) {        
    AssetManager assetManager = getAssets();

    File file = new File(Environment.getExternalStorageDirectory(),
            "/myRingtonFolder/Audio/");
    if (!file.exists()) {
        file.mkdirs();
    }

    String path = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/myRingtonFolder/Audio/";

    File out = new File(path + "/", "fusrodah.mp3");     
    if(!out.exists()){
        try {
            copyFile(assetManager, "fusrodah.mp3", out);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Slim C.
  • 1,119
  • 4
  • 25
  • 49

1 Answers1

1

Replace this

InputStream is = assetManager.open(fileName);       

with

 InputStream is = getResources().openRawResource(R.raw.fusrodah);
Sonali8890
  • 2,005
  • 12
  • 16