这是我的活动代码;
public class FromAssetToSDCardActivity extends Activity {
private final static int BUFFER_SIZE = 1024;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_from_asset_to_sdcard);
try {
AssetManager assetFiles = getAssets();
// visualModels is the name of folder from inside our assets folder
String[] files = assetFiles.list("visualModels");
// Initialize streams
InputStream in = null;
OutputStream out = null;
for (int i = 0; i < files.length; i++) {
if (files[i].toString().equalsIgnoreCase("images")
|| files[i].toString().equalsIgnoreCase("js")) {
/*
* @Do nothing. images and js are folders but they will be
* interpreted as files.
*
* @This is to prevent the app from throwing file not found
* exception.
*/
} else {
/*
* @Folder name is also case sensitive
*
* @MyHtmlFiles is the folder from our assets
*/
in = assetFiles.open("visualModels/" + files[i]);
/*
* Currently we will copy the files to the root directory
* but you should create specific directory for your app
*/
out = new FileOutputStream(
Environment.getExternalStorageDirectory() + "/"
+ files[i]);
copyAssetFiles(in, out);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void copyAssetFiles(InputStream in, OutputStream out) {
try {
byte[] buffer = new byte[BUFFER_SIZE];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
我知道这种不足是来自sdCard还是来自内存?
还是应该增加Buffer_Size
?