安卓用户修改图片功能

客户端选择图片:

                Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT); //"android.intent.action.GET_CONTENT"
                innerIntent.setType("image/*");
                startActivityForResult(innerIntent, REQUEST_CODE_SCAN_GALLERY);

选择完提交图片:

 Uri uriInfor;

    public void onActivityResult(final int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_CODE_SCAN_GALLERY:
                    uriInfor = data.getData();
                    String imagePath = getRealPathFromUri(getActivity(), data.getData());

                    String[] arrPath = imagePath.split("\\/");
                    userImage = arrPath[arrPath.length - 1];

                    FinalHttp finalHttp = new FinalHttp();
                    // 文件上传到服务器的位置

                    AjaxParams params = new AjaxParams();
                    // 获取要上传的本地资源
                    try {
                        params.put("userImage", new File(imagePath));
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    finalHttp.post(Consts.URL + Consts.APP.RegisterAction + "?action_flag=uploadImage", params, new AjaxCallBack<Object>() {
                        @Override
                        public void onStart() {
                            // mbtnAdd.setText("开始上传");
                            super.onStart();
                        }

                        @Override
                        public void onSuccess(Object o) {
                            // mbtnAdd.setText("上传成功");
//                            ToastUtil.show(getActivity(), "上传成功");
                            Log.i("pony_log", "图片上传成功");
                            // updateUser(false);
                            super.onSuccess(o);
                            updateImage(true);
                        }

                        @Override
                        public void onFailure(Throwable t, int errorNo, String strMsg) {
                            // mbtnAdd.setText("上传失败");
                            super.onFailure(t, errorNo, strMsg);
                        }
                    });


                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

客户端更新用户图片数据:


    private void updateImage(boolean isShow) {

        AjaxParams params = new AjaxParams();
        params.put("action_flag", "updateImage");
        params.put("uImg", userImage + "");
        params.put("userId", MemberUserUtils.getUid(getActivity()) + "");
        httpPost(Consts.URL + Consts.APP.RegisterAction, params, Consts.actionId.resultCode, isShow, "正在注册...");
    }


    @Override
    protected void callBackSuccess(ResponseEntry entry, int actionId) {
        super.callBackSuccess(entry, actionId);
        switch (actionId) {
            case Consts.actionId.resultCode:
                musicImage.setImageURI(uriInfor);
                ToastUtil.show(getActivity(), "修改成功");
                break;

        }
    }

服务端接收图片数据:

private void uploadImage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String imagePath = null;
		try {
			List<FileItem> items = this.upload.parseRequest(request);
			if (items != null && !items.isEmpty()) {
				for (FileItem fileItem : items) {
					String filename = fileItem.getName();

					File real_path = new File(Consts.imgPath + "/" + filename);
					InputStream inputSteam = fileItem.getInputStream();
					BufferedInputStream fis = new BufferedInputStream(inputSteam);
					FileOutputStream fos = new FileOutputStream(real_path);
					int f;
					while ((f = fis.read()) != -1) {
						fos.write(f);
					}
					fos.flush();
					fos.close();
					fis.close();
					inputSteam.close();
					System.out.println("文件:" + filename + "上传成功!");
					imagePath = filename;
				}
			}

		} catch (FileUploadException e) {
			e.printStackTrace();
		}
	}

服务端用户修改图片数据:

private void updateImage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path = request.getContextPath();
		String uImg = request.getParameter("uImg");
		String uid = request.getParameter("userId");
		List<Object> params = new ArrayList<Object>();
		params.add(uImg);
		params.add(uid);
		boolean flag = registerDao.updateImage(params);
		if (flag) {
			
			JSONObject jsonmsg = new JSONObject();
			jsonmsg.put("repMsg", "修改成功");
			jsonmsg.put("repCode", "666");
			System.out.println(jsonmsg);
			response.getWriter().print(jsonmsg);
			
		} else {
			JSONObject jsonmsg = new JSONObject();
			jsonmsg.put("repMsg", "修改失败");
			jsonmsg.put("repCode", "111");
			System.out.println(jsonmsg);
			response.getWriter().print(jsonmsg);
		}

	}
	

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Android毕业设计源码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值