Skip to content

Instantly share code, notes, and snippets.

@cuiyiming007
Created June 20, 2019 01:41
Show Gist options
  • Save cuiyiming007/cc73eb624651583586a5c9bb28e6c333 to your computer and use it in GitHub Desktop.
Save cuiyiming007/cc73eb624651583586a5c9bb28e6c333 to your computer and use it in GitHub Desktop.
private void copyBigDataToSD(String strOutFileName) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
//申请WRITE_EXTERNAL_STORAGE权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},
1);
}
InputStream myInput = null;
OutputStream myOutput = null;
File myDir = new File(strOutFileName);
myDir.mkdirs();
File file = new File(myDir, "out.json");
if (file.exists()) {
file.delete();
}
try {
file.createNewFile();
myOutput = new FileOutputStream(file);
myInput = this.getAssets().open("plants.json");
byte[] buffer = new byte[1024];
int length = myInput.read(buffer);
while (length > 0) {
myOutput.write(buffer, 0, length);
length = myInput.read(buffer);
}
myOutput.flush();
myInput.close();
myOutput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment