我正在尝试实施一个应用程序,在用户按下按钮后每5秒继续拍照。这是我的完整代码:
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.os.CountDownTimer;
import android.os.Environment;
import android.view.SurfaceView;
import android.view.View;
import android.widget.TextView;
public class MyAutoCamera extends Activity {
private Camera camera; // camera object
private TextView textTimeLeft; // time left field
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textTimeLeft=(TextView)findViewById(R.id.textTimeLeft); //make time left object
camera = Camera.open();
SurfaceView view = new SurfaceView(this);
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
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
path += "/AutoPictures/myPicture.jpg";//***
File file = new File(path);//***
//File destination=new File(Environment.getExternalStorageDirectory(),"myPicture.jpg");
try {
Bitmap userImage = BitmapFactory.decodeByteArray(data, 0, data.length);
// set file out stream
FileOutputStream out = new FileOutputStream(file);//**
//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){
// 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();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_auto_camera, menu);
return true;
}
}
main.xml中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MyAutoCamera" >
<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>
和权限:
<uses-feature android:name="android.hardware.camera" />
<uses-feature
android:name="android.hardware.camera.front"
android:required="false" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
我的问题是:
非常感谢任何帮助。
答案 0 :(得分:0)
将文件添加到文件系统后,为了使其在连接到存储时可见,请运行以下代码:
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(exportFile);
intent.setData(uri);
ctx.sendBroadcast(intent);
至于继续拍照,还有很多方法可以做,Handler,Timer,ScheduledThreadPoolExecutor,AsyncTask等...... http://developer.android.com/reference/java/util/Timer.html http://developer.android.com/reference/android/os/AsyncTask.html
你应该生成文件名(基于时间?),这样他们就不会超过写作。