android - 我正在尝试在 android 中拍摄连续照片

标签 android android-intent

在这里,我尝试使用单击按钮和服务在后台拍摄连续照片。我可以连续拍照。我以这样的方式编写代码,它必须在我单击按钮后才能工作,但每当我尝试关闭应用程序时,服务都会自行启动。请帮助我解决错误。

服务类:

public class RecorderService extends Service implements     
SurfaceHolder.Callback {
private WindowManager windowManager;
private SurfaceView surfaceView;
private Camera camera = null;

@Override
public void onCreate() {
    showMessage("Entered");
    // Create new SurfaceView, set its size to 1x1, move it to the top left 
corner and set this service as a callback
    windowManager = (WindowManager) 
this.getSystemService(Context.WINDOW_SERVICE);
    surfaceView = new SurfaceView(this);
    WindowManager.LayoutParams layoutParams = new    
WindowManager.LayoutParams(
            1, 1,
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
            PixelFormat.TRANSLUCENT
    );
    layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
    windowManager.addView(surfaceView, layoutParams);
    surfaceView.getHolder().addCallback(this);
    showMessage("Exited Oncreate");

}

// Method called right after Surface created (initializing and starting 
MediaRecorder)
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    try {
        camera = Camera.open();
        showMessage("Opened camera");
        try {
            camera.setPreviewDisplay(surfaceHolder);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        camera.startPreview();
        showMessage("Started preview");
        new CountDownTimer(25000, 5000) {
            @Override
            public void onFinish() {
                camera.lock();
                camera.release();
                windowManager.removeView(surfaceView);
                showMessage("Finished preview");
            }
            @Override
            public void onTick(long millisUntilFinished) {
                camera.takePicture(null, null, new Camera.PictureCallback()   
 {
                    @Override
                    public void onPictureTaken(byte[] data, Camera camera) {
                        File pictureFile = getOutputMediaFile();
                        if (pictureFile == null) {
                            showMessage("No picture");
                            return;
                        }
                        try {
                            showMessage("Picture present");
                            FileOutputStream fos = new 
FileOutputStream(pictureFile);
                            fos.write(data);
                            showMessage("writing Done");
                        } catch (FileNotFoundException e) {
                        } catch (IOException e) {
                        }
                        showMessage("Took picture");
                        Toast.makeText(getApplicationContext(), "Picture  
Captured",
                                Toast.LENGTH_SHORT).show();
            }
        });
    }
    }.start();
}
    catch (Exception e) {
    if (camera != null)
        camera.release();
    throw new RuntimeException(e);
    }
}
    private File getOutputMediaFile() {
            File mediaStorageDir = new 

 File(Environment.getExternalStoragePublicDirectory
 (Environment.DIRECTORY_PICTURES),
                    "MyCameraApp");
            showMessage("Picture present");
            if (!mediaStorageDir.exists()) {
                if (!mediaStorageDir.mkdirs()) {
                    Log.d("MyCameraApp", "failed to create directory");
                    return null;
                }
            }
            // Create a media file name
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                    .format(new Date());
            File mediaFile;
            mediaFile = new File(mediaStorageDir.getPath() + File.separator
                    + "IMG_" + timeStamp + ".jpg");

            return mediaFile;
        }
private static void showMessage(String message) {
    Log.i("Camera", message);
}

// Stop recording and remove SurfaceView
@Override
public void onDestroy() {
    showMessage("Entered OnDestroy");
    camera.lock();
    camera.release();
    windowManager.removeView(surfaceView);
    showMessage("Exited OnDestroy");
}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int 
width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}
}

启动按钮类:

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(),   
RecorderService.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startService(intent);
        }
    });

Android list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xxxx.xxxx" >

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".PhotoCapture"
        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=".RecorderService"/>
    <receiver android:name="com.example.xxxx.xxxx">
        <intent-filter>
            <action android:name="android.intent.action.SCREEN_OFF">
 </action>
            <action android:name="android.intent.action.SCREEN_ON"></action>
            <action  
  android:name="android.intent.action.ACTION_POWER_CONNECTED"></action>
            <action 
  android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action>
            <action android:name="android.intent.action.ACTION_SHUTDOWN">
  </action>
        </intent-filter>
    </receiver>
</application>



</manifest>

最佳答案

添加停止服务

stopService(new Intent(xxxx.this, service.class));

关于android - 我正在尝试在 android 中拍摄连续照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32494728/

相关文章:

android - 如何使用 Espresso 2.2 在 Android 中测试查看寻呼机滑动手势

java - 所有 "com.android.camera. - - -" Intent 过滤器操作都是在哪里定义的?

android - 未收到通知的 PendingIntent

android - 如何在Android中计算包含具有特定高度的TextView的行高?

Android Studio - Google map 在真正的 Android 设备上发布 apk 时仍为空白

安卓 : In Lollipop sms intent not return to app

java - 如何通过 Intent 共享多个文件?

android - GPS 状态变化的 Action

Android ArrayAdapter getItem() 类型不兼容?

Android SDK 设置代理与 ubuntu