现在多数的网络应用都有个人中心的模块,里面少不了用户上传图像功能,正好最近项目里用到了这个就从网上找了一个集成进来,写得挺好的代码给大家推广一下。
废话不多说了,代码已经上传到网盘,有需要的朋友可以去下载来看看,这里处理一下上传的问题。
使用AsyncTask异步上传
private class RegHeadAsyncTask extends AsyncTask<String, Void, String> { protected void onPreExecute() { } protected BaseResponse doInBackground(String... params) { List<NameValuePair> postParams = new ArrayList<NameValuePair>(); postParams.add(new BasicNameValuePair("uid", uid); postParams.add(new BasicNameValuePair("image", image)); String response = null; try { String result = HttpClientUtil.uploadImg(HttpConfig.url_getHead(), postParams); if (TextUtils.isEmpty(result)) return null; Gson gson = new Gson(); // 解析上传图像操作,服务器返回的信息 ... } catch (Exception e) { e.printStackTrace(); } return response; } protected void onPostExecute(String result) { if (null != result) { // 服务器返回的信息 ... if (flag) { // 上传成功 ImageLoader.getInstance().displayImage(imageUrl, imageView); // 上传成功更新ui } } } }
使用HTTP的POST方式提交(HttpClientUtil类)
public static String uploadImg(String url, List<NameValuePair> nameValuePairs) { BasicHttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, 10 * 1000); HttpConnectionParams.setSoTimeout(httpParams, 10 * 1000); HttpClient httpClient = new DefaultHttpClient(httpParams); HttpPost httpPost = new HttpPost(url); String result = ""; try { MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); for (int index = 0; index < nameValuePairs.size(); index++) { if (nameValuePairs.get(index).getName().equalsIgnoreCase("image")) { entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File(nameValuePairs.get(index).getValue()), "image/*")); } else { entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue())); } } httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost); if (response.getStatusLine().getStatusCode() == 200) { result = EntityUtils.toString(response.getEntity()); return result; } } catch (IOException e) { e.printStackTrace(); } return null; }
图片上传PHP服务端(ThinkPhp)
// 图像上传 public function upimg() { $uptypes = array( ‘image/jpg‘, ‘image/jpeg‘, ‘image/png‘, ‘imagepeg‘, ‘image/gif‘, ‘image/bmp‘, ‘image/x-png‘ ); $user_id=$_POST[‘uid‘]; $file = $_FILES["image"]; $fname = $_FILES["image"]["name"]; $fname_array = explode(‘.‘,$fname); $extend = $fname_array[count($fname_array)-1]; $MAX_FILE_SIZE = 1024000; $dest_folder = "./Uploads/avatar/";; if($extend!=""){ if(!in_array($file["type"],$uptypes)){ jsonerror("只能上传图片文件"); } if($file["size"]>$MAX_FILE_SIZE){ jsonerror("头像大小不能超过1M"); } $uploadfile = $dest_folder.$user_id.‘.‘.jpg; if(move_uploaded_file($_FILES["image"]["tmp_name"],$uploadfile)){ $user_info=M(‘Users‘); $data[‘HeadImg‘]=$uploadfile; $result=$user_info->where("Uid=$user_id")->save($data); if($result === FALSE){ jsonerror("头像修改失败"); }else{ jsonsuccess("头像修改成功"); } }else{ jsonerror("图片上传失败"); } } // 处理头像上传数据 public function upimgs() { // 判断是否上传会员卡 if(!empty($_FILES[‘image‘][‘name‘])){ $target_path = "./Uploads/avatar/";//接收文件目录 $filename = $_FILES[‘image‘][‘name‘]; // 文件名称 $exename = $this->getExeName($filename); // 获取扩展名 $filename = $_POST[‘app_uid‘].‘.‘.jpg; // 将文件重命名 $target_path = $target_path.$filename; if(move_uploaded_file($_FILES[‘image‘][‘tmp_name‘],$target_path)){ $user_id=$_POST[‘app_uid‘]; $user_info=M(‘Users‘); $data[‘HeadImg‘]=$filename; $result=$user_info->where("Uid=$user_id")->save($data); if($result === FALSE){ jsonerror("头像修改失败"); }else{ jsonsuccess("头像修改成功!"); } }else{ jsonerror("上传图片有错误,请再试一次"); } } } /** * 获取文件扩展名 */ public function getExeName($file) { $arrName = explode(‘.‘,$file); return end($arrName); }
附上拍照剪裁demo http://pan.baidu.com/s/1hqIfdS4
右侧带字母的联系人 http://pan.baidu.com/s/1tUKmM
时间: 2024-10-05 22:40:37