简体   繁体   中英

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

I am making an Android application, and I would like to add two files named fsx.xml and xplane.xml . This is the code I am using, it runs perfectly without errors, but the /planesim just appears empty. Please help!

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());
        }
    }
}

Thanks for your time and help, zeokila.

This is your mistake:

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

which is like:

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

The for loop never executed (because 1 != 2 ), should be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM