java.Lang.RuntimeException,setParameters在android(4.1.1)版本中失败

时间:2013-03-27 05:04:27

标签: android android-camera android-sdcard android-camera-intent

我开发了一个应用程序,可以在打卡时拍摄照片。它在Acer选项卡上运行良好(捕获图像并保存在SD卡中)。现在,当我在三星Galaxy(Android-4.1.1)中运行相同的应用程序时,我的应用程序正在变得“不幸”应用已经停止了#39;当点击打卡时,我的代码就在这里..

// ClockIn functionality
clockin_btn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {       
        clockin_btn.setEnabled(false);
        camera.stopPreview();
        capturePhoto(0);
      // showing one error here in log cat
        previewing = false;         
        clockin_label.setText(String.format(session_msg,logout_seconds));   
        ticker.setBase(SystemClock.elapsedRealtime()); 
        ticker.start();
    }       
});

private String capturePhoto(int clockInOutMode) {

    final int mode = clockInOutMode;
    img_file = String.format("%d.jpg", System.currentTimeMillis());

    Camera.PictureCallback mCall = new Camera.PictureCallback() {               

        public void onPictureTaken(byte[] data, Camera camera) {

            try {    

                Bitmap mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);           
                File file = new File(Constants.EMP_IMG_LOCATION, img_file);           
                FileOutputStream fOut = new FileOutputStream(file);                    
                mBitmap.compress(Bitmap.CompressFormat.JPEG, 80, fOut);               
                fOut.flush();               
                fOut.close();

                if ( mode == 0) {
                    clockIn(img_file);
                } else {
                    clockOut(img_file);
                }

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

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

            } 
        }   

    };  

    Camera.Parameters cameraParams = camera.getParameters();
    List<Camera.Size> sizes = cameraParams.getSupportedPictureSizes();
    Camera.Size result = null;
    for (int i=0;i<sizes.size();i++){
        result = (Size) sizes.get(i);
        Log.i("PictureSize", "Supported Size.Width: " + result.width + "height: " +result.height);         
    }

    cameraParams.setPictureSize(640, 480);
    camera.setParameters(cameraParams);
            System.gc();
    camera.setPreviewCallback(null);
    camera.takePicture(null, null, mCall);

       // showing one error here in logcat

    return img_file;
}

我的Logcat显示为:

03-27 04:52:19.273: E/AndroidRuntime(4105): FATAL EXCEPTION: main
03-27 04:52:19.273: E/AndroidRuntime(4105): java.lang.RuntimeException: setParameters failed
03-27 04:52:19.273: E/AndroidRuntime(4105):     at android.hardware.Camera.native_setParameters(Native Method)
03-27 04:52:19.273: E/AndroidRuntime(4105):     at android.hardware.Camera.setParameters(Camera.java:1452)

和我的android.manifest.xml文件:

     <uses-feature android:name="android.hardware.camera" />
 <uses-feature android:name="android.hardware.camera.autofocus" />
 <used-feature android:name="android.hardware.location" />
 <used-feature android:name="android.hardware.camera.setParameters" />

3 个答案:

答案 0 :(得分:2)

在调用takePicture方法camera.takePicture(null, null, mCall);之前调用startPreview方法,并且我使用的startPreview方法是

private void startPreview() {
        if (cameraConfigured && camera!=null) {
          camera.startPreview();
          inPreview=true;
        }
      }

通过这个我解决了我的问题......这可能会帮助你们。

答案 1 :(得分:0)

它并非在所有情况下都有效。您拨打getSupportedPictureSizes()然后就可以获得列表了。并从列表中选择setPictureSize()中的参数。

答案 2 :(得分:0)

private void flipBackToFrontCamera() {

    if (mCamera != null) {
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }

    mCamera = Camera.open(1);

    if (mCamera != null) {
        try {
            mCamera.setPreviewDisplay(surfaceView.getHolder());
            mCamera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}