2013-05-20 3 views
0

나는 응용 프로그램 코드를 이해하고 장치에서 실행하기 시작했습니다. 그러나 코드를 실행하고 버튼을 클릭하면 카메라가 불행하다고 말합니다.이 문제를 해결하는 방법은 무엇입니까? 이이 내 CameraPreview.java카메라 응용 프로그램을 실행하는 동안 오류가 발생했습니다.

package piyushs.camera; 
import android.content.Context; 
import android.hardware.Camera; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback 
{ 

    private SurfaceHolder mHolder; 
    private Camera mCamera; 

    public CameraPreview(Context context,Camera camera) 
    { 
     super(context); 
     mCamera = camera; 

     /*SurfaceView view = new SurfaceView(this); 
     c.setPreviewDisplay(view.getHolder()); 
     c.startPreview(); 
     c.takePicture(shutterCallback, rawPictureCallback, jpegPictureCallback); 
     * */ 

     // 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 surfaceChanged(SurfaceHolder holder, int format, int width,int height) 
    { 
     // TODO Auto-generated method stub 
     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) 
      { 
      } 
    } 
    public void surfaceCreated(SurfaceHolder holder) 
    { 
     // TODO Auto-generated method stub 
    } 
    public void surfaceDestroyed(SurfaceHolder holder) 
    { 
     // TODO Auto-generated method stub 
    } 
} 

내 PhotoIntentActivity.java

package piyushs.camera; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import android.app.Activity; 
import android.hardware.Camera; 
import android.hardware.Camera.PictureCallback; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.view.View; 
import android.widget.Button; 
import android.widget.FrameLayout; 

public class PhotoIntentActivity extends Activity 
{ 
    private Camera mCamera; 
    private CameraPreview mPreview; 
    public static final int MEDIA_TYPE_IMAGE = 1; 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_photo_intent); 
     Button captureButton = (Button) findViewById(R.id.button_capture); 
     System.out.println("Starting!"); 
     // 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); 
     final PictureCallback mPicture = new PictureCallback() 
     { 
      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(); 
        MediaStore.Images.Media.insertImage(getContentResolver(), pictureFile.getAbsolutePath(), pictureFile.getName(), pictureFile.getName()); 
       } catch (FileNotFoundException e) 
       { 
       } catch (IOException e) 
       { 
       } 
      } 
     }; 
     // Add a listener to the Capture button 
     captureButton.setOnClickListener 
     (
      new View.OnClickListener() 
      { 
       public void onClick(View v) 
       { 
        // get an image from the camera 
        System.out.println("Photo Taking!"); 
        mCamera.takePicture(null, null, mPicture); 
       } 
      } 
     ); 
    }// end of oncreate() 

    /** A safe way to get an instance of the Camera object. */ 
    public static Camera getCameraInstance() 
    { 
     Camera c = null; 
     try 
     { 
      c = Camera.open(); // attempt to get a Camera instance 
     } 
     catch (Exception e) 
     { 
      // Camera is not available (in use or does not exist) 
     } 
     return c; // returns null if camera is unavailable 
    } 
    @Override 
    protected void onPause() 
    { 
     super.onPause(); 
     releaseCamera();    // release the camera immediately on pause event 
    } 
    private void releaseCamera() 
    { 
     if (mCamera != null) 
     { 
      mCamera.release();  // release the camera for other applications 
      mCamera = null; 
     } 
    } 
    /** 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()) 
      { 
       return null; 
      } 
     } 
     // 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; 
    } 
} 

입니다 이것은 내가 Building my own camera application + Automatically capture the image

에서 대부분의 코드를 복사 한 내 activity_photo_intent.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <FrameLayout 
    android:id="@+id/camera_preview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    /> 
    <Button 
    android:id="@+id/button_capture" 
    android:text="Capture" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    /> 

</LinearLayout> 

입니다

답변

1

매니페스트에 필요한 permi를 추가하십시오. ssions 및 기능 :

<uses-permission android:name="android.permission.CAMERA" /> 
<uses-feature android:name="android.hardware.camera" /> 
<uses-feature android:name="android.hardware.camera.autofocus" /> 
+0

이 필요 추가 권한 및 기능. 나는 그림을 찍을 수있다. 그러나 나는이 사진들이 어디에 저장되어 있는지 알지 못한다. – user19390

0

확인 코드 :

파일은 MyCameraApp 폴더 아래에 SD 카드의 사진 폴더에 저장지고
 File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
     Environment.DIRECTORY_PICTURES), "MyCameraApp"); 

이 예 :/SDCARD/사진/MyCameraApp

관련 문제