拍照失败 java.lang.runtimeexception 无法有意图地启动服务

Posted

技术标签:

【中文标题】拍照失败 java.lang.runtimeexception 无法有意图地启动服务【英文标题】:Take picture failed java.lang.runtimeexception unable to start service with intent 【发布时间】:2014-09-02 00:34:53 【问题描述】:

我有异常 “无法使用意图启动服务 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");
            
            ;
    

这是我的日志

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 com.droidappdevs.keepsafe.LaunchActivity@4203a060 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)

这是清单文件

<?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>

【问题讨论】:

其实报错在camera.takePicture(null, null, jpegCallback);行 您为什么不在Activity 中这样做?你想监视你的妻子吗? 因为我在这个类中使用了几个从服务扩展而来的方法 我不认为在你的代码中使用它们,在你的活动中做同样的事情 我用过,没有添加服务代码 【参考方案1】:

当我运行找到here 的类似代码时,我遇到了同样的错误。为了解决这个问题,我在mCamera.startPreview();之前添加了SurfaceTexture st = new SurfaceTexture(MODE_PRIVATE);mCamera.setPreviewTexture(st);。感谢 Viren Kheni this answer。

所以根据你的代码,我建议你添加

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

TakePhoto() 方法中的camera.startPreview(); 之前。

【讨论】:

以上是关于拍照失败 java.lang.runtimeexception 无法有意图地启动服务的主要内容,如果未能解决你的问题,请参考以下文章

Android:来自相机和图库运行时异常的图片

Android ProgressDialog 不能与 AsyncTask 一起使用

如何将数据从 Spark SQL 导出到 CSV

Ionic/Cordova 应用程序找不到 MainActivity

troubshooting-sqoop 导出 TiDB表数据报com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communicatio

拍照失败并显示“需要 android:camera”