【问题标题】:I can't manage to copy files from assets to external storage我无法将文件从资产复制到外部存储
【发布时间】:2012-04-14 12:52:47
【问题描述】:

我正在制作一个 Android 应用程序,我想添加两个名为 fsx.xmlxplane.xml 的文件。这是我正在使用的代码,它运行完美,没有错误,但 /planesim 只是显示为空。请帮忙!

String planesimFolderName = "/planesim";
String fsxFile = "fsx.xml";
String xplaneFile = "xplane.xml";
String asset;
File assetDestination;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    final File planesimFolder = new File(Environment.getExternalStorageDirectory() + planesimFolderName);
    final AssetManager assetManager = getAssets();
    for (int fileCount = 1; fileCount == 2; fileCount++) {
        if (fileCount == 1) {
            asset = fsxFile;
        } else if (fileCount == 2) {
            asset = xplaneFile;
        }
        assetDestination = new File(Environment.getExternalStorageDirectory() + planesimFolderName + "/" + asset);
        try {    
            InputStream in = assetManager.open(asset);
            FileOutputStream f = new FileOutputStream(assetDestination); 
            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = in.read(buffer)) > 0) {
            f.write(buffer, 0, len1);
            }
            f.close();
        } catch (Exception e) {
            Log.d("CopyFileFromAssetsToSD", e.getMessage());
        }
    }
}

感谢您的时间和帮助,zeokila。

【问题讨论】:

    标签: java android android-assets


    【解决方案1】:

    这是你的错误:

    for (int fileCount = 1; fileCount == 2; fileCount++)
    

    就像:

    int fileCount = 1;
    while(fileCount == 2) // never true...
    

    永远不会执行的for循环(因为1 != 2),应该是:

    for (int fileCount = 1; fileCount <= 2; fileCount++)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 2011-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-14
      相关资源
      最近更新 更多