How to convert bitmap to File without compressing

非 Y 不嫁゛ 提交于 2019-12-10 10:08:30

问题


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.

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

回答1:


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



来源:https://stackoverflow.com/questions/28561263/how-to-convert-bitmap-to-file-without-compressing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!