如何选择要复制的文件?

时间:2014-02-19 13:10:34

标签: java android string copy

我有这个代码将文件从App目录复制到SD卡上的文件夹,并使用该文件设置为铃声。 但有一些我不明白的东西。 如何确定要采用的文件? 假设我在raw文件夹中有一个名为fusrodah的文件。 如何让我的应用程序选择该文件并将其复制到sdcard文件夹?

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 个答案:

答案 0 :(得分:1)

替换此

InputStream is = assetManager.open(fileName);       

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