1

I've created an app that loops through two bitmaps and store the pixel data into 2D arrays (compare1[][] & compare2[][]).

I believe my code is working to some extent. It looks like it saves the Bitmap but when I go to the gallery later its not there. Any ideas would be greatly appreciated; here is a sample of my code:

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();
    }

}
Ledgey1101
  • 85
  • 3
  • 11
  • you need to use bitmap.create to create a new bitmap, and then bitmap.compress to write it to a file – njzk2 Dec 18 '12 at 13:34
  • @etienne please scroll down: – Ledgey1101 Dec 18 '12 at 13:37
  • @njzk2 Am i not doing that already? Bitmap difference = Bitmap.createBitmap(width, height, Config.ARGB_8888); difference.compress(Bitmap.CompressFormat.JPEG, 80, fOut); – Ledgey1101 Dec 18 '12 at 13:40
  • yes, sorry, didn't see that. not appearing in the gallery is it ? then you probably need to either A/ put it somewhere it is seen by the gallery or B/ put it in the mediastore yourself – njzk2 Dec 18 '12 at 13:48

1 Answers1

0

After you've saved the Image, you need to invoke the MediaScanner for the image to appear in the gallery:

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

A detailed tutorial: Trigger Media Scanner.

Related question on SO:How update the android media database

Community
  • 1
  • 1
Gunnar Karlsson
  • 28,350
  • 10
  • 68
  • 71