我查了所有相关的帖子,没有找到答案。 我试图用相机库拍照,但拍照方法失败了。 代码假设要做的是按下按钮在ImageView上显示图像(这听起来很简单)。 该应用程序在拍摄时会崩溃,最后会附加堆栈跟踪。
MainActivity.java
package com.drivesafe.drivesafe;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.*;
import android.view.*;
import android.widget.*;
import android.hardware.Camera;
import java.util.Timer;
public class MainActivity extends AppCompatActivity {
public static ProgressDialog detectionProgressDialog;
public static ImageView imageView;
public static Camera camera;
public static Camera.PictureCallback pictureCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.imageView = (ImageView)findViewById(R.id.imageView1);
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
take_picture();
}
});
this.detectionProgressDialog = new ProgressDialog(this);
this.pictureCallback = new PhotoHandler(getApplicationContext());
this.initFrontCamera();
}
private void take_picture() {
camera.startPreview();
camera.takePicture(null, null, null, this.pictureCallback);
}
private void initFrontCamera() {
camera = Camera.open(1); //front camera
}
}
PhotoHandler.java
package com.drivesafe.drivesafe;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.AsyncTask;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;
public class PhotoHandler implements PictureCallback {
private final Context context;
public PhotoHandler(Context context) {
this.context = context;
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
MainActivity.imageView.setImageBitmap(bitmap);
}
}
堆栈追踪:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.drivesafe.drivesafe, PID: 28460
java.lang.RuntimeException: takePicture failed
at android.hardware.Camera.native_takePicture(Native Method)
at android.hardware.Camera.takePicture(Camera.java:1545)
at com.drivesafe.drivesafe.MainActivity.take_picture(MainActivity.java:43)
at com.drivesafe.drivesafe.MainActivity.access$000(MainActivity.java:13)
at com.drivesafe.drivesafe.MainActivity$1.onClick(MainActivity.java:29)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22433)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
Application terminated.
权限
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera"
android:required="true" />
我希望得到一些帮助。