gpt4 book ai didi

java - 拍照失败 java.lang.runtimeexception 无法以 Intent 启动服务

转载 作者:行者123 更新时间:2023-11-30 11:16:47 25 4
gpt4 key购买 nike

我有异常“无法启动服务, Intent java.lang.runtimeexception 拍照失败”。
我用我必须使用的服务扩展了我的类(class)。我不想扩展 Activity 。


这是我的简单代码:

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)

这是 list 文件

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

最佳答案

当我运行我发现的类似代码时遇到了同样的错误 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);

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

关于java - 拍照失败 java.lang.runtimeexception 无法以 Intent 启动服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24696801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com