我想启动相机并单击照片并将其存储在SD卡中,它应自动选择特定文件并提供上传选项,并在屏幕上显示图像。
点击上传后,它应该上传到服务器。我想知道如何通过Android应用程序处理PHP上传文件。
我是android新手。请指导我
答案 0 :(得分:2)
从this thread获取:
public void doUpload(String filepath,String filename) {
HttpClient httpClient = new DefaultHttpClient();
try {
httpClient.getParams().setParameter("http.socket.timeout", new Integer(90000)); // 90 second
post = new HttpPost(new URI(YOUR_SERVER_ADDRESS));
File file = new File(filepath);
FileEntity entity;
if (filepath.substring(filepath.length()-3, filepath.length ()).equalsIgnoreCase("txt") ||
filepath.substring(filepath.length()-3, filepath.length ()).equalsIgnoreCase("log")) {
entity = new FileEntity(file,"text/plain; charset=\"UTF-8\"");
entity.setChunked(true);
}else {
entity = new FileEntity(file,"binary/octet-stream");
entity.setChunked(true);
}
post.setEntity(entity);
post.addHeader(FILENAME_STR, filename);
HttpResponse response = httpClient.execute(post);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
Log.e(TAG,"--------Error--------Response Status line code:"+response.getStatusLine());
}else {
// Here every thing is fine.
}
HttpEntity resEntity = response.getEntity();
if (resEntity == null) {
Log.e(TAG,"---------Error No Response !!!-----");
}
} catch (Exception ex) {
Log.e(TAG,"---------Error-----"+ex.getMessage());
ex.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
}