【问题标题】:Image quality reduced after saving bitmap to SD card将位图保存到 SD 卡后图像质量下降
【发布时间】:2012-12-29 12:07:27
【问题描述】:

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

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

我正在将图像保存为 PNG 文件。

保存图像后,其质量会降低。我知道 PNG 不是无损的,但是有什么方法可以保存图像而不影响质量或分辨率??

代码如下:

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;

        }
    });
}

【问题讨论】:

    标签: java android image


    【解决方案1】:
    fullSizeImage.buildDrawingCache();
    Bitmap bm=fullSizeImage.getDrawingCache();
    

    fullSizeImage 所在的位置可能是一个 Image View。

    我发现在您加载图像时出现了问题。你一定用过 BitmapFactory.Options 之类的东西

    进行以下更改

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

    【讨论】:

    • BitmapFactory.decodeStream(,param2,null);是什么的替代品?
    • 正在做任何延迟加载的事情吗?还是将 url 解码为 bmp 一次图像?
    • 将一张图片的 url 解码为 bmp。
    • 我认为在我的 imageview 中,图像的质量正确加载。活动中有另一个按钮可以将此 imageview 的图像设置为墙纸。当我将图像直接设置为墙纸时,它会设置为高质量图像。问题是当我保存到 SD 卡时,它的质量会降低
    • 为什么要使用 getDrawingCache() 方法。从网址解码后,您必须拥有一个 bmp。使用该 bmp 将其保存到 SD 卡。 getDrawingCache() 是不必要的步骤。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 2017-05-06
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    相关资源
    最近更新 更多