我试着用计数器拍照,当计数器变为0时,它必须拍照,但当计数器为0时,应用程序崩溃,并说功能图片失败,是否有人知道为什么? 。提前谢谢。这是我的代码;
public class CapPhoto extends Activity {
private Camera camera;
private TextView textTimeLeft;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.capphoto);
textTimeLeft=(TextView)findViewById(R.id.textTimeLeft);
SurfaceView view = new SurfaceView(this);
camera = Camera.open();
try {
camera.setPreviewDisplay(view.getHolder()); // feed dummy surface to surface
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
camera.startPreview();
new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
camera.takePicture(null, null, jpegCallBack);
}
}
.start();
}
public void startTimer(View v){
// 5000ms=5s at intervals of 1000ms=1s so that means it lasts 5 seconds
new CountDownTimer(5000,1000){
@Override
public void onFinish() {
// count finished
textTimeLeft.setText("Picture Taken");
camera.takePicture(null, null, null, jpegCallBack);
}
@Override
public void onTick(long millisUntilFinished) {
// every time 1 second passes
textTimeLeft.setText("Seconds Left: "+millisUntilFinished/1000);
}
}.start();
}
Camera.PictureCallback jpegCallBack=new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
// set file destination and file name
File destination=new File(Environment.getExternalStorageDirectory(),"ddddddss.jpg");
try {
Bitmap userImage = BitmapFactory.decodeByteArray(data, 0, data.length);
// set file out stream
FileOutputStream out = new FileOutputStream(destination);
// set compress format quality and stream
userImage.compress(Bitmap.CompressFormat.JPEG, 90, out);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}
答案 0 :(得分:0)
private TextView textTimeLeft;
private Camera camera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textTimeLeft=(TextView)findViewById(R.id.textTimeLeft);
SurfaceView view = new SurfaceView(this);
camera = Camera.open();
try {
camera.setPreviewDisplay(view.getHolder()); // feed dummy surface to surface
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
camera.startPreview();
}
Camera.PictureCallback jpegCallBack=new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
// set file destination and file name
File destination=new File(Environment.getExternalStorageDirectory(),"ddddddss.jpg");
try {
Bitmap userImage = BitmapFactory.decodeByteArray(data, 0, data.length);
// set file out stream
FileOutputStream out = new FileOutputStream(destination);
// set compress format quality and stream
userImage.compress(Bitmap.CompressFormat.JPEG, 90, out);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
public void startTimer(View v){
new CountDownTimer(10000, 1000) {//CountDownTimer(edittext1.getText()+edittext2.getText()) also parse it to long
public void onTick(long millisUntilFinished) {
textTimeLeft.setText("Seconds Left: "+millisUntilFinished/1000);
}
public void onFinish() {
camera.takePicture(null, null, null, jpegCallBack);
}
}
.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
camera.release();
}
}
将您的代码更改为此。
添加以下权限
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
我的activity_main.xml是
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/buttonStartTimer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Timer"
android:onClick="startTimer" />
<TextView
android:id="@+id/textTimeLeft"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout>
答案 1 :(得分:0)
我认为你应该释放相机资源onDestroy() 不是onPause(),一个错误是当你离开应用程序并返回onPause()方法是trigerred和相机被释放然后它给出一个错误,除了一切似乎运行良好