Android:将位图保存到库中

时间:2012-12-18 13:31:22

标签: android bitmap save

我创建了一个循环通过两个位图并将像素数据存储到2D数组中的应用程序(compare1 [] []& compare2 [] [])。

我相信我的代码在某种程度上有效。看起来它保存了Bitmap,但是当我稍后去画廊时它就不存在了。任何想法将不胜感激;这是我的代码示例:

public void getPixels(int[][] compare2,int[][]compare1, int x) 
{
    Bitmap difference = Bitmap.createBitmap(width, height, Config.ARGB_8888);

    for(int i = 0; i<width-1; i++)
    {
        for(int j=0; j<height-1; j++)
        {

            int mColor = BIT.get(x).getPixel(i, j);

            int alpha = Color.alpha(mColor);
            int red = Color.red(mColor);                
            int green = Color.green(mColor);
            int blue = Color.blue(mColor);

            int xAvg = ((red+green+blue)/3);

            compare2[i][j] = xAvg;
            if ((compare1[i][j] - compare2[i][j]) < 50)
            {
                difference.setPixel(i, j, -16777216);
                //System.out.println("No Significant Change");

            }
            else
            {
                difference.setPixel(i, j, -1);
                //System.out.println("Change");
            }

        }


    }       
    try {
            String path = Environment.getExternalStorageDirectory().toString();
            OutputStream fOut = null;
            File file = new File(path,"Test.jpg");
            fOut = new FileOutputStream(file);

            difference.compress(Bitmap.CompressFormat.JPEG, 80, fOut);

            fOut.flush();
            fOut.close();
            fOut = null;

    } catch (Exception e) {
           e.printStackTrace();
    }

}

1 个答案:

答案 0 :(得分:0)

在您保存图片后,您需要调用MediaScanner才能让图片显示在图库中:

Intent intent = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory());
sendBroadcast(intent);

详细教程:Trigger Media Scanner.

关于SO的相关问题:How update the android media database