Android调用安卓相机拍照上传
16lz
2021-12-19
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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic void onClick(View v) {dismiss();}});}}@SuppressLint("ShowToast")@Overridepublic 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:1intent.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:返回uristartActivityForResult(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() {@Overridepublic 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);// 获取个人}}@Overridepublic 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 maxLines ellipsize 配合使用失效,解决办法
- android 对话框
- Android填坑记录
- android图片裁剪
- Android快速显示4G
- RecyclerView嵌套ScrollView
- android 实现保存图片到相册
- Android6.0修改设备设置中kernel信息