編輯:關於 Android 程式設計
專案裡遇到一個需求:拍照之後把圖片上傳到伺服器,而後端是用 C# 寫的 WebService。一般呼叫 WebService 傳的是 XML 或 JSON 字串,沒傳過檔案這種東西。試了幾種做法後,最後採用的是:在 Android 端用 I/O 流讀進圖片,以 Base64 編碼成字串,透過 WebService 當參數送到伺服器端,伺服器解碼後存檔。
整個流程的關鍵就是「以 Base64 字串進行資料傳遞」。下載過程與上傳相反,把兩端的程式對調即可。
public void testUpload() {
try {
String srcUrl = "/sdcard/"; // 路徑
String fileName = "aa.jpg"; // 檔名
FileInputStream fis = new FileInputStream(srcUrl + fileName);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int count = 0;
while ((count = fis.read(buffer)) >= 0) {
baos.write(buffer, 0, count);
}
String uploadBuffer = new String(Base64.encode(baos.toByteArray())); // 進行 Base64 編碼
String methodName = "uploadImage";
connectWebService(methodName, fileName, uploadBuffer); // 呼叫 webservice
Log.i("connectWebService", "start");
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
這只是個 demo,沒有 UI 介面,檔案路徑與檔名都寫死,實際運行時改一下即可。
// 使用 ksoap2 呼叫 webservice
private boolean connectWebService(String methodName, String fileName, String imageBuffer) {
// 命名空間,即服務器端的介面,注意:後綴沒加 .wsdl
String namespace = "https://134.192.44.105:8080/SSH2/service/IService";
// 對應的 url,服務器端是用 x-fire 實作 webservice 介面
String url = "https://134.192.44.105:8080/SSH2/service/IService";
SoapObject soapObject = new SoapObject(namespace, methodName);
soapObject.addProperty("filename", fileName); // 參數 1:圖片名
soapObject.addProperty("image", imageBuffer); // 參數 2:圖片字串
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
envelope.dotNet = false;
envelope.setOutputSoapObject(soapObject);
HttpTransportSE httpTranstation = new HttpTransportSE(url);
try {
httpTranstation.call(namespace, envelope);
Object result = envelope.getResponse();
Log.i("connectWebService", result.toString());
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
不懂的部分請參考 ksoap2 的官方文件,這裡就不贅述 SOAP 封包細節。
public String uploadImage(String filename, String image) {
FileOutputStream fos = null;
try {
String toDir = "C:\\Program Files\\Tomcat 6.0\\webapps\\SSH2\\images"; // 儲存路徑
byte[] buffer = new BASE64Decoder().decodeBuffer(image); // 對 android 傳過來的圖片字串解碼
File destDir = new File(toDir);
if (!destDir.exists()) destDir.mkdir();
fos = new FileOutputStream(new File(destDir, filename)); // 保存圖片
fos.write(buffer);
fos.flush();
fos.close();
return "上傳圖片成功! 圖片路徑為:" + toDir;
} catch (Exception e) {
e.printStackTrace();
}
return "上傳圖片失敗!";
}
對 Android 端的 testUpload() 做單元測試呼叫,看到綠條就代表呼叫成功,此時到伺服器該目錄下就能看到上傳的圖片。
這個 demo 很簡陋,沒有任何 UI,但足以說明 Android 端呼叫 WebService 上傳圖片的完整過程。若要用在正式專案,有幾點必須調整:
NetworkOnMainThreadException。java.util.Base64.getDecoder()。下載的做法與上傳相反:伺服器端把圖片讀成位元組並 Base64 編碼回傳,Android 端收到字串後解碼寫檔即可。歡迎交流學習。
Android 自定義 View 實現字母導航欄
字母導航欄的實作 字母導航欄常見於聯絡人、城市選擇或音樂應用。使用者滑動右側字母時,中間會顯示目前字母,左側列表也會快速定位到對應位置。 自訂 NavView 字母導航
Gson、FastJson、org.JSON 解析效能實測比較
測試背景 在看到有人分享 FlatBuffers 的效能優勢後,作者開始好奇 Android 開發中常用的三種 JSON 解析方式:Google 的 Gson、阿里巴巴的
仿 QQ 空間相冊橫向滑動展示多圖:MyHorizontalScrollView 實作
動手客製化吧 版面配置檔:activity_main.xml 版面配置檔:activity_index_gallery_item.xml
微信評論找不到?用消息列表快速回覆好友
微信評論太多找不到時,可從相簿進入。點我、相簿,右上角更多,選擇消息列表,即可查看朋友回覆與留言紀錄。