簡體   English   中英

我無法將文件從資產復制到外部存儲

[英]I can't manage to copy files from assets to external storage

我正在制作一個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。

這是你的錯誤:

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++)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM