gpt4 book ai didi

java - 将位图保存到 SD 卡后图像质量下降

转载 作者:行者123 更新时间:2023-11-30 03:51:32 25 4
gpt4 key购买 nike

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

图像正在从我在互联网上托管图像的 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;

}
});
}

最佳答案

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

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

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

进行以下更改

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

关于java - 将位图保存到 SD 卡后图像质量下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14081497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com