如何在不压缩的情况下将位图转换为文件 [英] How to convert bitmap to File without compressing

查看:192
本文介绍了如何在不压缩的情况下将位图转换为文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从服务器下载图像并以原始质量保存它,但是显然保存后的图像被压缩并且质量下降了。

I need to download image from a server and save it in its original quality, but apparently the after saving the image is compressed and the quality decreased.

我使用用于http请求的android-async-http库。我的请求代码和保存文件:

I use android-async-http library for http requests. my code of the request and saving file:

     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;
}


推荐答案

删除该BitmapFactory并只需将参数 byte []响应中的所有字节直接保存到文件中即可。

Do away with that BitmapFactory and just save all the bytes in parameter byte[] response directly to file.

这篇关于如何在不压缩的情况下将位图转换为文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆