我正在学习将图片上传到服务器,其中图片来自画廊或相机安卓......
当我使用图像解码从图库或相机拍摄到图像视图后显示图像时,图像不模糊... 但是我上传后,图片就像小尺寸和模糊一样..
我不知道,错误在哪里。是否在已解码图像或上传图片
这是我的代码的一部分
解码
public void decodeFile(String filePath) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 1024;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
bitmap = BitmapFactory.decodeFile(filePath, o2);
imgView.setImageBitmap(bitmap);
}
上传代码
try {
DatabaseHandler userDB = new DatabaseHandler(getApplicationContext());
HashMap<String, String> userDetail = userDB.getUserDetails();
String uid= userDetail.get("uid");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(PHP_URL);
ByteArrayBody bab = new ByteArrayBody(data,file_name);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("uploadedfile", bab);
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),"UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
return s.toString().trim();
} catch (Exception e) {
err="error"+e.getMessage();
Log.e(e.getClass().getName(), e.getMessage());
return e.getMessage();
}
上传前,图片显示在ImageView中
上传后,在listview中显示
我希望有人能帮助我。对不起,如果我的英语不好......
答案 0 :(得分:-1)
According to method documentation您正在压缩图像100%会降低质量
bitmap.compress(CompressFormat.JPEG, 100, bos);
我建议使用小于100的值并进行调整,直到您在质量和尺寸之间取得平衡