标签:
1. 相册选择图片
private void setalbumImg(int type2) { // TODO Auto-generated method stub pw.dismiss(); // 从相册获取图片 Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType(IMAGE_TYPE); startActivityForResult(intent, type2); // 把资源图片转换成bitmap // bm= BitmapFactory.decodeResource(MainActivity.this.getResources(), // R.drawable.ic_launcher); // img.setImageBitmap(bm); }
2.拍照
private void setcameraImg(int type2) { // TODO Auto-generated method stub pw.dismiss(); Intent intent = new Intent(); intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, type2+1); }
3 onActivityResult: 其中img_shipper表示的是ImgView要显示的那个照片
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { // 外界的程序访问ContentProvider所提供数据 可以通过ContentResolver接口 ContentResolver resolver = getContentResolver(); // 此处的用于判断接收的Activity是不是你想要的那个 if (requestCode == type) { try { Uri originalUri = data.getData(); // 获得图片的uri bm = MediaStore.Images.Media.getBitmap(resolver, originalUri); // 图片等比例缩放 Bitmap bitmap = CommonUtils.scaleImg(bm, CommonUtils.getViewWIth(img_shipper)[0], CommonUtils.getViewWIth(img_shipper)[1]); // scaleImg(); // 显得到bitmap图片 // cutImg(originalUri); iconpath = "touxiang.png"; // saveImg(); img_shipper.setImageBitmap(bitmap); } catch (IOException e) { Log.e("TAG-->Error", e.toString()); } } if (requestCode ==type+1) { Bundle bundle = data.getExtras(); bm = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式 // 图片等比例缩放 Bitmap bitmap= CommonUtils.scaleImg(bm, CommonUtils.getViewWIth(img_shipper)[0], CommonUtils.getViewWIth(img_shipper)[1]); // scaleImg(); // 图片剪切 iconpath = "touxiang.png"; // Uri uri = Uri.fromFile(fi); // cutImg(uri); // 显得到bitmap图片 img_shipper.setImageBitmap(bitmap); } if (requestCode == 200) { // 拿到剪切数据 bm = data.getParcelableExtra("data"); // 显示剪切的图像 // img.setImageBitmap(bm); } } }
4.
/** * 得到view宽高 * * @return 0,宽;1,高 */ public static int[] getViewWIth(final View view) { int[] dpi = new int[2]; int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); view.measure(w, h); dpi[1] = view.getMeasuredHeight(); dpi[0] = view.getMeasuredWidth(); return dpi; }
private void saveImg(Bitmap bitmap,String path, Runnable r) { // TODO Auto-generated method stub saveMyBitmap(path, bitmap,r);// 将图像保存到SD卡中 } public void saveMyBitmap(String bitName, Bitmap mBitmap, Runnable r) { // 先创建要保存图片的文件夹,然后在创建图片 ff = new File(Environment.getExternalStorageDirectory() .getAbsoluteFile() + File.separator + "saveBitmap"); if (!ff.exists()) { ff.mkdirs(); } fi = new File(ff, bitName); // FileOutputStream fOut = null; try { fOut = new FileOutputStream(fi); } catch (Exception e) { e.printStackTrace(); } //图片压缩 Bitmap bitmap=comp(mBitmap); // 把bitmap保存到file中 bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut); //图片上传 loadBitmap(bitName); try { fOut.flush(); } catch (IOException e) { e.printStackTrace(); } try { fOut.close(); } catch (IOException e) { e.printStackTrace(); } }
5.上传方法
/** * 上传图片 * * */ public void loadBitmap(String bitName){ ff = new File(Environment.getExternalStorageDirectory() .getAbsoluteFile() + File.separator + "saveBitmap"); if (!ff.exists()) { ff.mkdirs(); } fi = new File(ff, bitName); Log.d("file", fi.getAbsolutePath()); try { //图片上传方法 HttpFile u = new HttpFile(Url); u.addFileParameter("file", fi); u.addTextParameter("userId", "userId")); byte[] b = u.send(); String result = new String(b); System.out.println(result); Gson gson=new Gson(); CarUrl carUrl=gson.fromJson(result, CarUrl.class); Log.d("carurl", carUrl.url); Message msg=mHandler.obtainMessage(); msg.what=100; msg.obj=r; mHandler.sendMessage(msg); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
标签:
原文地址:http://www.cnblogs.com/heke-heke/p/5364325.html