首页 > 其他 > 详细

调用系统相机

时间:2015-11-28 10:22:05      阅读:177      评论:0      收藏:0      [点我收藏+]
***加权限 

1
package com.xj.finance_connect; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.InputStreamReader; 8 import java.io.UnsupportedEncodingException; 9 import java.util.ArrayList; 10 import java.util.List; 11 12 import org.apache.http.HttpResponse; 13 import org.apache.http.HttpStatus; 14 import org.apache.http.NameValuePair; 15 import org.apache.http.client.ClientProtocolException; 16 import org.apache.http.client.HttpClient; 17 import org.apache.http.client.entity.UrlEncodedFormEntity; 18 import org.apache.http.client.methods.HttpPost; 19 import org.apache.http.impl.client.DefaultHttpClient; 20 import org.apache.http.message.BasicNameValuePair; 21 22 import android.app.Activity; 23 import android.content.Intent; 24 import android.graphics.Bitmap; 25 import android.graphics.drawable.BitmapDrawable; 26 import android.net.Uri; 27 import android.os.Bundle; 28 import android.os.Environment; 29 import android.os.Handler; 30 import android.os.Message; 31 import android.provider.MediaStore; 32 import android.util.Log; 33 import android.view.LayoutInflater; 34 import android.view.View; 35 import android.view.View.OnClickListener; 36 import android.view.ViewGroup.LayoutParams; 37 import android.widget.ImageView; 38 import android.widget.PopupWindow; 39 import android.widget.RelativeLayout; 40 import android.widget.TextView; 41 import android.widget.Toast; 42 43 import com.google.gson.Gson; 44 import com.lidroid.xutils.ViewUtils; 45 import com.xj.model.Person_Info; 46 import com.xj.model.Personal_Geren; 47 48 public class PersonalActivity extends Activity implements OnClickListener { 49 50 private ImageView person_image; 51 private TextView person_name; 52 private TextView person_phone; 53 private RelativeLayout person_grade; 54 private RelativeLayout person_indent; 55 private RelativeLayout person_buy; 56 private RelativeLayout person_amend; 57 // 裁剪后图片的宽(X)和高(Y),480 X 480的正方形。 58 private static int output_X = 480; 59 private static int output_Y = 480; 60 61 /* 请求识别码 */ 62 private static final int CODE_GALLERY_REQUEST = 0xa0; 63 private static final int CODE_CAMERA_REQUEST = 0xa1; 64 private static final int CODE_RESULT_REQUEST = 0xa2; 65 66 /* 头像文件 */ 67 private static final String IMAGE_FILE_NAME = "temp_head_image.jpg"; 68 69 Handler handler=new Handler(){ 70 71 @Override 72 public void handleMessage(Message msg) { 73 // TODO Auto-generated method stub 74 if(msg.what==1){ 75 Personal_Geren geren=(Personal_Geren) msg.obj; 76 Person_Info info=geren.getInfo(); 77 person_phone.setText("电话|"+info.getUser_name()); 78 } 79 } 80 81 }; 82 @Override 83 protected void onCreate(Bundle savedInstanceState) { 84 super.onCreate(savedInstanceState); 85 setContentView(R.layout.personal); 86 TextView left = (TextView) findViewById(R.id.left); 87 left.setText("返回"); 88 left.setOnClickListener(new OnClickListener() { 89 90 @Override 91 public void onClick(View v) { 92 // TODO Auto-generated method stub 93 finish(); 94 } 95 }); 96 TextView center = (TextView) findViewById(R.id.center); 97 center.setText("个人中心"); 98 center.setTextSize(20f); 99 initview(); 100 getMessage(); 101 } 102 /** 103 * 网络请求得到个人信息 104 */ 105 private void getMessage() { 106 // TODO Auto-generated method stub 107 new Thread() { 108 109 @Override 110 public void run() { 111 // TODO Auto-generated method stub 112 HttpClient client = new DefaultHttpClient(); 113 HttpPost post = new HttpPost( 114 "http://www.myphp888.com/index/index.php?r=member/usermessage"); 115 List<NameValuePair> list = new ArrayList<NameValuePair>(); 116 // list.add(new BasicNameValuePair("r", "member/usermessage")); 117 list.add(new BasicNameValuePair("id",MainActivity.user_id)); 118 119 try { 120 // 得到UrlEncodedFormEntity传递参数封装实体对象 121 UrlEncodedFormEntity entity = new UrlEncodedFormEntity( 122 list, "UTF-8"); 123 post.setHeader("Content-Type", 124 "application/x-www-form-urlencoded; charset=utf-8"); 125 126 // 把实体对象存入到httpPost对象中 127 post.setEntity(entity); 128 // 调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例 129 HttpResponse response = client.execute(post); 130 // 判断是否链接成功 131 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 132 133 InputStream stream = response.getEntity().getContent(); 134 BufferedReader reader = new BufferedReader( 135 new InputStreamReader(stream)); 136 StringBuffer buffer = new StringBuffer(); 137 String str = ""; 138 while ((str = reader.readLine()) != null) { 139 buffer.append(str); 140 } 141 Log.i("TAG", buffer.toString()); 142 Gson gson = new Gson(); 143 Personal_Geren json = gson.fromJson(buffer.toString(), 144 Personal_Geren.class); 145 146 Message message = handler.obtainMessage(1, json); 147 handler.sendMessage(message); 148 stream.close(); 149 // 关闭链接 150 client.getConnectionManager().shutdown(); 151 } 152 153 } catch (UnsupportedEncodingException e) { 154 // TODO Auto-generated catch block 155 e.printStackTrace(); 156 } catch (ClientProtocolException e) { 157 // TODO Auto-generated catch block 158 e.printStackTrace(); 159 } catch (IOException e) { 160 // TODO Auto-generated catch block 161 e.printStackTrace(); 162 } 163 164 }; 165 166 }.start(); 167 } 168 169 /** 170 * 初始化控件 171 */ 172 private void initview() { 173 person_image = (ImageView) findViewById(R.id.person_image);// 头像 174 person_name = (TextView) findViewById(R.id.person_name);// 名字 175 person_phone = (TextView) findViewById(R.id.person_phone);// 电话 176 person_grade = (RelativeLayout) findViewById(R.id.person_grade);// 会员等级 177 person_indent = (RelativeLayout) findViewById(R.id.person_indent);// 订单 178 person_buy = (RelativeLayout) findViewById(R.id.person_buy);// 申购的产品 179 person_amend = (RelativeLayout) findViewById(R.id.person_amend);// 修改密码 180 181 person_image.setOnClickListener(this); 182 person_grade.setOnClickListener(this); 183 person_indent.setOnClickListener(this); 184 person_buy.setOnClickListener(this); 185 person_amend.setOnClickListener(this); 186 } 187 188 /** 189 * 监听 190 */ 191 @Override 192 public void onClick(View v) { 193 switch (v.getId()) { 194 case R.id.person_image://上传头像 195 getPhoto(); 196 break; 197 case R.id.person_grade://会员等级 198 199 break; 200 case R.id.person_indent:// 订单 201 Intent intent=new Intent(PersonalActivity.this,DingDanActivity.class); 202 startActivity(intent); 203 204 break; 205 case R.id.person_buy://申购的产品 206 Intent intent_product=new Intent(PersonalActivity.this,MyProductActivity.class); 207 startActivity(intent_product); 208 break; 209 case R.id.person_amend:// 修改密码 210 211 break; 212 213 default: 214 break; 215 } 216 217 } 218 /** 219 * 上传照片 220 */ 221 private void getPhoto() { 222 // TODO Auto-generated method stub 223 View popView = LayoutInflater.from(getApplicationContext()) 224 .inflate(R.layout.pop_person, null); 225 ViewUtils.inject(PersonalActivity.this,popView); 226 final PopupWindow pop = new PopupWindow(popView, LayoutParams.WRAP_CONTENT, 227 LayoutParams.WRAP_CONTENT, true); 228 pop.setBackgroundDrawable(new BitmapDrawable()); 229 pop.showAsDropDown(person_buy, 200, -180); 230 TextView pop_cear = (TextView) popView.findViewById(R.id.pop_cearem); 231 TextView pop_cancle = (TextView) popView.findViewById(R.id.pop_cancle); 232 TextView pop_xiangce = (TextView) popView.findViewById(R.id.pop_xiangce); 233 pop_cear.setOnClickListener(PersonalActivity.this); 234 pop_cancle.setOnClickListener(PersonalActivity.this); 235 236 pop_xiangce.setOnClickListener(new OnClickListener() { 237 238 @Override 239 public void onClick(View v) { 240 // TODO Auto-generated method stub 241 // 从本地相册选取图片作为头像 242 Intent intentFromGallery = new Intent(); 243 // 设置文件类型 244 intentFromGallery.setType("image/*"); 245 intentFromGallery.setAction(Intent.ACTION_GET_CONTENT); 246 startActivityForResult(intentFromGallery, CODE_GALLERY_REQUEST); 247 } 248 }); 249 250 pop_cear.setOnClickListener(new OnClickListener() { 251 252 @Override 253 public void onClick(View v) { 254 // TODO Auto-generated method stub 255 Intent intentFromCapture = new Intent( 256 MediaStore.ACTION_IMAGE_CAPTURE); 257 258 // 判断存储卡是否可用,存储照片文件 259 if (hasSdcard()) { 260 intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT, Uri 261 .fromFile(new File(Environment 262 .getExternalStorageDirectory(), 263 IMAGE_FILE_NAME))); 264 } 265 266 startActivityForResult(intentFromCapture, CODE_CAMERA_REQUEST); 267 } 268 }); 269 270 pop_cancle.setOnClickListener(new OnClickListener() { 271 272 @Override 273 public void onClick(View v) { 274 // TODO Auto-generated method stub 275 pop.dismiss(); 276 } 277 }); 278 279 280 } 281 /** 282 * 检查设备是否存在SDCard的工具方法 283 */ 284 public static boolean hasSdcard() { 285 String state = Environment.getExternalStorageState(); 286 if (state.equals(Environment.MEDIA_MOUNTED)) { 287 // 有存储的SDCard 288 return true; 289 } else { 290 return false; 291 } 292 } 293 /** 294 * 裁剪原始的图片 295 */ 296 public void cropRawPhoto(Uri uri) { 297 298 Intent intent = new Intent("com.android.camera.action.CROP"); 299 intent.setDataAndType(uri, "image/*"); 300 301 // 设置裁剪 302 intent.putExtra("crop", "true"); 303 304 // aspectX , aspectY :宽高的比例 305 intent.putExtra("aspectX", 1); 306 intent.putExtra("aspectY", 1); 307 308 // outputX , outputY : 裁剪图片宽高 309 intent.putExtra("outputX", output_X); 310 intent.putExtra("outputY", output_Y); 311 intent.putExtra("return-data", true); 312 313 startActivityForResult(intent, CODE_RESULT_REQUEST); 314 } 315 316 /** 317 * 提取保存裁剪之后的图片数据,并设置头像部分的View 318 */ 319 private void setImageToHeadView(Intent intent) { 320 Bundle extras = intent.getExtras(); 321 if (extras != null) { 322 Bitmap photo = extras.getParcelable("data"); 323 person_image.setImageBitmap(photo); 324 325 } 326 } 327 @Override 328 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 329 // TODO Auto-generated method stub 330 super.onActivityResult(requestCode, resultCode, data); 331 if (requestCode == CODE_GALLERY_REQUEST) { 332 cropRawPhoto(data.getData()); 333 334 } else if (requestCode == CODE_CAMERA_REQUEST) { 335 if (hasSdcard()) { 336 File tempFile = new File( 337 Environment.getExternalStorageDirectory(), 338 IMAGE_FILE_NAME); 339 cropRawPhoto(Uri.fromFile(tempFile)); 340 } else { 341 Toast.makeText(getApplication(), "没有SDCard!", Toast.LENGTH_LONG) 342 .show(); 343 } 344 } else if (requestCode == CODE_RESULT_REQUEST) { 345 if (data != null) { 346 setImageToHeadView(data); 347 } 348 } 349 } 350 }

 

调用系统相机

原文:http://www.cnblogs.com/zxwisfdlkjglkf/p/5002248.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!