【问题标题】:Copying file from asset folder to sdcard doesn't seem to work将文件从资产文件夹复制到 sdcard 似乎不起作用
【发布时间】:2015-07-19 12:18:10
【问题描述】:

我正在尝试将图像从资产文件夹复制到 sdcard,但似乎没有在首次启动时复制它。它可以创建文件夹,但不会复制文件。

prefs = getPreferences(Context.MODE_PRIVATE);
    if (prefs.getBoolean("firstLaunch", true)) {
        prefs.edit().putBoolean("firstLaunch", false).commit();
    File nfile=new File(Environment.getExternalStorageDirectory()+"/My Images");
    nfile.mkdir();
    }
    AssetManager assetManager = getAssets();
    String[] files = null;
    try {
        files = assetManager.list("middle.jpg");
    } catch (IOException e) {
        Log.e("tag", "Failed to get asset file list.", e);
    }
    for(String filename : files) {
        InputStream in = null;
        OutputStream out = null;
        try {
          in = assetManager.open(filename);
          File outFile = new File(Environment.getExternalStorageDirectory()+ "/My Images" + filename);
          out = new FileOutputStream(outFile);
          copyFile(in, out);
        } catch(IOException e) {
            Log.e("tag", "Failed to copy asset file: " + filename, e);
        }     
        finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {

                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {

                }
            }
        }  
    }

   private void copyFile(InputStream in, OutputStream out) {
    // TODO Auto-generated method stub

}

middle.jpg 是我要复制的文件。谁能告诉我我做错了什么?

PS 我的清单中有WRITE_EXTERNAL_STORAGE。

谢谢

【问题讨论】:

    标签: android android-assets


    【解决方案1】:

    您在构建路径时忘记在 /My images 的末尾添加 /

    File outFile = new File(Environment.getExternalStorageDirectory()+ "/My Images/" + filename); out = new FileOutputStream(outFile);

    因为文件名是 MyImages+Filename 所以它不存在用于复制。

    【讨论】:

    • mmmm 我现在真的觉得自己很蠢 :-) 谢谢你的帮助 :-)
    猜你喜欢
    • 2014-01-30
    • 2011-06-25
    • 2012-01-14
    • 1970-01-01
    • 2011-06-23
    • 2011-05-25
    • 1970-01-01
    • 2011-08-07
    • 1970-01-01
    相关资源
    最近更新 更多