博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
调用系统相机
阅读量:5736 次
发布时间:2019-06-18

本文共 13402 字,大约阅读时间需要 44 分钟。

***加权限  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 stub107         new Thread() {108 109             @Override110             public void run() {111                 // TODO Auto-generated method stub112                 HttpClient client = new DefaultHttpClient();113                 HttpPost post = new HttpPost(114                         "http://www.myphp888.com/index/index.php?r=member/usermessage");115                 List
list = new ArrayList
();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 block155 e.printStackTrace();156 } catch (ClientProtocolException e) {157 // TODO Auto-generated catch block158 e.printStackTrace();159 } catch (IOException e) {160 // TODO Auto-generated catch block161 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 @Override192 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 stub223 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 @Override239 public void onClick(View v) {240 // TODO Auto-generated method stub241 // 从本地相册选取图片作为头像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 @Override253 public void onClick(View v) {254 // TODO Auto-generated method stub255 Intent intentFromCapture = new Intent(256 MediaStore.ACTION_IMAGE_CAPTURE);257 258 // 判断存储卡是否可用,存储照片文件259 if (hasSdcard()) {260 intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT, Uri261 .fromFile(new File(Environment262 .getExternalStorageDirectory(),263 IMAGE_FILE_NAME)));264 }265 266 startActivityForResult(intentFromCapture, CODE_CAMERA_REQUEST);267 }268 });269 270 pop_cancle.setOnClickListener(new OnClickListener() {271 272 @Override273 public void onClick(View v) {274 // TODO Auto-generated method stub275 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 // 有存储的SDCard288 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 * 提取保存裁剪之后的图片数据,并设置头像部分的View318 */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 @Override328 protected void onActivityResult(int requestCode, int resultCode, Intent data) {329 // TODO Auto-generated method stub330 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 }

 

转载于:https://www.cnblogs.com/zxwisfdlkjglkf/p/5002248.html

你可能感兴趣的文章
我的友情链接
查看>>
DNS显性+隐性URL转发原理
查看>>
我的友情链接
查看>>
网易有道 IP地址、手机号码归属地和身份证 查询接口API
查看>>
鼠标停留在GridView某一行时行的颜色改变
查看>>
系列3:WAS Liberty Profile hello mysql jdbc
查看>>
基础知识:python模块的导入
查看>>
Android MVC之我的实现
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
关于批处理-1
查看>>
Tomcat部署Web应用方法总结
查看>>
Python3 django2.0 字段加密 解密 AES
查看>>
CCNA实验之:网络地址转换(NAT)实验
查看>>
计算机网络原理笔记-停止等待协议
查看>>
确定当前记录和下一条记录之间相差的天数
查看>>
sql语句返回主键SCOPE_IDENTITY()
查看>>
机器学习开源项目精选TOP30
查看>>
代码分析系列 内存执行过程
查看>>
iOS开发-邮件发送
查看>>