在Android中保存多个图像

时间:2012-07-03 07:39:43

标签: android save-image

我有一个基本图像视图,可以从SD卡加载图像。用户可以在按钮单击时将templates(bitmap images)添加到此图像视图,并可以将添加的图像定位到用户需要。我想将这些多个图像作为单个图像保存到SD卡。我怎么能这样做?

2 个答案:

答案 0 :(得分:3)

您可以简单地获取ImageView的DrawingCache,然后将其转换为Bitmap,然后将其保存到SDCARD。

imageview.setDrawingCacheEnabled(true);
Bitmap b = imageview.getDrawingCache();
b.compress(CompressFormat.JPEG, 100, new FileOutputStream("/sdcard/image.jpg"));

您可能必须在清单中设置SDCARD写入权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

答案 1 :(得分:3)

看看这个片段:

How to merge multiple images into one image - Java ImageIO

int rows = 2;   //we assume the no. of rows and cols are known and each chunk has equal width and height  
        int cols = 2;  
        int chunks = rows * cols;  

        int chunkWidth, chunkHeight;  
        int type;  
        //fetching image files  
        File[] imgFiles = new File[chunks];  
        for (int i = 0; i < chunks; i++) {  
            imgFiles[i] = new File("archi" + i + ".jpg");  
        }  

       //creating a bufferd image array from image files  
        BufferedImage[] buffImages = new BufferedImage[chunks];  
        for (int i = 0; i < chunks; i++) {  
            buffImages[i] = ImageIO.read(imgFiles[i]);  
        }  
        type = buffImages[0].getType();  
        chunkWidth = buffImages[0].getWidth();  
        chunkHeight = buffImages[0].getHeight();  

        //Initializing the final image  
        BufferedImage finalImg = new BufferedImage(chunkWidth*cols, chunkHeight*rows, type);  

        int num = 0;  
        for (int i = 0; i < rows; i++) {  
            for (int j = 0; j < cols; j++) {  
                finalImg.createGraphics().drawImage(buffImages[num], chunkWidth * j, chunkHeight * i, null);  
                num++;  
            }  
        }  
        System.out.println("Image concatenated.....");  
        ImageIO.write(finalImg, "jpeg", new File("finalImg.jpg"));