[英]Capture image without preview in Android
I have this code, but it gives me this: 我有这段代码,但是它给了我这个:
Unknown URI: content://media/external/images/media
NOTE: I DO have permissions for Camera / Write. 注意:我确实具有“相机/写入”权限。
This is my code: 这是我的代码:
public class MainActivity extends Activity {
// ...
Camera.PictureCallback photoCallback=new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,
new ContentValues());
OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriTarget);
imageFileOS.write(data);
imageFileOS.flush();
imageFileOS.close();
Toast.makeText(getApplicationContext(),
"Image saved: " + uriTarget.toString(),
Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finish();
}
};
// ...
}
Any suggestions? 有什么建议么?
// Here is the example for surface view.
public void takePictureNoPreview(Context context) { // open back facing camera by default
Camera myCamera=Camera.open();
if(myCamera!=null) {
try { // set camera parameters if you want to
// here, the unused surface view and holder
SurfaceView dummy=new SurfaceView(context);
myCamera.setPreviewDisplay(dummy.getHolder());
myCamera.startPreview();
myCamera.takePicture(null, null, getJpegCallback());
} finally {
myCamera.close();
}
} else {
// booo, failed!
}
}
private PictureCallback getJpegCallback() {
PictureCallback jpeg = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream fos;
try {
fos = new FileOutputStream("test.jpeg");
fos.write(data); fos.close();
} catch (IOException e) {
//do something about it
}
}
};
return jpeg;
}
// Selecting front facing camera.
private Camera openFrontFacingCameraGingerbread() {
int cameraCount = 0; Camera cam = null;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
Camera.getCameraInfo( camIdx, cameraInfo );
if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT ) {
try {
cam = Camera.open( camIdx );
} catch (RuntimeException e) {
Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
}
}
}
return cam;
}
// This should work...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.