我从某个地方得到了一个bitmap对象我想把它存到sdcard上面,保存的格式是png,可我发现用compress,和FileOutputStream 配合使用只能保存一个比较小的png图片,如果前面得到的那bitmap图片过大就行不通了。 FileOutputStream out = new FileOutputStream(dir);
img.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
不知道怎么去解决这个问题?

解决方案 »

  1.   

    直接上代码,代码片段,可能有的变量没有定义,请注意。    /**
         * Store image to SD card.
         */
        private String storeImageToFile(Bitmap bitmap){
            if(bitmap == null){
                return null;
            }        int count = 15;   //the number which will prevent the create segment locked.
            File file = null;
            RandomAccessFile accessFile = null;
            int MagicNum;
            String path = null;
            
            do{
                MagicNum = (int)mRandom.nextLong();
                path = path + "/" + String.valueOf(MagicNum) + ".png";
                file = new File(path);
                count--;
            }while(!file.exists() && count > 0);
            
            ByteArrayOutputStream steam = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, steam);
            byte[] buffer = steam.toByteArray();        try {
                accessFile = new RandomAccessFile(file, "rw");
                accessFile.write(buffer);
            } catch (Exception e) {
                return null;
            }
            
            try {
                steam.close();
                accessFile.close();
            } catch (IOException e) {
                //Note: do nothing.
            }
            
            return path;
        }
      

  2.   

    你也完成了生成图片并保存在sd卡里,我也要做这个但是不知道怎么做,可不可以帮我看看啊。我的qq1909731318谢谢了你好心人。
      

  3.   

    可以通过压缩后再保存,取出时在放大 Bitmap.createScaledBitmap(bitmap, width, height, false);