[英]Upload picture to server
我正在使用以下代碼: http : //androidexample.com/Upload_File_To_Server_-_Android_Example/index.php?view=article_discription&aid=83&aaid=106
有用!
我想知道我要發送的照片是幾秒鍾前用相機拍攝的照片。 所以..我已經創建了一個簡單的方法來拍照,然后此OnActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
//2
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
//3
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
//4
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "guasto.jpg");
try {
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
//5
fo.write(bytes.toByteArray());
fo.close();
uploadFile(uploadFilePath + "" + uploadFileName); //this should call the working method to upload the picture
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
問題是上傳無法正常工作,並且在執行以下代碼行之前就停止了:
dos = new DataOutputStream(conn.getOutputStream());
我該怎么做才能解決問題...? 非常感謝你
嘗試以下代碼。 創建一個異步任務,並在doInBackground方法中添加代碼。 您將獲得InputStream的對象作為回報。
您應該使用Multipart實體。
File file = new File(selectedImage);
ContentBody cbFile = new FileBody(file, "image/*");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart(RequestParams.POST, cbFile);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://yourwebserveraddress.com/apiname_or_whatever_path_it_is");
httpPost.setEntity(reqEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
其中selectedImage是圖像的路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.