将位图保存到SD卡后图像质量下降 [英] Image quality reduced after saving bitmap to SD card

查看:149
本文介绍了将位图保存到SD卡后图像质量下降的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个活动来显示一个完整的图像,用户有一个按钮来保存它。

I am using an activity to display a Full image and user has a button to save it.

图像正在从我托管图像的URL的imageview中加载在互联网上。

Image is being loaded in imageview from URL where I have hosted image on internet.

我将图像保存为PNG文件。

I am saving an image as PNG file.

保存图像后,其质量会降低。我知道PNG不是无损的,但有什么方法可以保存图像,因为它没有任何质量或分辨率的妥协吗?

After saving image its quality is reduced. I know PNG is not lossless, but is there any way I can save an image as it is without any compromise with quality or resolution??

这是代码:

buttonSave.setOnClickListener(new Button.OnClickListener()
{
    public void onClick(View arg0) 
    {
        fullSizeImage.buildDrawingCache();
        Bitmap bm=fullSizeImage.getDrawingCache();
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + File.separator + "MyWallpapers");
        if(!myDir.exists())
        {
            myDir.mkdir();
        }
        String fname = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date())+".png";
        File file = new File (myDir, fname);
        /*if (file.exists ()) file.delete (); */
        try 
        {
            //FileOutputStream out = new FileOutputStream(file);
            /*FileOutputStream out = new FileOutputStream(file);

            bm.compress(Bitmap.CompressFormat.PNG, 100, out);*/

            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.PNG, 100, bytes);

            file.createNewFile();
            //write the bytes in file
            FileOutputStream fo = new FileOutputStream(file);
            fo.write(bytes.toByteArray());
            bytes.flush();
            bytes.close();
            /*out.flush();
            out.close();*/
            Toast.makeText(FullSizeImageDisplay.this, "Wallpaper Saved in SDcard/MyWallpapers folder",Toast.LENGTH_SHORT).show();
            String[] paths = {root + File.separator + "MyWallpapers"};

            String[] mediaType = {"image/png"};
            MediaScannerConnection.scanFile(FullSizeImageDisplay.this, paths, mediaType, null);
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
            values.put(MediaStore.Images.Media.MIME_TYPE, "image/png"); 

            getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }

        OutputStream fOut = null;
        Uri outputFileUri;

        }
    });
}


推荐答案

fullSizeImage.buildDrawingCache();
Bitmap bm=fullSizeImage.getDrawingCache();

其中fullSizeImage可能是图像视图。

Where fullSizeImage is may be an Image View.

我看到加载图片时出现问题。您必须使用类似BitmapFactory.Options的内容

I see problem is arising while you load the image. You must have used something like BitmapFactory.Options

进行以下更改

BitmapFactory.decodeStream(<param1>,param2,null);

这篇关于将位图保存到SD卡后图像质量下降的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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