I have sent the data from the android application to the webservice which further will transfer it to the browser by using KSOAP. now i am planning to transfer a file from the app to the web service. Is it possible?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
InputStream is = null;
try{
is = new BufferedInputStream(new FileInputStream(Environment.getExternalStorageDirectory().getAbsolutePath() +"/Filename"));
}
catch (FileNotFoundException e1) {
e1.printStackTrace();
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
while (is.available() > 0) {
bos.write(is.read());
}
}
catch (IOException e1) {
e1.printStackTrace();
}
byte[] byteArray = bos.toByteArray();
String base64= Base64.encodeToString(byteArray, Base64.DEFAULT);
1st step: Get the file from the SDcard and assign that file in INPUTSTREAM.
2nd step: Write the file into BYTEARRAYOUTPUTSTREAM
3rd step: Convert that Stream into BYTEARRAY
4th step: Convert Bytearray into BASE64STRING