在android中获取高支持的图片大小

时间:2013-08-21 17:34:45

标签: android image camera

我有相机应用拍照。我的问题是当我拍照时它是240 * 320,但我的相机可以拍摄高清照片。如何才能拍摄高支持的照片?我试试这段代码:

public void GetHighResolutionForCamera(){
    int Max = 0;
    Camera.Size MR = null ;
    Parameters params = mCamera.getParameters();
    List<Camera.Size> resolution ;
    resolution = params.getSupportedPictureSizes();
    for (Camera.Size size : resolution) {
        if(Max < size.height){
            MR = size ;
            Max = size.height;
        }
    }
    params.setPictureSize(MR.width, MR.height);
    mCamera.setParameters(params);
}

但是在尝试设置参数时出错。这与表面视图有关吗?什么时候我可以为我的相机设置参数?

这是logcat:

08-21 22:21:27.442: D/AndroidRuntime(27812): Shutting down VM
08-21 22:21:27.442: W/dalvikvm(27812): threadid=1: thread exiting with uncaught exception (group=0x41a5e700)
08-21 22:21:27.458: E/AndroidRuntime(27812): FATAL EXCEPTION: main
08-21 22:21:27.458: E/AndroidRuntime(27812): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mytackpicture/com.example.mytackpicture.MainActivity}: java.lang.NullPointerException
08-21 22:21:27.458: E/AndroidRuntime(27812):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-21 22:21:27.458: E/AndroidRuntime(27812):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-21 22:21:27.458: E/AndroidRuntime(27812):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-21 22:21:27.458: E/AndroidRuntime(27812):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-21 22:21:27.458: E/AndroidRuntime(27812):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 22:21:27.458: E/AndroidRuntime(27812):    at android.os.Looper.loop(Looper.java:137)
08-21 22:21:27.458: E/AndroidRuntime(27812):    at android.app.ActivityThread.main(ActivityThread.java:5103)
08-21 22:21:27.458: E/AndroidRuntime(27812):    at java.lang.reflect.Method.invokeNative(Native Method)
08-21 22:21:27.458: E/AndroidRuntime(27812):    at java.lang.reflect.Method.invoke(Method.java:525)
08-21 22:21:27.458: E/AndroidRuntime(27812):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-21 22:21:27.458: E/AndroidRuntime(27812):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-21 22:21:27.458: E/AndroidRuntime(27812):    at dalvik.system.NativeStart.main(Native Method)
08-21 22:21:27.458: E/AndroidRuntime(27812): Caused by: java.lang.NullPointerException
08-21 22:21:27.458: E/AndroidRuntime(27812):    at com.example.mytackpicture.MainActivity.GetHighResolutionForCamera(MainActivity.java:63)
08-21 22:21:27.458: E/AndroidRuntime(27812):    at com.example.mytackpicture.MainActivity.onCreate(MainActivity.java:57)
08-21 22:21:27.458: E/AndroidRuntime(27812):    at android.app.Activity.performCreate(Activity.java:5133)
08-21 22:21:27.458: E/AndroidRuntime(27812):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-21 22:21:27.458: E/AndroidRuntime(27812):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-21 22:21:27.458: E/AndroidRuntime(27812):    ... 11 more

1 个答案:

答案 0 :(得分:0)

它的解决方法和纠正方法:

public void GetHighResolutionForCamera(){
    int Max = 0;
    Parameters params = mCamera.getParameters();
    List<Camera.Size> resolution ;
    resolution = params.getSupportedPictureSizes();
    Camera.Size MR = resolution.get(0);
    for (Camera.Size size : resolution) {
        if(Max < size.height){
            MR = size ;
            Max = size.height;
        }
    }
    params.setPictureSize(MR.width, MR.height);
    mCamera.setParameters(params);
}

问题,因为MR为空