我需要从服务器下载图片并以其原始质量保存,但是保存后,图片被压缩且质量下降。我使用android-async-http库进行http请求。我的请求和文件保存代码如下:
AsyncHttpClient client = new AsyncHttpClient();
client.get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int arg0, Header[] arg1, byte[] response) {
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inPreferQualityOverSpeed=true;
Bitmap bitmap = BitmapFactory.decodeByteArray(response , 0, response .length,bmOptions);
mImageView.setImageBitmap(bitmap);
bitmapToFile(bitmap);
});
File bitmapToFile(Bitmap bitmap){
// convert Bitmap to File
File newImage;
try {
newImage = functions.createImageFile();
FileOutputStream fos = new FileOutputStream(newImage);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
Uri contentUri = Uri.fromFile(newImage);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
} catch (IOException e) {
e.printStackTrace();
return null;
}
return newImage;
}
protected File createImageFile() throws IOException {
// Create an image file name
String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
File albumF = getAlbumDir();
File imageF = File.createTempFile(imageFileName, JPEG_FILE_SUFFIX, albumF);
return imageF;
}
compress()
调用,您自己压缩图像作为写出图像的一部分。 - CommonsWare