图片文件上传以及图片文件流上传

来源:互联网 发布:市政工程算量软件 编辑:程序博客网 时间:2024/06/02 08:59
/**
     * file文件 上传图片至服务器
     * */
    public void uploadImage() {
        File temp = new File(ImageUtil.getPhotoAbsolutePath(),
                ImageUtil.getCropPhotoName());

        RequestParams params = new RequestParams();
        MultipartEntity entity = new MultipartEntity();
        try {
            entity.addPart("username",
                    new StringBody(Setting.getCurrentMerchantPhone()));
            entity.addPart("password",
                    new StringBody(Setting.getCurrentUserPwd()));
            entity.addPart("image", new FileBody(temp, "multipart/form-data"));
            params.setBodyEntity(entity);
            LogUtil.i(TAG, "" + temp);
            HttpUtils http = new HttpUtils();
            http.send(HttpRequest.HttpMethod.POST, Constants.Portrait, params,
                    new RequestCallBack<String>() {
                        @Override
                        public void onFailure(HttpException error, String msg) {
                            // 网络访问不成功

                        }

                        @Override
                        public void onSuccess(ResponseInfo<String> responseInfo) {
                            if (responseInfo != null) {

                                LogUtil.d(TAG, "upload  success "
                                        + responseInfo.result);
                                String result = responseInfo.result;
                                LoginInfo loginInfo = GsonTools
                                        .changeGsonToBean(result,
                                                LoginInfo.class);

                                if (loginInfo.getCode() == Constants.SUCCESSFUL_CODE) {
                                    // 清除指定URL的内存、磁盘缓存
                                    mBitmapUtils.clearCache(Setting
                                            .getCurrentUserPortrait());
                                    // 清空内存、磁盘缓存
                                    // mBitmapUtils.clearCache();
                                    Setting.setCurrentUserPortrait(loginInfo
                                            .getResult().getPortrait());
                                }
                                ToastUtil.TextToast(mContext,
                                        loginInfo.getMsg(), 0);

                            }
                        }

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





try {
                if (data != null) {
                    Bundle extras = data.getExtras();
                    bitmap = extras.getParcelable("data");
                    //this.iv_head_img.setImageBitmap(bitmap);
                    // tempFile.delete();//删除本地文件

                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.PNG, 70, stream);

                    byte[] byteArray = stream.toByteArray();
                    InputStream is = new ByteArrayInputStream(byteArray);

                    OutputStream outStream = new ByteArrayOutputStream();// 输出到内存

                    int len = 0;
                    byte[] buffer = new byte[1024];
                    while ((len = is.read(buffer)) != -1) {
                        outStream.write(buffer, 0, len);//
                    }
                    byte[] content_byte = ((ByteArrayOutputStream) outStream).toByteArray();
                    InputStream is2 = new ByteArrayInputStream(((ByteArrayOutputStream) outStream).toByteArray());

                    uploadHeadImage(Setting.getTelphone(), content_byte, is2);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

         RequestParams requestParams = new RequestParams();
        requestParams.addBodyParameter("telphone", telphone);
        requestParams.addBodyParameter("image", is, bytes.length, "portrait_1.png", ".png");


0 0
原创粉丝点击