android 上传图片成功但是显示错误(全是灰色或者是马赛克图片)

Xshuo1993 发布于 2016/11/11 15:03
阅读 308
收藏 0

我上传图片先又bitmap转成base64代码如下:

/*
* 将bitmap转换为base64字节数组
*/
public static String bitmapToBase64(Bitmap bitmap) {


String result = "";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] bitmapBytes = baos.toByteArray();
result = Base64.encodeToString(bitmapBytes, Base64.NO_WRAP);
// Base64Encoder encoder = new Base64Encoder(baos);
// result=encoder.encode(bitmapBytes);
return new String(result);
}

然后将其上传:

public FileUploadEntity uploadPic2(String param) throws XLException {
try {
String json = RequestUtils.sendPostByHttp("/com/picupload.html",
"pic=" + param);
Log.d("uploadPic", param);
int a = param.length();
Log.d(TAG, "json:" + json);
FileUploadEntity order = mGson.fromJson(json,
FileUploadEntity.class);
if (order != null) {
return order;
} else {
throw new XLException(new Result(Result.DEFAULT_ERROR));
}
} catch (JsonParseException e) {
throw new XLException(e, new Result(Result.JSON_ERROR));
}
}

这里我把post请求的代码也贴出来:

/**
* 向指定 URL 发送POST方法的请求

* @param url
*            发送请求的 URL
* @param param
*            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
public static String sendPostByHttp(String url, String param){
url = Constant.SERVER_URL + url;
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestProperty("Accept-Charset", "UTF-8");
conn.setRequestProperty("contentType", "UTF-8");
conn.setReadTimeout(READ_TIME_OUT_NORMAL);
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
conn.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
// logger.error("发送Http的POST 请求出现异常!" + e);
}
// 使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}

希望哪位大神能帮忙看看,很急!

加载中
返回顶部
顶部