2014-07-11 123 views
2

我收到異常 「無法啓動帶有意圖java.lang.runtimeexception拍照失敗的服務」。
我用我必須使用它的服務擴展我的課程。我不想延長活動。拍照失敗java.lang.runtimeexception無法用意圖啓動服務


這裏是我的簡單的代碼:

public class LaunchActivity extends Service { 


     Camera camera; 
    @Override 
     public int onStartCommand(Intent intent, int flags, int startId) { 
      // TODO Auto-generated method stub 
      try { 
       TakePhoto(); 
      } catch (IOException e2) { 
       // TODO Auto-generated catch block 
       e2.printStackTrace(); 
      } 
    } 
    public void TakePhoto() throws IOException{ 
      //findFrontFacingCamera(); 
      int cameraId = -1; 
      int numberOfCameras = Camera.getNumberOfCameras(); 
      for (int i = 0; i < numberOfCameras; i++) { 
        CameraInfo info = new CameraInfo(); 
        Camera.getCameraInfo(i, info); 
        if (info.facing == CameraInfo.CAMERA_FACING_FRONT) { 
         Log.v("MyActivity", "Camera found"); 
         cameraId = i; 
       break; 
       } 
       } 

       // safeCameraOpen(cameraId); 

       boolean qOpened = false; 
       try { 
        if (camera != null) { 
         camera.stopPreview(); 
         camera.release(); 
         camera = null; 
        }  
        camera = Camera.open(cameraId); 
        qOpened = (camera != null); 
       } catch (Exception e) { 
        Log.e(getString(R.string.app_name), "failed to open Camera"); 
        e.printStackTrace(); 
       } 


       SurfaceView view = new SurfaceView(getApplicationContext()); 
       try { 
         camera.setPreviewDisplay(view.getHolder()); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       camera.startPreview(); 
       Camera.Parameters params = camera.getParameters(); 
       params.setJpegQuality(100); 
       camera.setParameters(params); 

       camera.takePicture(null, null, jpegCallback);  
      } 

// This method are taking photos. 

     PictureCallback jpegCallback = new PictureCallback() { 
      public void onPictureTaken(byte[] data, Camera camera) 
      { 
       FileOutputStream outStream = null; 
       try { 
        String fileName = "Image.jpg"; 
        String backPicPath = Environment.getExternalStorageDirectory().getPath()+ "/FolderName/"+fileName; 

       //String dir_path = "";// set your directory path here 
        outStream = new FileOutputStream(backPicPath);  
        outStream.write(data); 
        outStream.close(); 
        Log.d("asdas", "onPictureTaken - wrote bytes: " + data.length); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } finally 
       { 
        camera.stopPreview(); 
        camera.release(); 
        camera = null; 
        Toast.makeText(getApplicationContext(), "Image snapshot Done",Toast.LENGTH_LONG).show(); 


       } 
       Log.d("asd", "onPictureTaken - jpeg"); 
      } 
      }; 
    } 

這裏是我的logcat

07-11 16:17:15.171: E/AndroidRuntime(15215): FATAL EXCEPTION: main 
07-11 16:17:15.171: E/AndroidRuntime(15215): Process: com.droidappdevs.keepsafe, PID: 15215 
07-11 16:17:15.171: E/AndroidRuntime(15215): java.lang.RuntimeException: Unable to start service [email protected] with Intent { flg=0x10000000 cmp=com.droidappdevs.keepsafe/.LaunchActivity }: java.lang.RuntimeException: takePicture failed 
07-11 16:17:15.171: E/AndroidRuntime(15215): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2746) 
07-11 16:17:15.171: E/AndroidRuntime(15215): at android.app.ActivityThread.access$2100(ActivityThread.java:139) 
07-11 16:17:15.171: E/AndroidRuntime(15215): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307) 
07-11 16:17:15.171: E/AndroidRuntime(15215): at android.os.Handler.dispatchMessage(Handler.java:102) 
07-11 16:17:15.171: E/AndroidRuntime(15215): at android.os.Looper.loop(Looper.java:136) 
07-11 16:17:15.171: E/AndroidRuntime(15215): at android.app.ActivityThread.main(ActivityThread.java:5102) 
07-11 16:17:15.171: E/AndroidRuntime(15215): at java.lang.reflect.Method.invokeNative(Native Method) 
07-11 16:17:15.171: E/AndroidRuntime(15215): at java.lang.reflect.Method.invoke(Method.java:515) 
07-11 16:17:15.171: E/AndroidRuntime(15215): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
07-11 16:17:15.171: E/AndroidRuntime(15215): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
07-11 16:17:15.171: E/AndroidRuntime(15215): at dalvik.system.NativeStart.main(Native Method) 
07-11 16:17:15.171: E/AndroidRuntime(15215): Caused by: java.lang.RuntimeException: takePicture failed 
07-11 16:17:15.171: E/AndroidRuntime(15215): at android.hardware.Camera.native_takePicture(Native Method) 
07-11 16:17:15.171: E/AndroidRuntime(15215): at android.hardware.Camera.takePicture(Camera.java:1265) 
07-11 16:17:15.171: E/AndroidRuntime(15215): at android.hardware.Camera.takePicture(Camera.java:1210) 
07-11 16:17:15.171: E/AndroidRuntime(15215): at com.droidappdevs.keepsafe.LaunchActivity.TakePhoto(LaunchActivity.java:464) 
07-11 16:17:15.171: E/AndroidRuntime(15215): at com.droidappdevs.keepsafe.LaunchActivity.onStartCommand(LaunchActivity.java:267) 
07-11 16:17:15.171: E/AndroidRuntime(15215): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2729) 

這裏是Menifest文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.package.name" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="10" 
     android:targetSdkVersion="19" /> 


    <uses-permission android:name="android.permission.RECORD_AUDIO" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.INTERNET" />" 
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.RECEIVE_SMS"/> 
    <uses-permission android:name="android.permission.READ_SMS"/> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 
    <uses-permission android:name="android.permission.READ_CALL_LOG" /> 
    <uses-permission android:name="android.permission.READ_CONTACTS" /> 
    <uses-permission android:name="android.permission.SEND_SMS" /> 
    <uses-permission android:name="android.permission.CAMERA" /> 
<uses-feature android:name="android.hardware.camera" /> 
<uses-feature android:name="android.hardware.camera.autofocus" /> 
    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.package.name.StartPage" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <service android:name=".LaunchActivity" ></service> 
     <receiver android:enabled="true" 
        android:name=".ReceiverTrigger" 
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED" > 
     <intent-filter android:priority="1000">    
      <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 

      <category android:name="android.intent.category.DEFAULT" />  

     </intent-filter> 
     </receiver> 
    </application> 

</manifest> 
+2

發佈您的logcat。 –

+0

其實錯誤是在camera.takePicture(null,null,jpegCallback);行 –

+2

爲什麼你不在'Activity'中這樣做?你想對你的妻子做間諜嗎? –

回答

1

我得到了同樣的錯誤時,我正在運行一個類似的代碼,我發現here。爲了解決這個問題,我在mCamera.startPreview();之前加了SurfaceTexture st = new SurfaceTexture(MODE_PRIVATE);mCamera.setPreviewTexture(st);。感謝Viren Kheni的this answer

所以按你的代碼,我建議你在TakePhoto()方法camera.startPreview();前加

SurfaceTexture st = new SurfaceTexture(MODE_PRIVATE); 
camera.setPreviewTexture(st); 

+0

經過如此多的研究,是否已經解決了這個問題:p Thankyou –