I'm trying to take a picture without showing the user anything (no view) through a service. This question has been asked several times before and I've gone through all I could find. Some similar questions:
Most of the questions link to other questions without providing a new solution.
I believe this is the best way to solve this problem: https://stackoverflow.com/a/10268650/3860594 Unfortunately the person has not provided a complete answer and I'm having trouble reproducing his method.
What I'm trying to do is create a SurfaceView inside a SurfaceHolder. I will then use WindowManager with the SurfaceView to create a floating window of sorts that is completely transparent so that it's hidden from the user. Please correct me if I'm wrong.
Here is my code:
SurfaceView mview = new SurfaceView(this);
SurfaceHolder mholder = new SurfaceHolder() {
@Override
public void addCallback(Callback callback) {
}
@Override
public void removeCallback(Callback callback) {
}
@Override
public boolean isCreating() {
return false;
}
@Override
public void setType(int type) {
}
@Override
public void setFixedSize(int width, int height) {
}
@Override
public void setSizeFromLayout() {
}
@Override
public void setFormat(int format) {
}
@Override
public void setKeepScreenOn(boolean screenOn) {
}
@Override
public Canvas lockCanvas() {
return null;
}
@Override
public Canvas lockCanvas(Rect dirty) {
return null;
}
@Override
public void unlockCanvasAndPost(Canvas canvas) {
}
@Override
public Rect getSurfaceFrame() {
return null;
}
@Override
public Surface getSurface() {
return null;
}
};
WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
wm.addView(mview, params);
mview.setZOrderOnTop(true);
mholder.setFormat(PixelFormat.TRANSPARENT);
try {
camera.setPreviewDisplay(mview.getHolder());
camera.startPreview();
camera.takePicture(null,null,photoCallback);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
None of this works as the usual message RuntimeException: takePicture failed
is shown. Any help would be awesome, thanks.