0

我的应用程序中有一个带有保存按钮的图像视图。当我们点击保存按钮时,当用户选择图像要保存到设备外部设备的任何尺寸时,应用程序将显示一些尺寸。在某些(低)尺寸下工作正常,但对于大尺寸,它是强制关闭的。我正面临着异常的记忆。我不想失去图像的质量。

请任何人帮助

4

2 回答 2

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);    
                    }
于 2014-03-18T11:09:52.983 回答
0

使用BitmapFactory.Options保存图像而不会出现 oom 错误,如下所示。

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);
于 2014-03-18T11:13:33.087 回答