Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法在其他活动中获取从自定义相机捕获的图像_Android_Camera_Nullpointerexception_Android Sdcard - Fatal编程技术网

Android 无法在其他活动中获取从自定义相机捕获的图像

Android 无法在其他活动中获取从自定义相机捕获的图像,android,camera,nullpointerexception,android-sdcard,Android,Camera,Nullpointerexception,Android Sdcard,我的应用程序中有一个自定义相机,我用它拍摄照片,并将其保存在SD卡文件夹中。我能够捕获图像,但我需要在其他活动中获取图像。我在onActivityResult事件中获得传递结果失败和NullPointer异常。我知道图像正在保存,因为我可以看到图像的路径,但是图像本身在经过一段随机时间后显示在文件夹中。我不确定这是否是NullPointerException的原因。我一直在尝试StackOverflow的各种解决方案,但没有解决这个问题。请尽快有人来帮我 以下是我的代码片段: CustomCam

我的应用程序中有一个自定义相机,我用它拍摄照片,并将其保存在SD卡文件夹中。我能够捕获图像,但我需要在其他活动中获取图像。我在onActivityResult事件中获得传递结果失败和NullPointer异常。我知道图像正在保存,因为我可以看到图像的路径,但是图像本身在经过一段随机时间后显示在文件夹中。我不确定这是否是NullPointerException的原因。我一直在尝试StackOverflow的各种解决方案,但没有解决这个问题。请尽快有人来帮我

以下是我的代码片段: CustomCameraActivity.java文件:

public class CustomCameraActivity extends Activity {
private static final String TAG = "CameraActivity";
CameraPreview camPreview;
Button btn_take_pic, btn_use_pic;
Camera mCamera;
String fileName;
Activity act;
Context ctx;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ctx = this;
    act = this;
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_custom_camera);

    camPreview = new CameraPreview(this,
            (SurfaceView) findViewById(R.id.surfaceView));
    camPreview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT));
    ((FrameLayout) findViewById(R.id.preview)).addView(camPreview);
    camPreview.setKeepScreenOn(true);
    addListenerForButton();

}
private void addListenerForButton() {

    btn_take_pic = (Button) findViewById(R.id.btn_take_pic);
    btn_take_pic.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.e(TAG, "Before taking picture");
            mCamera.stopPreview();
            mCamera.startPreview();
            mCamera.takePicture(null, null, jpegCallback);
            Log.e(TAG, "After taking picture");
            btn_take_pic.setText("Retake");
            // mCamera.startPreview();

        }
    });
btn_use_pic = (Button) findViewById(R.id.btn_usePhoto);
    btn_use_pic.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            File file = getOutputMediaFile();
            Log.e("use_pic buttn ", file.getPath());
            Intent intent = new Intent();
            intent.putExtra("output", file.getPath());
            finish();

        }
    });
}
...
PictureCallback jpegCallback = new PictureCallback() {

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        Log.e(TAG, "Reached jpegcallback");
        File pictureFile = getOutputMediaFile();
        if (pictureFile == null) {
            Log.e("blah3", "picture file was null!");
            return;
        }
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        } catch (FileNotFoundException e) {
            Log.e(TAG, "picture file not found!");
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
        }
        Log.e(TAG, "hooray " + pictureFile.toURI().getRawPath());
    }

};
private static File getOutputMediaFile() {
    Log.e(TAG, "Get me the o/p file");
    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            "PostcareApp");
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.e("PostcareApp", "failed to create directory");
            return null;
        }
    }
    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
            .format(new Date());
    File mediaFile;
    mediaFile = new File(mediaStorageDir.getPath() + File.separator
            + "IMG_" + timeStamp + ".jpg");
    Log.e(TAG, "Before returning " + mediaFile.getAbsolutePath());

    return mediaFile;
}
需要图像的活动:

public class HowItWorksActivity extends Activity {

ImageButton btn_Account, btn_Photo, btn_Edit, btn_Flip, btn_Post;
RelativeLayout rl;
private static final int CAMERA_PIC_REQUEST = 2500;
private static final int SELECT_PICTURE = 1;
Bitmap postcard_image;
Uri uri_image;
...
private void takepicture() {
 Intent cameraIntent = new Intent(HowItWorksActivity.this,CustomCameraActivity.class);
 startActivityForResult(cameraIntent,CAMERA_PIC_REQUEST);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    Log.e("Howitworks..", "in activityresult");
    String imgfile = (String) getIntent().getExtras().get("output");
    Log.e("image ", imgfile);
if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {
postcard_image = BitmapFactory.decodeFile(imgfile);
        postcard_image = Bitmap.createScaledBitmap(postcard_image,
                metrics.widthPixels, 600, true);
        imgview.setImageBitmap(postcard_image);
 }
}
以下是LogCat文件:

12-06 12:27:40.779: E/CameraActivity(16607): Get me the o/p file
12-06 12:27:40.779: E/CameraActivity(16607): Before returning /mnt/sdcard/Pictures/PostcareApp/IMG_20131206_122740.jpg
12-06 12:27:40.779: E/use_pic buttn(16607): /mnt/sdcard/Pictures/PostcareApp/IMG_20131206_122740.jpg
12-06 12:27:41.309: E/Howitworks..(16607): in activityresult
12-06 12:27:41.499: E/AndroidRuntime(16607): FATAL EXCEPTION: main
12-06 12:27:41.499: E/AndroidRuntime(16607): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2500, result=0, data=null} to activity {com.sample.postcare2/com.sample.postcare2.HowItWorksActivity}: java.lang.NullPointerException
12-06 12:27:41.499: E/AndroidRuntime(16607):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3387)
12-06 12:27:41.499: E/AndroidRuntime(16607):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3437)
12-06 12:27:41.499: E/AndroidRuntime(16607):    at android.app.ActivityThread.access$1100(ActivityThread.java:139)
12-06 12:27:41.499: E/AndroidRuntime(16607):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1291)
12-06 12:27:41.499: E/AndroidRuntime(16607):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 12:27:41.499: E/AndroidRuntime(16607):    at android.os.Looper.loop(Looper.java:154)
12-06 12:27:41.499: E/AndroidRuntime(16607):    at android.app.ActivityThread.main(ActivityThread.java:4944)
12-06 12:27:41.499: E/AndroidRuntime(16607):    at java.lang.reflect.Method.invokeNative(Native Method)
12-06 12:27:41.499: E/AndroidRuntime(16607):    at java.lang.reflect.Method.invoke(Method.java:511)
12-06 12:27:41.499: E/AndroidRuntime(16607):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-06 12:27:41.499: E/AndroidRuntime(16607):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-06 12:27:41.499: E/AndroidRuntime(16607):    at dalvik.system.NativeStart.main(Native Method)
12-06 12:27:41.499: E/AndroidRuntime(16607): Caused by: java.lang.NullPointerException
12-06 12:27:41.499: E/AndroidRuntime(16607):    at com.sample.postcare2.HowItWorksActivity.onActivityResult(HowItWorksActivity.java:217)
12-06 12:27:41.499: E/AndroidRuntime(16607):    at android.app.Activity.dispatchActivityResult(Activity.java:4740)
12-06 12:27:41.499: E/AndroidRuntime(16607):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3383)
12-06 12:27:41.499: E/AndroidRuntime(16607):    ... 11 more

您必须对Uri使用static,如下所示
私有静态Uri\u图像。

希望这能解决您的问题

我已经解决了这个问题。实际上,我们需要以下java文件。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Image captured and saved to fileUri specified in the Intent
            Toast.makeText(this, "this is the uri for the iamge path:\n" +
                     data.getData(), Toast.LENGTH_LONG).show();
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
        }
    }
1.OnPictureSavedListener(接口) 2.CustomCameraActivity扩展了活动在PictureSavedListener上的实现 3.扩展SurfaceView的预览 4.PhotoHandler,实现android.hardware.Camera.PictureCallback 5.PhotoHelper它包含创建目录的方法和获得最佳预览大小的方法等 和一个名为activity_custom_camera.xml的xml文件。 需要以下许可

<uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
2:这是CustomCameraActivity类

 package ali.customcamera;


    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class CustomCameraActivity extends Activity implements
            OnPictureSavedListener, Preview.Callback {
        private Preview mPreview;
        Button btnCapture;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_custom_camera);
            mPreview = (Preview) findViewById(R.id.preview);
            btnCapture = (Button) findViewById(R.id.btnCapture);
            btnCapture.setOnClickListener(captureClick);
        }

        OnClickListener captureClick = new OnClickListener() {



            @Override
            public void onClick(View v) {
    //          Progresss.start(CustomCameraActivity.this);
                PhotoHandler photoHandler = new PhotoHandler(
                        CustomCameraActivity.this, CustomCameraActivity.this, 0);
                mPreview.takeScreenshot(photoHandler);
                btnCapture.setClickable(false);
            }
        };

// this is where we get the path of the image saved in sdcard
        @Override
        public void onPictureSaved(String imagePath) {
            // Progresss.stop();
            btnCapture.setClickable(true);
            if (imagePath != null && !imagePath.equals("")){
                Intent i = getIntent();
                i.putExtra("imagePath",imagePath );
                setResult(2500, i);
                finish();
            }
        }

        @Override
        public void cameraReady() {
            // TODO Auto-generated method stub

        }

        @Override
        public void cameraNotAvailable() {
            // TODO Auto-generated method stub

        }

    }
3:这是预习课

package ali.customcamera;

import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.util.AttributeSet;
import android.view.Display;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;




public class Preview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;
    private Callback mCallback;
    Activity mActivity;
    static boolean hasCamera = false;
    Context context;
    List<Camera.Size> previewSizes;
    Camera.Size previewSize;
    private PhotoHandler photoHandler;
    private int width;
    private int height;
    private boolean configured;


    public   Preview(Context context) {
        super(context);
        this.context = context;
        initHolder();
    }

    public   Preview(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        initHolder();
    }
    public   Preview(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initHolder();
    }
    public void setCallback(Callback c) {
        mCallback = c;
        mActivity = (Activity) c;
    }


    public interface Callback {
        public void cameraReady();
        public void cameraNotAvailable();
    }

    @SuppressWarnings("deprecation")
    private void initHolder() {
        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void takeScreenshot(PhotoHandler mPhotoHandler ) {

        try {

            this.photoHandler = mPhotoHandler;
            photoHandler.setCamDimensions(previewSize.width, previewSize.height);
            photoHandler.setSurfaceDimensions(width, height);
            mCamera.takePicture(null, null, null, mPhotoHandler);

        } catch (Exception e) {

            e.getCause();
        }

    }

    public void surfaceCreated(SurfaceHolder holder) {

        mHolder = holder;
        initCamera();

    }

    public void initCamera() {
        if (!hasCamera) {
            try {
                mCamera = Camera.open();
                hasCamera = true;
            } catch (RuntimeException e) {
                hasCamera = false;
                mCallback.cameraNotAvailable();
                return;
            }
            try {
                mCamera.setPreviewDisplay(mHolder);
            } catch (IOException exception) {
                mCamera.release();
                mCamera = null;
                hasCamera = false;
            }
        }
    }



    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

        width = w;
        height = h;

        if(!configured){
            configureCamera();
        }

        try 
        {
            mCamera.startPreview();

        } catch (Exception e) {

        }

    }

    private void configureCamera(){

        Parameters parameters = mCamera.getParameters();
        previewSizes = parameters.getSupportedPreviewSizes();

        Display display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        if(display.getRotation() == Surface.ROTATION_0)
        {
            previewSize = PhotoHelper.getOptimalPreviewSize(previewSizes, width, height, true);

            //parameters.setPreviewSize(previewSize.height, previewSize.width);//AQAG
            parameters.setPreviewSize(previewSize.width, previewSize.height);
            mCamera.setDisplayOrientation(90);
        }

        else if(display.getRotation() == Surface.ROTATION_90)
        {
            previewSize = PhotoHelper.getOptimalPreviewSize(previewSizes, width, height, false);
            parameters.setPreviewSize(previewSize.width, previewSize.height);   
            //parameters.setPreviewSize(previewSize.height, previewSize.width); 

        }

        else if(display.getRotation() == Surface.ROTATION_180)
        {
            previewSize = PhotoHelper.getOptimalPreviewSize(previewSizes, width, height, true);
            //parameters.setPreviewSize(previewSize.height, previewSize.width);   AQAG
            parameters.setPreviewSize(previewSize.width, previewSize.height);   
        }

        else if(display.getRotation() == Surface.ROTATION_270)
        {
            previewSize = PhotoHelper.getOptimalPreviewSize(previewSizes, width, height, false);

            parameters.setPreviewSize(previewSize.width, previewSize.height);
            mCamera.setDisplayOrientation(180);
        }

        mCamera.setParameters(parameters);
    }



    public void surfaceDestroyed(SurfaceHolder holder) {

        releaseCamera();
    }

    public void releaseCamera() {
        if (hasCamera) {
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
            hasCamera = false;
        }
    }
}
5:这是PhotoHelper类

package ali.customcamera;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.os.Environment;
import android.util.Log;

public class PhotoHelper {

    public static final int MEDIA_TYPE_IMAGE = 1;
    public static final int MEDIA_TYPE_VIDEO = 2;
//  private static final  String dirName = "CustomCamera";
    public static 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_DCIM/*DIRECTORY_PICTURES*/), "CustomCamera");
        // 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");
                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 if(type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "VID_"+ timeStamp + ".mp4");
        } else {
            return null;
        }

        return mediaFile;
    }

    public static Bitmap rotateBitmap90(Bitmap bitMap ){
        Matrix mat = new Matrix();
        mat.postRotate(90);

        return Bitmap.createBitmap(bitMap, 0, 0, 
                bitMap.getWidth(), bitMap.getHeight(), 
                mat, true);
    }


    public static Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h, boolean isPortrait) {
        final double ASPECT_TOLERANCE = 0.1;

        double targetRatio;

        if(isPortrait){
            targetRatio=(double)h / w;
        }
        else{
            targetRatio=(double)w / h;
        }

        if (sizes == null) return null;

        Camera.Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;

        int targetHeight = h;

        for (Camera.Size size : sizes) {
            double ratio = (double) size.width / size.height;
            if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }

        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Camera.Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }
}
package ali.customcamera;
导入java.io.File;
导入java.text.simpleDataFormat;
导入java.util.Date;
导入java.util.List;
导入android.graphics.Bitmap;
导入android.graphics.Matrix;
导入android.hardware.Camera;
导入android.os.Environment;
导入android.util.Log;
公共类摄影助手{
公共静态最终int媒体类型图像=1;
公共静态最终int媒体类型视频=2;
//私有静态最终字符串dirName=“CustomCamera”;
公共静态文件getOutputMediaFile(int类型){
//为了安全起见,您应该检查SD卡是否已安装
//在执行此操作之前,请先使用Environment.getExternalStorageState()。
File mediaStorageDir=新文件(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM/*DIRECTORY_PICTURES*/),“CustomCamera”);
//如果希望共享创建的图像,则此位置效果最佳
//在应用程序之间,并在卸载应用程序后保持。
//如果存储目录不存在,请创建该目录
如果(!mediaStorageDir.exists()){
如果(!mediaStorageDir.mkdirs()){
Log.d(“MyCameraApp”,“创建目录失败”);
返回null;
}
}
//创建媒体文件名
字符串时间戳=新的SimpleDateFormat(“yyyyMMdd_HHmmss”)。格式(新日期();
文件媒体文件;
如果(类型==媒体类型图像){
mediaFile=新文件(mediaStorageDir.getPath()+File.separator+
“IMG_”+时间戳+”.jpg”);
}else if(类型==媒体类型视频){
mediaFile=新文件(mediaStorageDir.getPath()+File.separator+
“视频+时间戳+”.mp4”);
}否则{
返回null;
}
返回媒体文件;
}
公共静态位图rotateBitmap90(位图位图){
矩阵矩阵=新矩阵();
材料旋转后(90);
返回位图.createBitmap(位图,0,0,
bitMap.getWidth(),bitMap.getHeight(),
mat,正确);
}
公共静态摄像头。大小getOptimalPreviewSize(列表大小,整数w,整数h,布尔值isPortrait){
最终双纵横比公差=0.1;
双目标;
如果(isPortrait){
targetRatio=(双)高/低;
}
否则{
targetRatio=(双)w/h;
}
如果(size==null)返回null;
照相机。大小优化大小=空;
double minDiff=double.MAX_值;
int targetHeight=h;
用于(相机尺寸:尺寸){
双倍比率=(双倍)size.width/size.height;
如果(数学abs(比率-目标比率)>纵横比公差)继续;
if(数学绝对值(尺寸高度-目标光)
最后,这是布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ali.customcamera.Preview
        android:id="@+id/preview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/btnCapture"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Capture" />

</RelativeLayout>


我知道我已经声明了Uri\u图像,但我目前没有使用它。我已将文件路径作为字符串(见上文),但在这两行中无法获取字符串:string imgfile=(string)getIntent().getExtras().get(“输出”);Log.e(“图像”,imgfile);我只是得到了一个异常。您需要使用intent.setData()而不是intent.putExtras(),这样只有您的onActivity结果才能理解。像这样的意图。setData(uri);uri=data.getData();如果您看到上面的logcat文件data=null,那么我不确定您的答案。感谢您的回复。我已将上述代码更改为:public void onClick(View v){Log.e(“使用pic buttn”,pictureFile.getPath());Uri Uri\u img=Uri.fromFile(pictureFile);Intent Intent=new Intent();Intent.setData(Uri\u img);finish()}此处pictureFile是静态文件变量,在PictureCallback()中为其赋值。然后我使用了uri_image=getIntent().getData();在OnActivityResult()中,但它仍然会给出与上面在Logcat file.ch中所示相同的错误
package ali.customcamera;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.os.Environment;
import android.util.Log;

public class PhotoHelper {

    public static final int MEDIA_TYPE_IMAGE = 1;
    public static final int MEDIA_TYPE_VIDEO = 2;
//  private static final  String dirName = "CustomCamera";
    public static 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_DCIM/*DIRECTORY_PICTURES*/), "CustomCamera");
        // 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");
                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 if(type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "VID_"+ timeStamp + ".mp4");
        } else {
            return null;
        }

        return mediaFile;
    }

    public static Bitmap rotateBitmap90(Bitmap bitMap ){
        Matrix mat = new Matrix();
        mat.postRotate(90);

        return Bitmap.createBitmap(bitMap, 0, 0, 
                bitMap.getWidth(), bitMap.getHeight(), 
                mat, true);
    }


    public static Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h, boolean isPortrait) {
        final double ASPECT_TOLERANCE = 0.1;

        double targetRatio;

        if(isPortrait){
            targetRatio=(double)h / w;
        }
        else{
            targetRatio=(double)w / h;
        }

        if (sizes == null) return null;

        Camera.Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;

        int targetHeight = h;

        for (Camera.Size size : sizes) {
            double ratio = (double) size.width / size.height;
            if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }

        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Camera.Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ali.customcamera.Preview
        android:id="@+id/preview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/btnCapture"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Capture" />

</RelativeLayout>