我正在学习将图像上传到从画廊或相机 android 获取图像的服务器....
当我从画廊或相机拍摄到带有图像解码的图像视图后显示图像时,图像不会模糊...... 但在我上传后,图像就像是小尺寸和模糊......
不知道,错在哪里。无论是在解码图像上还是上传图像上
这是我的代码的一部分
解码代码
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
上传后,并显示在列表视图中
我希望任何人都可以帮助我。对不起,如果我的英语不好...