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();
        }

1 Answer 1

1

Replace this

InputStream is = assetManager.open(fileName);       

with

 InputStream is = getResources().openRawResource(R.raw.fusrodah);
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.