Slim C.
Slim C.

Reputation: 1127

How to chose which file to copy?

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

Upvotes: 0

Views: 42

Answers (1)

Sonali8890
Sonali8890

Reputation: 2005

Replace this

InputStream is = assetManager.open(fileName);       

with

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

Upvotes: 1

Related Questions