Пример #1
0
  /**
   * 使用andbase框架 头像上传的方法 @2015-11-20 * 上传用户头像图片uploadUserImg // @param id 用户id // @param file 文件
   * // @param fileName 文件名字
   */
  public static void UpLoad_Portrait(final Context context, Uri uri) {
    AbHttpUtil mAbHttpUtil = AbHttpUtil.getInstance(context);
    String url = DataInit.httpHead + "uploadUserImg" + ".action";
    AbRequestParams params = new AbRequestParams();
    File file1 = null;
    try {
      // 多文件上传添加多个即可
      file1 = new File(ImageTool.getPath(context, uri));
      if (!file1.exists()) {
        throw new Exception("图片路径错误");
      }
      if (file1.getTotalSpace() > 4 * 1024 * 1024) {
        Point point = ImageTool.getScreenSize(context);
        Bitmap bitmap = ImageTool.readUri(uri, context, point.y, point.y, true);
        String ImagesDir =
            BaseDataManager.getMyBaseDataManager(context).getDataInit().getSD_Directory();
        Log.i("正常", "图片过大,转换中的路径ImagesDir=" + ImagesDir);
        String fileName = file1.getName();
        ImageTool.saveFile(bitmap, fileName, ImagesDir);
        file1 = new File(ImagesDir, fileName);
      }
      params.put("fileName", file1.getName());
      params.put("file", file1);
      params.put("id", BaseDataManager.getMyBOSSInfo(context).getId());
    } catch (Exception e) {
      Log.e("异常201511201059", e.toString());
      return;
    }
    mAbHttpUtil.post(
        url,
        params,
        new AbStringHttpResponseListener() {
          @Override
          public void onSuccess(int statusCode, String content) {
            try {
              JSONObject jsonObject = new JSONObject(content);
              Toast.makeText(context, Reader.analysis("message", jsonObject), Toast.LENGTH_SHORT)
                  .show();
            } catch (Exception e) {
              Log.e("异常201511201311", e.toString());
            }
          }

          // 开始执行前
          @Override
          public void onStart() {
            Log.i("正常", "onStart");
          }

          @Override
          public void onFailure(int statusCode, String content, Throwable error) {
            Toast.makeText(context, error.getMessage(), Toast.LENGTH_SHORT).show();
          }

          // 进度
          @Override
          public void onProgress(int bytesWritten, int totalSize) {}

          // 完成后调用,失败,成功,都要调用
          public void onFinish() {
            Log.i("正常", "结束");
          }
        });
  }
Пример #2
0
  /** 使用andbase框架 聊天图片上传的方法 @2015-11-16 */
  public static void UpLoadImage_Chat(
      final Context context,
      Uri uri,
      String QuestionId,
      String userId,
      String toUserId,
      String petId,
      final Trancaction trancaction) {
    AbHttpUtil mAbHttpUtil = AbHttpUtil.getInstance(context);
    String url = DataInit.httpHead + "chatLogImgSave" + ".action";
    AbRequestParams params = new AbRequestParams();
    File file1 = null;
    try {
      // 多文件上传添加多个即可
      file1 = new File(ImageTool.getPath(context, uri));
      if (!file1.exists()) {
        throw new Exception("图片路径错误");
      }
      if (file1.getTotalSpace() > 4 * 1024 * 1024) {
        Point point = ImageTool.getScreenSize(context);
        Bitmap bitmap = ImageTool.readUri(uri, context, point.y, point.y, true);
        String ImagesDir =
            BaseDataManager.getMyBaseDataManager(context).getDataInit().getSD_Directory();
        Log.i("正常", "图片过大,转换中的路径ImagesDir=" + ImagesDir);
        String fileName = file1.getName();
        ImageTool.saveFile(bitmap, fileName, ImagesDir);
        file1 = new File(ImagesDir, fileName);
      }
      params.put("fileName", file1.getName());
      params.put("file", file1);
      params.put("id", QuestionId);
      params.put("userId", userId);
      params.put("toUserId", toUserId);
      params.put("petId", petId);
    } catch (Exception e) {
      Log.e("异常201511161459", e.toString());
      return;
    }
    mAbHttpUtil.post(
        url,
        params,
        new AbStringHttpResponseListener() {
          @Override
          public void onSuccess(int statusCode, String content) {
            Log.i("正常", "onSuccess=" + statusCode);
            try {
              if (statusCode == 200) {
                trancaction.onResponse(new JSONObject(content));
              } else {
                trancaction.onErrorResponse(new VolleyError());
              }
            } catch (Exception e) {
              Log.e("异常201512021617", e.toString());
            }
          }

          // 开始执行前
          @Override
          public void onStart() {
            Log.i("正常", "onStart");
          }

          @Override
          public void onFailure(int statusCode, String content, Throwable error) {
            Toast.makeText(context, error.getMessage(), Toast.LENGTH_SHORT).show();
          }

          // 进度
          @Override
          public void onProgress(int bytesWritten, int totalSize) {}

          // 完成后调用,失败,成功,都要调用
          public void onFinish() {
            Log.i("正常", "结束");
          }
        });
  }