我的应用程序中有一个带有保存按钮的图像视图。当我们点击保存按钮时,当用户选择任意大小的图像以保存到设备外部设备时,应用程序将显示一些尺寸。在一些(低)尺寸下工作正常,但对于大尺寸,它是强制关闭。我面临异常的记忆。我不想失去图像的质量。
请在此
中提供任何帮助答案 0 :(得分:0)
以下是将位图保存到Sdcard的代码
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
curentDateandTime = sdf.format(new Date());
File file = new File(mScreenshotPath + "/"+curentDateandTime+".png");
FileOutputStream fos;
try
{
System.runFinalization();
Runtime.getRuntime().gc();
System.gc();
fos = new FileOutputStream(file);
originalImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
}
catch (FileNotFoundException e)
{
// Log.e("Panel", "FileNotFoundException", e);
}
catch (IOException e)
{
// Log.e("Panel", "IOEception", e);
}
答案 1 :(得分:0)
使用BitmapFactory.Options保存图像而不会出现如下所示的错误。
final int DESIRED_WIDTH = 640;
// Set inJustDecodeBounds to get the current size of the image; does not
// return a Bitmap
final BitmapFactory.Options sizeOptions = new BitmapFactory.Options();
sizeOptions.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, sizeOptions);
Log.d(TAG, "Bitmap is " + sizeOptions.outWidth + "x"
+ sizeOptions.outHeight);
// Now use the size to determine the ratio you want to shrink it
final float widthSampling = sizeOptions.outWidth / DESIRED_WIDTH;
sizeOptions.inJustDecodeBounds = false;
// Note this drops the fractional portion, making it smaller
sizeOptions.inSampleSize = (int) widthSampling;
Log.d(TAG, "Sample size = " + sizeOptions.inSampleSize);
// Scale by the smallest amount so that image is at least the desired
// size in each direction
final Bitmap result = BitmapFactory.decodeByteArray(data, 0, data.length,
sizeOptions);