我有例外 “无法以意图启动服务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 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)
这是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>
答案 0 :(得分:1)
当我运行类似的代码时遇到同样的错误here。为解决此问题,我在SurfaceTexture st = new SurfaceTexture(MODE_PRIVATE);
之前添加了mCamera.setPreviewTexture(st);
mCamera.startPreview();
。感谢Viren Kheni this answer。
根据您的代码,我建议您添加
SurfaceTexture st = new SurfaceTexture(MODE_PRIVATE);
camera.setPreviewTexture(st);
在camera.startPreview();
方法{。}}之前。