我试图将我的相机应用程序构建为我项目的一部分 我按照文档,它需要一张照片,存储它和Just Freeze,我需要能够捕获更多的照片
我尝试在camera.startPreview();
之后添加mCamera.takePicture(null, null, mPicture);
,但这会导致异常
这是我的活动
public class MainActivity extends ActionBarActivity {
public FrameLayout preview ;
public Button Shutter_btn;
private Camera mCamera;
CameraPreview mPreview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create an instance of Camera
mCamera = getCameraInstance();
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
Shutter_btn = (Button) findViewById(R.id.button3);
Shutter_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TakePhoto();
}
});
}
public void TakePhoto()
{
PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null){
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d("TAG", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("TAG", "Error accessing file: " + e.getMessage());
}
}
};
mCamera.takePicture(null, null, mPicture);
}
public void CameraRelease()
{
if(mCamera!=null){
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
}
}
private void galleryAddPic(String ImagePath) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(ImagePath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
public final int MEDIA_TYPE_IMAGE = 1;
/** Create a file Uri for saving an image or video */
private Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
/** Create a File for saving an image or video */
private File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
Toast.makeText(MainActivity.this, "Dirrectory not been made " +mediaStorageDir , Toast.LENGTH_LONG).show();
return null;
}
}
Toast.makeText(MainActivity.this, "Dirrectory not been made " +mediaStorageDir , Toast.LENGTH_LONG).show();
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
}
else {
return null;
}
return mediaFile;
}
/** A safe way to get an instance of the Camera object. */
public Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
Toast.makeText(MainActivity.this, "No Camera", Toast.LENGTH_SHORT).show();// no camera on this device
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
}
CameraPreview类:
package ahmed.Labib.mycamera;
import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
public Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d("TAG", "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d("TAG", "Error starting camera preview: " + e.getMessage());
}
}
}
logcat的
03-02 13:22:52.345: D/Camera-JNI(21812): (tid:21812)[MtkJNICameraContext] this:0x5bd7e628 camera->getStrongCount(3)
03-02 13:22:52.346: D/SurfaceView(21812): checkGLSurfaceViewlLogProperty get invalid command
03-02 13:22:52.352: D/ActivityThread(21812): ACT-AM_ON_RESUME_CALLED ActivityRecord{423774e8 token=android.os.BinderProxy@42376cc0 {ahmed.Labib.mycamera/ahmed.Labib.mycamera.MainActivity}}
03-02 13:22:52.352: V/PhoneWindow(21812): DecorView setVisiblity: visibility = 4 ,Parent =null, this =com.android.internal.policy.impl.PhoneWindow$DecorView{42386b00 I.E..... R.....ID 0,0-0,0}
03-02 13:22:52.361: V/PhoneWindow(21812): DecorView setVisiblity: visibility = 0 ,Parent =ViewRoot{423f0800 ahmed.Labib.mycamera/ahmed.Labib.mycamera.MainActivity,ident = 0}, this =com.android.internal.policy.impl.PhoneWindow$DecorView{42386b00 V.E..... R.....ID 0,0-0,0}
03-02 13:22:52.361: D/ActivityThread(21812): ACT-LAUNCH_ACTIVITY handled : 0 / ActivityRecord{423774e8 token=android.os.BinderProxy@42376cc0 {ahmed.Labib.mycamera/ahmed.Labib.mycamera.MainActivity}}
03-02 13:22:52.367: I/SurfaceView(21812): updateWindow -- onWindowVisibilityChanged, visibility = 0
03-02 13:22:52.408: E/(21812): appName=ahmed.Labib.mycamera, acAppName=/system/bin/surfaceflinger
03-02 13:22:52.408: E/(21812): 0
03-02 13:22:52.408: E/(21812): appName=ahmed.Labib.mycamera, acAppName=/system/bin/surfaceflinger
03-02 13:22:52.408: E/(21812): 0
03-02 13:22:52.412: D/GraphicBuffer(21812): create handle(0x603e5080) (w:1280, h:736, f:1)
03-02 13:22:52.414: I/MaliEGL(21812): [Mali]window_type=1, is_framebuffer=0, errnum = 0
03-02 13:22:52.414: I/MaliEGL(21812): [Mali]surface->num_buffers=4, surface->num_frames=3, win_min_undequeued=1
03-02 13:22:52.414: I/MaliEGL(21812): [Mali]max_allowed_dequeued_buffers=3
03-02 13:22:52.415: D/GraphicBuffer(21812): close handle(0x603e5080) (w:1280 h:736 f:1)
03-02 13:22:52.419: D/GraphicBuffer(21812): create handle(0x603e5e80) (w:736, h:1280, f:1)
03-02 13:22:52.421: D/OpenGLRenderer(21812): Enabling debug mode 0
03-02 13:22:52.423: D/GraphicBuffer(21812): create handle(0x608c0730) (w:768, h:768, f:1)
03-02 13:22:52.424: I/[MALI][Gralloc](21812): dlopen libsec_mem.so fail
03-02 13:22:52.425: D/OpenGLRenderer(21812): setViewport 1280x736 <0x608be490>
03-02 13:22:52.425: I/SurfaceView(21812): updateWindow -- setFrame
03-02 13:22:52.427: I/SurfaceView(21812): updateWindow -- OnPreDrawListener, mHaveFrame = true
03-02 13:22:52.428: I/SurfaceView(21812): Changes: creating=true format=true size=true visible=true left=true top=true mUpdateWindowNeeded=false mReportDrawNeeded=false redrawNeeded=false forceSizeChanged=true mVisible=false mRequestedVisible=true
03-02 13:22:52.432: I/SurfaceView(21812): Cur surface: Surface(name=null)/@0x423eb128
03-02 13:22:52.439: V/SurfaceView(21812): ahmed.Labib.mycamera.CameraPreview{423eae80 V.E..... ......ID 0,0-1110,576} got resized: w=1110 h=576, cur w=-1 h=-1
03-02 13:22:52.441: I/SurfaceView(21812): New surface: Surface(name=null)/@0x423eb1f8, vis=true, frame=Rect(85, 139 - 1195, 715)
03-02 13:22:52.442: I/SurfaceView(21812): Callback --> surfaceCreated
03-02 13:22:52.442: I/SurfaceView(21812): surfaceCreated callback +
03-02 13:22:53.146: I/SurfaceView(21812): surfaceCreated callback -
03-02 13:22:53.146: I/SurfaceView(21812): surfaceChanged -- format=4 w=1110 h=576
03-02 13:22:53.146: I/SurfaceView(21812): surfaceChanged callback +
03-02 13:22:55.035: I/SurfaceView(21812): surfaceChanged callback -
03-02 13:22:55.035: I/SurfaceView(21812): surfaceRedrawNeeded
03-02 13:22:55.035: I/SurfaceView(21812): finishedDrawing
03-02 13:22:55.040: V/SurfaceView(21812): Layout: x=85 y=139 w=1110 h=576, frame=Rect(0, 0 - 1110, 576)
03-02 13:22:55.041: I/Choreographer(21812): Skipped 165 frames! The application may be doing too much work on its main thread.
03-02 13:22:55.046: I/SurfaceView(21812): updateWindow -- OnPreDrawListener, mHaveFrame = true
03-02 13:22:55.047: I/SurfaceView(21812): Changes: creating=false format=false size=false visible=false left=false top=false mUpdateWindowNeeded=true mReportDrawNeeded=true redrawNeeded=false forceSizeChanged=false mVisible=true mRequestedVisible=true
03-02 13:22:55.047: I/SurfaceView(21812): Cur surface: Surface(name=null)/@0x423eb128
03-02 13:22:55.052: I/SurfaceView(21812): New surface: Surface(name=null)/@0x423eb1f8, vis=true, frame=Rect(85, 139 - 1195, 715)
03-02 13:22:55.052: I/SurfaceView(21812): surfaceRedrawNeeded
03-02 13:22:55.053: I/SurfaceView(21812): finishedDrawing
03-02 13:22:55.054: V/SurfaceView(21812): Layout: x=85 y=139 w=1110 h=576, frame=Rect(0, 0 - 1110, 576)
03-02 13:22:55.069: D/OpenGLRenderer(21812): prepareDirty (0.00, 0.00, 1280.00, 736.00) opaque 1 <0x608be490>
03-02 13:22:55.083: D/OpenGLRenderer(21812): finish <0x608be490>
03-02 13:22:55.127: V/InputMethodManager(21812): onWindowFocus: null softInputMode=32 first=true flags=#1810100
03-02 13:22:55.128: V/InputMethodManager(21812): START INPUT: com.android.internal.policy.impl.PhoneWindow$DecorView{42386b00 V.E..... R.....ID 0,0-1280,736} ic=null tba=android.view.inputmethod.EditorInfo@424143c8 controlFlags=#104
03-02 13:22:55.138: I/SurfaceView(21812): updateWindow -- UPDATE_WINDOW_MSG
03-02 13:22:55.152: I/SurfaceView(21812): updateWindow -- setFrame
03-02 13:22:55.154: I/SurfaceView(21812): updateWindow -- OnPreDrawListener, mHaveFrame = true
03-02 13:22:55.159: D/GraphicBuffer(21812): create handle(0x60ca4738) (w:736, h:1280, f:1)
03-02 13:22:55.164: D/OpenGLRenderer(21812): prepareDirty (0.00, 0.00, 1280.00, 736.00) opaque 1 <0x608be490>
03-02 13:22:55.166: D/OpenGLRenderer(21812): finish <0x608be490>
03-02 13:23:16.506: I/View(21812): Touch down dispatch to android.widget.Button{423b7070 VFED..C. ........ 1088,255-1195,362 #7f090042 app:id/button3}, event = MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=37.12097, y[0]=55.464417, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=82397060, downTime=82397060, deviceId=2, source=0x1002 }
03-02 13:23:16.582: I/View(21812): Touch up dispatch to android.widget.Button{423b7070 VFED..C. ...P.... 1088,255-1195,362 #7f090042 app:id/button3}, event = MotionEvent { action=ACTION_UP, id[0]=0, x[0]=37.12097, y[0]=55.464417, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=82397141, downTime=82397060, deviceId=2, source=0x1002 }
03-02 13:23:16.583: V/Provider/Settings(21812): from db cache, name = sound_effects_enabled , value = 0
03-02 13:23:17.656: I/Choreographer(21812): Skipped 66 frames! The application may be doing too much work on its main thread.
03-02 13:23:18.486: D/Camera-JNI(21812): Allocating callback buffer
03-02 13:23:18.493: I/CameraFramework(21812): handleMessage: 256
03-02 13:23:18.569: D/GraphicBuffer(21812): create handle(0x60c9d8e0) (w:640, h:59, f:1)
03-02 13:23:22.006: D/GraphicBuffer(21812): close handle(0x60c9d8e0) (w:640 h:59 f:1)
我需要帮助才能知道错误
答案 0 :(得分:0)
我想出了什么问题
我必须在mCamera.startPreview();
之后添加onPictureTaken()
,而不是在
mCamera.takePicture(null, null, mPicture);
更新onPictureTaken()
:
PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null){
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
galleryAddPic(pictureFile.getPath());
} catch (FileNotFoundException e) {
Log.d("TAG", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("TAG", "Error accessing file: " + e.getMessage());
}
finally
{
mCamera.startPreview();
}
}
};