我使用自定义相机应用程序,但没有打开相机片段和应用程序崩溃。我的代码在下面我的代码错误的我?请帮帮我!!!
TakeCameraFragment :::
public class TakeCameraFragment extends Fragment {
Camera mCamera = null;
private CameraPreview mCameraPreview;
protected static final int MEDIA_TYPE_IMAGE = 0;
static String FilePAth = "";
Button takePicture;
static String base64string = "";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.camerafragment,
container, false);
mCamera = getCameraInstance();
Log.v("log_tag", "mCamera :: "+mCamera);
mCameraPreview = new CameraPreview(getActivity(), mCamera);
FrameLayout preview = (FrameLayout) rootView
.findViewById(R.id.camera_preview_fragment);
preview.addView(mCameraPreview);
takePicture = (Button) rootView
.findViewById(R.id.btnTakePicturefragment);
takePicture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mCamera.takePicture(null, null, mPictureframent);
}
});
return rootView;
}
public boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
releaseCamera();
}
private void releaseCamera() {
if (mCamera != null) {
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
private Camera getCameraInstance() {
try {
Log.v("log_tag", "camera try:::"+mCamera);
mCamera.open();
} catch (Exception e) {
// cannot get camera or does not exist
Log.v("log_tag", "camera catch:::"+mCamera);
releaseCamera();
}
return mCamera;
}
private static File getOutputMediaFile() {
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"MyCameraAppFragment");
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());
FilePAth = mediaStorageDir.getPath() + File.separator + "IMG_fragment_"
+ timeStamp + ".jpg";
Log.v("log", " FilePAth " + FilePAth);
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_fragment_" + timeStamp + ".jpg");
return mediaFile;
}
PictureCallback mPictureframent = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
/*
* Intent returnIntent = new Intent();
* returnIntent.putExtra("data", data); setResult(RESULT_OK,
* returnIntent); finish();
*/
Log.v("log_tag", "data :: " + data);
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
SetPictureImageFragment fm2 = new SetPictureImageFragment();
fragmentTransaction.replace(R.id.relative_camerafragment_id, fm2,
"HELLO");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Bundle bundle = new Bundle();
bundle.putByteArray("position", data);
fm2.setArguments(bundle);
//SetPictureImageFragment fm2 = new SetPictureImageFragment();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
};
/*
* public void onBackPressed() { /*Intent returnIntent = new Intent();
* returnIntent.putExtra("path", FilePAth); setResult(RESULT_OK,
* returnIntent); finish(); };
*/
}
SetPictureImageFragment :::
public class SetPictureImageFragment extends Fragment {
ImageView img;
Bundle bundle;
byte[] path;
Button btnTakecanclePicture;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.capturepicturefragment, null);
Log.v("log_tag","SetPictureImageFragment");
bundle = this.getArguments();
path = bundle.getByteArray("position");
Log.v("log_tag","SetPictureImageFragment ::: Path :: "+path);
img = (ImageView) view.findViewById(R.id.camera_preview_fragment_imageview);
btnTakecanclePicture=(Button)view.findViewById(R.id.btnTakecanclePicture);
//img.setScaleType(ImageView.ScaleType.FIT_XY);
Bitmap b = BitmapFactory.decodeByteArray(path, 0,path.length);
Log.v("log_tag","SetPictureImageFragment ::: bitmap :: "+b);
img.setImageBitmap(b);
img.setScaleType(ImageView.ScaleType.FIT_XY);
/* btnTakecanclePicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
TakeCameraFragment fm2 = new TakeCameraFragment();
fragmentTransaction.replace(R.id.relative_cameraimageview_fragment, fm2,
"HELLO");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
/*FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm
.beginTransaction();
CreateStoreFragment fm2 = new CreateStoreFragment();
fragmentTransaction.replace(R.id.relative_cameraimageview_fragment,
fm2, "HELLO");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
*/
return view;
}
}
相机预览类::
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mSurfaceHolder;
private Camera mCamera;
// Constructor that obtains context and camera
@SuppressWarnings("deprecation")
public CameraPreview(Context context, Camera camera) {
super(context);
this.mCamera = camera;
this.mSurfaceHolder = this.getHolder();
this.mSurfaceHolder.addCallback(this);
this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try {
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
//mCamera.setDisplayOrientation(90);
} catch (IOException e) {
// left blank for now
}
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
mCamera.stopPreview();
mCamera.release();
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
int width, int height) {
// start preview with new settings
try {
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
//mCamera.setDisplayOrientation(90);
} catch (Exception e) {
// intentionally left blank for a test
}
}
}
我得到错误:::
03-26 11:32:48.325: E/AndroidRuntime(28732): FATAL EXCEPTION: main
03-26 11:32:48.325: E/AndroidRuntime(28732): java.lang.NullPointerException
03-26 11:32:48.325: E/AndroidRuntime(28732): at com.buymysari.camera.CameraPreview.surfaceCreated(CameraPreview.java:28)
03-26 11:32:48.325: E/AndroidRuntime(28732): at android.view.SurfaceView.updateWindow(SurfaceView.java:609)
03-26 11:32:48.325: E/AndroidRuntime(28732): at android.view.SurfaceView.access$000(SurfaceView.java:86)
03-26 11:32:48.325: E/AndroidRuntime(28732): at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:178)
03-26 11:32:48.325: E/AndroidRuntime(28732): at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:707)
03-26 11:32:48.325: E/AndroidRuntime(28732): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1981)
03-26 11:32:48.325: E/AndroidRuntime(28732): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1143)
03-26 11:32:48.325: E/AndroidRuntime(28732): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4674)
03-26 11:32:48.325: E/AndroidRuntime(28732): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
03-26 11:32:48.325: E/AndroidRuntime(28732): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
03-26 11:32:48.325: E/AndroidRuntime(28732): at android.view.Choreographer.doFrame(Choreographer.java:525)
03-26 11:32:48.325: E/AndroidRuntime(28732): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
03-26 11:32:48.325: E/AndroidRuntime(28732): at android.os.Handler.handleCallback(Handler.java:615)
03-26 11:32:48.325: E/AndroidRuntime(28732): at android.os.Handler.dispatchMessage(Handler.java:92)
03-26 11:32:48.325: E/AndroidRuntime(28732): at android.os.Looper.loop(Looper.java:137)
03-26 11:32:48.325: E/AndroidRuntime(28732): at android.app.ActivityThread.main(ActivityThread.java:4960)
03-26 11:32:48.325: E/AndroidRuntime(28732): at java.lang.reflect.Method.invokeNative(Native Method)
03-26 11:32:48.325: E/AndroidRuntime(28732): at java.lang.reflect.Method.invoke(Method.java:511)
03-26 11:32:48.325: E/AndroidRuntime(28732): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
03-26 11:32:48.325: E/AndroidRuntime(28732): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
03-26 11:32:48.325: E/AndroidRuntime(28732): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您的mCamera
对象为null
..您需要更改此行
Camera.open();
到
mCamera = Camera.open();
像这样改变..
private Camera getCameraInstance() {
try {
Log.v("log_tag", "camera try:::"+mCamera);
mCamera = Camera.open();
} catch (Exception e) {
// cannot get camera or does not exist
Log.v("log_tag", "camera catch:::"+mCamera);
releaseCamera();
}
return mCamera;
}