【问题标题】:How to chose which file to copy?如何选择要复制的文件?
【发布时间】:2014-02-19 13:10:34
【问题描述】:

我得到这个代码来将文件从 App 目录复制到 SD 卡上的文件夹,并使用该文件设置为铃声。 但是有一点我不明白。 我如何确定要使用哪个文件? 假设我在 raw 文件夹中有 1 个名为 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();
        }

【问题讨论】:

    标签: java android string copy


    【解决方案1】:

    替换这个

    InputStream is = assetManager.open(fileName);       
    

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    • 2014-12-21
    相关资源
    最近更新 更多