title: How to Resolve the Android Camera setParameters failed
body: <p>We are used Camera functionality in our Application. App will be published in Google App Store also. but we are facing new problem is setParameters failed in our application some devices only like moto g second generation. Please find the error console and SurfaceView coding in below.how to resolve this problem please guide me. Thanks</p>
  
  <pre><code>07-09 11:54:08.460: E/AndroidRuntime(13821): FATAL EXCEPTION: main
  07-09 11:54:08.460: E/AndroidRuntime(13821): Process: com.example.surfaceviewtest, PID: 13821
  07-09 11:54:08.460: E/AndroidRuntime(13821): java.lang.RuntimeException: setParameters failed
  07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.hardware.Camera.native_setParameters(Native Method)
  07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.hardware.Camera.setParameters(Camera.java:1962)
  07-09 11:54:08.460: E/AndroidRuntime(13821):    at com.example.surfaceviewtest.Test.surfaceChanged(Test.java:58)
  07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.view.SurfaceView.updateWindow(SurfaceView.java:594)
  07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:177)
  07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
  07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2067)
  07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119)
  07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6060)
  07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
  07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.view.Choreographer.doCallbacks(Choreographer.java:670)
  07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.view.Choreographer.doFrame(Choreographer.java:606)
  07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
  07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.os.Handler.handleCallback(Handler.java:746)
  07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.os.Handler.dispatchMessage(Handler.java:95)
  07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.os.Looper.loop(Looper.java:148)
  07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.app.ActivityThread.main(ActivityThread.java:5443)
  07-09 11:54:08.460: E/AndroidRuntime(13821):    at java.lang.reflect.Method.invoke(Native Method)
  07-09 11:54:08.460: E/AndroidRuntime(13821):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
  07-09 11:54:08.460: E/AndroidRuntime(13821):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
  </code></pre>
  
  <p>xml file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical"
      android:background="#ffffffff"
      &gt;
  
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="9"
          android:orientation="vertical"
          android:id="@+id/shareLayout"
  
  
          &gt;
  
          &lt;FrameLayout
              android:id="@+id/FrameLayout1"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              &gt;
  
              &lt;com.example.surfaceviewtest.Test
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
  
                  /&gt;
  
  
              &lt;LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:id="@+id/weather"
                  android:orientation="vertical"
                    android:background="@android:color/transparent"
  
                  android:visibility="visible"
                  &gt;
  
                 &lt;!--   android:background="#4FFFFFFF" --&gt;
  
                  &lt;TextView
                      android:layout_width="match_parent"
                      android:layout_height="0dp"
                      android:layout_weight="6"
  
                      /&gt;
  
                  &lt;ImageView
                      android:id="@+id/imageView1"
                      android:layout_width="fill_parent"
                      android:layout_height="453dp"
                      android:adjustViewBounds="true"
                      android:background="@android:color/transparent"
                      android:scaleType="matrix"
                      android:src="@drawable/ic_launcher" /&gt;
  
                  &lt;TextView
                      android:layout_width="match_parent"
                      android:layout_height="0dp"
                      android:layout_weight="1"
                      android:gravity="center"
                      android:text="This is SurfaceView Test"
                      /&gt;
                  &lt;TextView
                      android:layout_width="match_parent"
                      android:layout_height="0dp"
                      android:layout_weight="1"
                      android:gravity="center"
                      android:text="Answerofgod.blogspot.kr"
                      /&gt;
                  &lt;TextView
                      android:layout_width="match_parent"
                      android:layout_height="0dp"
                      android:layout_weight="1"
                      android:gravity="center"
                      android:text="Answerofgod.tistory.com"
                      /&gt;
              &lt;/LinearLayout&gt;
  
          &lt;/FrameLayout&gt;
  
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:id="@+id/linearLayout2"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
  
          android:gravity="center_horizontal"&gt;
  
          &lt;Button
              android:id="@+id/share"
              android:layout_width="0dp"
              android:layout_weight="1"
              android:layout_height="match_parent"
              android:text="share"
              android:gravity="center"
              /&gt;
  
          &lt;Button
              android:id="@+id/exit"
              android:layout_width="0dp"
              android:layout_weight="1"
              android:layout_height="match_parent"
              android:text="explore"
              android:gravity="center"
              /&gt;
          &lt;ToggleButton
          android:id="@+id/onOffFlashlight"
          android:layout_width="0dp"
              android:layout_weight="1"
              android:layout_height="match_parent"
          android:textOn="Turn Off"
          android:textOff="Turn On"
          android:checked="false"
          android:text="Turn On/Off Camera LED/ Flashlight Android" /&gt;
      &lt;/LinearLayout&gt; 
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Test.java</p>
  
  <pre><code>public class Test extends SurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback{
  
      SurfaceHolder holder;   
      Camera cam=null;        
  int width;
  int height;
  private Camera.Size mPreviewSize;
  
      public Test(Context context, AttributeSet attrs) {
          super(context, attrs);
          init();
      }
      public Test(Context context) {
          super(context);
          init();
      }
      public void init(){
  
          holder=getHolder();
          holder.addCallback(this);
  
      }
  
  
  
       @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
          int height) {
         Camera.Parameters myParameters = cam.getParameters();
  
         int   wid = ((Activity) getContext()).getWindowManager().getDefaultDisplay().getWidth(); 
         int hei = ((Activity) getContext()).getWindowManager().getDefaultDisplay().getHeight();
  
         System.out.println("wid"+wid);
         System.out.println("hei"+hei);
         Camera.Size myBestSize = getBestPreviewSize(hei, wid, myParameters);
  
         if(myBestSize != null){
          myParameters.setPreviewSize(hei, wid);
          cam.setParameters(myParameters);
          cam.setDisplayOrientation(90);
          cam.startPreview();
         // isPreview = true;
  
  
         }
        }
  
       private Camera.Size getBestPreviewSize(int width, int height, Camera.Parameters parameters){
           Camera.Size bestSize = null;
           List&lt;Camera.Size&gt; sizeList = parameters.getSupportedPreviewSizes();
  
           bestSize = sizeList.get(0);
  
           for(int i = 1; i &lt; sizeList.size(); i++){
            if((sizeList.get(i).width * sizeList.get(i).height) &gt;
              (bestSize.width * bestSize.height)){
             bestSize = sizeList.get(i);
            }
           }
  
           return bestSize;
          }
  
  
  
  
  
      /*@Override
      public void surfaceChanged(SurfaceHolder holder, int format, int width,
              int height) {
  
          cam.setDisplayOrientation(90);      
          cam.startPreview();                 
      }*/
      @Override
      public void surfaceCreated(SurfaceHolder holder) {
          cam=Camera.open();              
  
          try{
              cam.setPreviewDisplay(holder);  
              cam.setPreviewCallback(this);
          }catch(Exception e){
              e.printStackTrace();
          }
      }
      @Override
      public void surfaceDestroyed(SurfaceHolder holder) {
          cam.setPreviewCallback(null);
          cam.stopPreview();
          cam.release();                  
          cam=null;
  
      }
  
      /*@Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
          height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
      }*/
  
  
      @Override
      public void onPreviewFrame(final byte[] data, Camera camera) {
  
          Camera.Parameters params = camera.getParameters();
           Camera.Size previewSize = params.getSupportedPreviewSizes().get(0);
          int wi = previewSize.width;
          //System.out.println("wi======"+wi);
          int he = previewSize.height;
          //params.setPreviewSize(wi, he);
          System.out.println("wi==========="+wi);
          System.out.println("he======"+he);
          int w = params.getPreviewSize().width;
      System.out.println("w=============="+ w);
          int h = params.getPreviewSize().height;
          System.out.println(" h =============="+ h );
          int format = params.getPreviewFormat();
  
  
          int  wid = ((Activity) getContext()).getWindowManager().getDefaultDisplay().getWidth(); 
          int hei = ((Activity) getContext()).getWindowManager().getDefaultDisplay().getHeight();
  
  
  
  
          YuvImage image = new YuvImage(data, format,w, h, null);
  
  
  
  
  
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          Rect area = new Rect(0, 0,w, h);
          image.compressToJpeg(area, 100, out);
          Bitmap bm = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size());
  
  
  
  
  
  
          Matrix matrix = new Matrix();
          matrix.postRotate(90);
           //mBitmap = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight()gr
              //     mtrxFreeze, true);
  
          Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
          //Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0,w, he, matrix, true);
  
          //System.out.println("shareBitmap   getHeight"+rotatedBitmap.getHeight());
          //System.out.println("shareBitmap   getWidth"+rotatedBitmap.getWidth());
  
  
          MainActivity.shareBitmap=rotatedBitmap;
  
      }
  }
  </code></pre>