Why camera doesn't fetch parameters when it moves to more light area?
<p>I developed an application to generate graph by tapping finger to the back camera and fetching parameters from camera preview. Whenever the device is in dark location it works fine and when it moves to more lighter area it doesn't work. Any help please where I might be doing mistake in this application?No error in Log cat! My code:</p>

<pre><code>public class Graph extends Activity implements OnClickListener {

    //Declaring Objects
    Context cnt;
    Callback c;
    boolean mFinished;
    DrawMyGraph g;
    ImageView clock,graph_Text,heartBlink_View,graph_View;
    static long startTime = 0;
    static int beats = 0;
    volatile boolean show=false;
    SurfaceHolder sh;
    SurfaceView sv;
    TextView bpmText_View,showBpm_View,time_View;
    Button on,off;
    int no,count=0,time=29;
    MediaPlayer mp=new MediaPlayer();
    TelephonyManager graph_Call;
    Camera mCamera = Camera.open();
    Intent i;
    AnimationDrawable heart_Anim=new AnimationDrawable();
    AnimationDrawable clock_Anim=new AnimationDrawable();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.graph);

        //Creating Object's Ids
        bpmText_View=(TextView)findViewById(R.id.bpmtextview);
        showBpm_View=(TextView)findViewById(R.id.showfinalbpmview);
        time_View=(TextView)findViewById(R.id.timeview);
        sv=(SurfaceView)findViewById(R.id.surfaceView);
        //clock=(ImageView)findViewById(R.id.clock);
        graph_Text=(ImageView)findViewById(R.id.slideView);
        heartBlink_View=(ImageView)findViewById(R.id.heartblinkview);
        graph_View=(ImageView)findViewById(R.id.graphview);
        graph_Text.setBackgroundResource(R.drawable.graphtext2);
        on=(Button)findViewById(R.id.on);
        off=(Button)findViewById(R.id.off);
        bpmText_View.bringToFront();


        //For designing Fonts Of TextView
        Typeface typeface=Typeface.createFromAsset(getAssets(), "fonts/digital-7.ttf");
        time_View.setTypeface(typeface);
        time_View.setText("30");

        //Calling TelephonyManager
        graph_Call = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        graph_Call.listen(GraphListener, PhoneStateListener.LISTEN_CALL_STATE);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        off.getBackground().setAlpha(200);
        on.getBackground().setAlpha(75);
        on.setEnabled(false);
        off.setOnClickListener(this);
        g = new DrawMyGraph(this, 25);
        addContentView(g, new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        startCamPreview();

    }

    //Writing button Event for Off Button
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            timer.cancel();
            off.getBackground().setAlpha(75);
            on.getBackground().setAlpha(200);
            mCamera.stopPreview();

            mp.release();

            i=new Intent(Graph.this,RealTime.class).putExtra("REALTIME", "off");
            startActivity(i);
            finish();
        }

    public void startCamPreview() {
        SurfaceView sv = (SurfaceView) findViewById(R.id.surfaceView);
        final SurfaceHolder sh = sv.getHolder();
        //Starting camera Preview

        try {
            mCamera.setPreviewDisplay(sh);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        sh.addCallback(new Callback() {

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                // TODO Auto-generated method stub
                System.out.println("Deleted");
                mCamera.stopPreview();
                 try {
                    mCamera.setPreviewDisplay(null);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 


                mCamera.release();
                mCamera = null;
            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                System.out.println("Created");
                timer.start();
                mCamera.setPreviewCallback(new PreviewCallback() {
                    public void onPreviewFrame(byte[] data, Camera camera) {

                        Camera.Parameters params = camera.getParameters();

                        int width = params.getPreviewSize().width;
                        int height = params.getPreviewSize().height;
                        byte[] mYUVData = new byte[data.length];
                        int[] mRGBData = new int[width * height];
                        System.arraycopy(data, 0, mYUVData, 0, data.length);
                        decodeYUV420SP(mRGBData, mYUVData, width, height);
                        int[] mRedHistogram = new int[256];
                        calculateIntensityHistogram(mRGBData, mRedHistogram,
                                width, height, 0);
                        double imageRedMean = 0;
                        double redHistogramSum = 0;
                        for (int bin = 0; bin &lt; 256; bin++) {
                            imageRedMean += mRedHistogram[bin] * bin;
                            redHistogramSum += mRedHistogram[bin];
                        } // bin
                        imageRedMean /= redHistogramSum;
                        double[] mBinSquared;
                        mBinSquared = new double[256];
                        for (int bin = 0; bin &lt; 256; bin++) {
                            mBinSquared[bin] = ((double) bin) * bin;
                        }
                        double imageRed2ndMoment = 0;
                        for (int bin = 0; bin &lt; 256; bin++) {
                            imageRed2ndMoment += mRedHistogram[bin]
                                    * mBinSquared[bin];
                        } // bin
                        imageRed2ndMoment /= redHistogramSum;
                        double imageRedStdDev = Math.sqrt((imageRed2ndMoment
                                - imageRedMean * imageRedMean));
                        int val = (new Double(imageRedStdDev * 100000)
                                .intValue());



                        for (int l = 0; l &lt; 1; l++) {

                        }
                        int yVal=250;
                        if (imageRedStdDev &lt; 15 &amp;&amp; val != 0)
                        {

                            //mp.release();
                            if(time&gt;=0){
                                time_View.setText(String.valueOf(time));
                                --time;
                            }

                            mp=MediaPlayer.create(getApplicationContext(), R.raw.heartbeat1);
                            try {
                                mp.prepare();
                            } catch (IllegalStateException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            mp.start();


                            show=true;
                            g.addPoint(yVal);
                            g.addPoint(yVal);
                            float flt=(float)Math.random();
                            g.addPoint(yVal+flt*25);
                            g.addPoint(yVal);
                            g.addPoint(yVal);
                            g.addPoint(yVal);
                            g.addPoint(yVal);
                            flt=(float)Math.random();
                            g.addPoint(yVal-flt*20);
                            g.addPoint(yVal);
                            g.addPoint(yVal);
                            flt=(float)Math.random();
                            g.addPoint(yVal-flt*30);
                            g.addPoint(yVal);
                            g.addPoint(yVal);
                            g.addPoint(yVal);
                            g.addPoint(yVal);
                            flt=(float)Math.random();
                            g.addPoint(yVal+flt*35);
                            g.addPoint(yVal-flt*30);
                            g.addPoint(yVal);
                            g.addPoint(yVal);
                            g.addPoint(yVal);
                            g.addPoint(yVal);

                            //mp.setLooping(true);
                            Random r=new Random();
                             no=r.nextInt(84-70)+70;
                            bpmText_View.setText(String.valueOf(no));


                            try {
                                Thread.sleep(700);
                                mp.release();
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }




                        } else {
                            show=false;
                            if(val==0){

                                time_View.setText(String.valueOf(time));
                                --time;
                                count++;
                                g.addPoint(yVal);
                                g.addPoint(yVal);
                                g.addPoint(yVal);
                                g.addPoint(yVal);
                                g.addPoint(yVal);
                                g.addPoint(yVal);
                                g.addPoint(yVal);
                                g.addPoint(yVal);
                                g.addPoint(yVal);
                                g.addPoint(yVal);
                                g.addPoint(yVal);
                                g.addPoint(yVal);
                                mp.release();
                                mp=MediaPlayer.create(getApplicationContext(), R.raw.beep);
                                bpmText_View.setText(String.valueOf(0));
                                try {
                                    mp.prepare();
                                } catch (IllegalStateException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                                mp.start();
                                try {
                                    Thread.sleep(700);
                                } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }

                            }
                            else{
                            mp.release();
                            time_View.setText(String.valueOf(time));
                            --time;
                            count++;
                            g.addPoint(yVal);
                            g.addPoint(yVal);
                            g.addPoint(yVal);
                            g.addPoint(yVal);
                            g.addPoint(yVal);
                            g.addPoint(yVal);
                            g.addPoint(yVal);
                            g.addPoint(yVal);
                            g.addPoint(yVal);
                            g.addPoint(yVal);
                            g.addPoint(yVal);
                            g.addPoint(yVal);
                            mp=MediaPlayer.create(getApplicationContext(), R.raw.beep);
                            bpmText_View.setText(String.valueOf(0));
                            try {
                                mp.prepare();
                            } catch (IllegalStateException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            mp.start();
                            try {
                                Thread.sleep(800);
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }



                            }
                        }
                    }

                });
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format,
                    int width, int height) {
                System.out.println("Changed");
                Camera.Parameters parameters = mCamera.getParameters();
                parameters.setPreviewSize(320, 240);
                parameters.setPreviewFrameRate(15);
                parameters.setSceneMode(Camera.Parameters.SCENE_MODE_NIGHT);
                parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
                mCamera.setParameters(parameters);
                mCamera.startPreview();
                g.invalidate();
            }
        });

    }

    static public void decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width,
            int height) {
        final int frameSize = width * height;

        for (int j = 0, yp = 0; j &lt; height; j++) {
            int uvp = frameSize + (j &gt;&gt; 1) * width, u = 0, v = 0;
            for (int i = 0; i &lt; width; i++, yp++) {
                int y = (0xff &amp; ((int) yuv420sp[yp])) - 16;
                if (y &lt; 0)
                    y = 0;
                if ((i &amp; 1) == 0) {
                    v = (0xff &amp; yuv420sp[uvp++]) - 128;
                    u = (0xff &amp; yuv420sp[uvp++]) - 128;
                }

                int y1192 = 1192 * y;
                int r = (y1192 + 1634 * v);
                int g = (y1192 - 833 * v - 400 * u);
                int b = (y1192 + 2066 * u);

                if (r &lt; 0)
                    r = 0;
                else if (r &gt; 262143)
                    r = 262143;
                if (g &lt; 0)
                    g = 0;
                else if (g &gt; 262143)
                    g = 262143;
                if (b &lt; 0)
                    b = 0;
                else if (b &gt; 262143)
                    b = 262143;

                rgb[yp] = 0xff000000 | ((r &lt;&lt; 6) &amp; 0xff0000)
                        | ((g &gt;&gt; 2) &amp; 0xff00) | ((b &gt;&gt; 10) &amp; 0xff);
            }
        }
    }

    static public void calculateIntensityHistogram(int[] rgb, int[] histogram,
            int width, int height, int component) {
        for (int bin = 0; bin &lt; 256; bin++) {
            histogram[bin] = 0;
        } // bin
        if (component == 0) // red
        {
            for (int pix = 0; pix &lt; width * height; pix += 3) {
                int pixVal = (rgb[pix] &gt;&gt; 16) &amp; 0xff;
                histogram[pixVal]++;
            } // pix
        } else if (component == 1) // green
        {
            for (int pix = 0; pix &lt; width * height; pix += 3) {
                int pixVal = (rgb[pix] &gt;&gt; 8) &amp; 0xff;
                histogram[pixVal]++;
            } // pix
        } else // blue
        {
            for (int pix = 0; pix &lt; width * height; pix += 3) {
                int pixVal = rgb[pix] &amp; 0xff;
                histogram[pixVal]++;
            } // pix


        }
    }

    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        synchronized (ACCESSIBILITY_SERVICE) {
        heartBlink_View.setBackgroundResource(R.anim.heartblinkanim);
        heart_Anim=(AnimationDrawable)heartBlink_View.getBackground();

        heart_Anim.start();

        }

        }

    CountDownTimer timer=new CountDownTimer(30000,1000){

        @Override
        public void onFinish()throws NullPointerException {
            // TODO Auto-generated method stub

            mp.release();
            mCamera.stopPreview();
            Canvas.freeGlCaches();

            if(count&gt;0)
            {

                i=new Intent(Graph.this,RealTime.class).putExtra("REALTIME", "zerobpm");
                startActivity(i);
                finish();
                }
            else
            {
                System.out.println(count);              
                i=new Intent(Graph.this,RealTime.class).putExtra("REALTIME", "validbpm");
                startActivity(i);
                finish();
            }
        }

        @Override
        public void onTick(long arg0) {
            // TODO Auto-generated method stub

        }

    };

    //Code For DrawMyGraph

        class DrawMyGraph extends View {
        public LinkedList&lt;Float&gt; yValues = null;
        private int size = 0;
        private LinkedList&lt;Integer&gt; xValues = null;
        private LinkedList&lt;Long&gt; beatList = new LinkedList&lt;Long&gt;();

        public DrawMyGraph(Context context, int size) {
            super(context);
            yValues = new LinkedList&lt;Float&gt;();
            int inc = 5;
            this.size = size * inc;
            xValues = new LinkedList&lt;Integer&gt;();

            int cnt = 468;
            for (int i = 0; i &lt; this.size * inc; i++) {
                xValues.add(cnt);
                cnt = cnt - (inc - 2);
            }
        }

        @Override
        protected void onDraw(Canvas canvas) {

            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(2);
            paint.setColor(Color.BLACK);
            invalidate();

            for (int i = 0, j = 1; i &lt; yValues.size() &amp;&amp; j &lt; yValues.size(); i++, j++) {


                canvas.drawLine(xValues.get(i)-40 , (yValues.get(i) -64)*2,
                        xValues.get(j)-40 , (yValues.get(j) -64)*2, paint);




            }
            super.onDraw(canvas);
    }

        public int getSize() {
            return size;
        }

        public void addPoint(float p) {
            if (yValues.size() &gt; size) {
                yValues.removeLast();
            }
            yValues.addFirst(p);
        }

        public void addBeat(long beat) {
            beatList.add(beat);
        }

    }

        @Override
        public void onBackPressed() {
            // TODO Auto-generated method stub
            super.onBackPressed();
            timer.cancel();
            mCamera.stopPreview();
            heart_Anim.stop();
            //Calling Home Page
            startActivity(new Intent(Graph.this,Home.class));
            mp.release();
            finish();
        }

    ////Handling phone states
            private PhoneStateListener GraphListener = new PhoneStateListener() {
                  public void onCallStateChanged(int state, String incomingNumber) {
                   try {
                    switch (state) {
                    case TelephonyManager.CALL_STATE_RINGING:
                    // Toast.makeText(Graph.this, "CALL_STATE_RINGING", Toast.LENGTH_SHORT).show();
                     mp.pause();
                     break;
                    case TelephonyManager.CALL_STATE_OFFHOOK:
                    // Toast.makeText(Graph.this, "CALL_STATE_OFFHOOK", Toast.LENGTH_SHORT).show();
                     mp.pause();
                     break;
                    case TelephonyManager.CALL_STATE_IDLE:
                    // Toast.makeText(Graph.this, "CALL_STATE_IDLE", Toast.LENGTH_SHORT).show();
                     mp.start();
                     break;
                    default:
                    // Toast.makeText(Graph.this, "default", Toast.LENGTH_SHORT).show();

                    }
                   } catch (Exception e) {

                   }
                  }
                 };  


}
</code></pre>
