Android调用安卓相机拍照上传
2015-09-19 01:10
295 查看
popupWindow_photo = new PopupWindow_photo(getActivity().getApplicationContext(), base_title); /* bitmap截图信息 */ // 修改头像 /* * * public class headrel implements View.OnClickListener { * * @Override public void onClick(View v) { popupWindow_photo = new * PopupWindow_photo(getActivity(), base_title); } } */ // 选择头像弹窗 private static final int PHOTO_REQUEST_CAMERA = 1;// 拍照 private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择 private static final int PHOTO_REQUEST_CUT = 3;// 结果 public class PopupWindow_photo extends PopupWindow { private Button select_camera; private Button select_photo; private Button select_out; private LinearLayout select_input; public PopupWindow_photo(Context mContext, View parent) { super(parent); View view = View .inflate(mContext, R.layout.popup_type_select, null);// 自己写的xml弹框布局 setWidth(ViewGroup.LayoutParams.MATCH_PARENT);// 设置宽度 setHeight(ViewGroup.LayoutParams.MATCH_PARENT);// 设置高度 setFocusable(true); setOutsideTouchable(true);// 设置是否可以外部点击 setContentView(view);// 在父view中呈现 showAtLocation(parent, Gravity.TOP, 0, 0);// 在父view的x,y位置出现 update();// 刷新 // 自己xml定义的组件用法 select_input = (LinearLayout) view.findViewById(R.id.select_input); select_camera = (Button) view.findViewById(R.id.select_camera); select_photo = (Button) view.findViewById(R.id.select_photo); select_out = (Button) view.findViewById(R.id.select_out); StaticData.buttonnowscale(select_camera, 90, 610); StaticData.buttonnowscale(select_photo, 90, 610); StaticData.buttonnowscale(select_out, 90, 610); StaticData.linearlayoutnowscale(select_input, 275, 610); select_camera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent( "android.media.action.IMAGE_CAPTURE"); // 判断存储卡是否可以用,可用进行存储 if (hasSdcard()) { intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri .fromFile(new File(Environment .getExternalStorageDirectory(), "IMGface"))); } startActivityForResult(intent, PHOTO_REQUEST_CAMERA); dismiss(); } }); select_photo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("image/*"); startActivityForResult(intent, PHOTO_REQUEST_GALLERY); dismiss(); } }); select_out.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dismiss(); } }); } } @SuppressLint("ShowToast") @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case PHOTO_REQUEST_GALLERY: if (data != null) { // 得到图片的全路径 Uri uri = data.getData(); crop(uri); } break; case PHOTO_REQUEST_CAMERA: if (hasSdcard()) { tempFile = new File(Environment.getExternalStorageDirectory(), "IMGface"); crop(Uri.fromFile(tempFile)); } else { Toast.makeText(getActivity().getApplicationContext(), "未找到存储卡,无法存储照片!", 0).show(); } break; case PHOTO_REQUEST_CUT: bitmap = data.getParcelableExtra("data"); imgs.setImageBitmap(bitmap); try { if (bitmap != null) { // boolean delete = tempFile.delete(); // System.out.println("delete = " + delete); f = new File(getActivity().getApplicationContext() .getFilesDir(), "IMGface.jpg"); FileOutputStream out = null; out = new FileOutputStream(f); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); } } catch (Exception e) { e.printStackTrace(); } sendData(); break; } } /** * 剪切图片 * * @param uri * @function: * @author:Jerry * @date:2013-12-30 */ private void crop(Uri uri) { // 裁剪图片意图 Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // 裁剪框的比例,1:1 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // 裁剪后输出图片的尺寸大小 intent.putExtra("outputX", 250); intent.putExtra("outputY", 250); // 图片格式 intent.putExtra("outputFormat", "JPG"); intent.putExtra("noFaceDetection", true);// 取消人脸识别 intent.putExtra("return-data", true);// true:不返回uri,false:返回uri startActivityForResult(intent, PHOTO_REQUEST_CUT); } private boolean hasSdcard() { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { return true; } else { return false; } } // 上传 public void sendData() { String url = UrlVO.Host_Url + UrlVO.Change_Url; try { RequestParams params = new RequestParams(); params.put("file", f); AsyncHttpClient client = new AsyncHttpClient(); if (!UrlVO.getShareData("JSESSIONID", getActivity().getApplicationContext()).equals("false")) { client.addHeader("Cookie", UrlVO.getShareData("JSESSIONID", getActivity().getApplicationContext())); } client.post(url, params, new AsyncHttpResponseHandler() { @Override public void onSuccess(String response) { Bundle bundle = new Bundle(); bundle.putString("response", response); if (response == null || response.equals("")) { return; } String[] key = { "success" }; String[] logindata = json.getJSON(response, key); if (logindata == null) { return; } if (logindata[0].equals("true")) { Toast.makeText(getActivity().getApplicationContext(), "修改成功", 3000).show(); // getActivity().finish(); getData(UrlVO.Personal_Url, UrlVO.JSESSIONID);// 获取个人 } } @Override public void onFailure(int statusCode, org.apache.http.Header[] headers, byte[] responseBody, Throwable error) { // TODO 自动生成的方法存根 super.onFailure(statusCode, headers, responseBody, error); Toast.makeText(getActivity().getApplicationContext(), "网络访问异常 ", 0).show(); } // @Override // public void onFailure(int statusCode, Header[] headers, // byte[] responseBody, Throwable error) { // super.onFailure(statusCode, headers, responseBody, error); // Toast.makeText(getActivity().getApplicationContext(), // "网络访问异常 ", 0).show(); // } }); } catch (Exception e) { e.printStackTrace(); } }
相关文章推荐
- Android点击事件方法
- Android一个ListView列表之中插入两种不同的数据
- Android下拉刷新,上拉加载
- Android水平列表循环插入数据
- Android下拉列表使用方法
- Android ImageLoader组件加载图片
- Android完整弹窗界面
- Android单击界面上面的其他位置,取消焦点,让键盘消失
- android studio 常用插件
- Android取消文本框聚焦事件
- Android刷新页面
- Android下滑条ScrollView
- Android自定义控件按钮
- android 找工作
- Android跳转页面的基本方式
- Android完整的Activity页面
- Android Studio 修改注释模板中的${USER}变量
- Android完整的Fragment页面
- databinding学习(2)
- 我的Android设计模式(一) 层级控制模式