我有一款適用於大多數手機的相機應用程序,但今天我收到了用戶的錯誤消息。該錯誤說應用程序崩潰Camera.setParameters()
我已閱讀This StackOverflow Post關於該主題,但已經實現了相同的解決方案。Android 2.3.4在Camera.setParameters上崩潰
下面是我使用的代碼:
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
camera.setDisplayOrientation(90);
Camera.Parameters parameters = camera.getParameters();
Camera.Size size = getBestPreviewSize(w, h);
parameters.setPreviewSize(size.width, size.height); // preview size
camera.setParameters(parameters);
camera.startPreview();
Camera.Parameters parameters = camera.getParameters();
List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
// You need to choose the most appropriate previewSize for your app
Camera.Size previewSize = // .... select one of previewSizes here
}
private Camera.Size getBestPreviewSize(int width, int height)
{
// Get For Photo Size
Camera.Parameters camparams = camera.getParameters();
// Find the Best Preview Size
List<Size> sizes = camparams.getSupportedPreviewSizes();
Camera.Size result=null;
int finalHeight = 0;
for (Size s : sizes) {
if (s.width <= width && s.height <= height) {
if (result == null) {
result = s;
finalHeight = s.height;
} else {
int resultArea=result.width*result.height;
int newArea=s.width*s.height;
if (newArea>resultArea) {
result=s;
finalHeight = s.height;
}
}
}
}
// Just in case...
if (result == null) {
finalHeight = height;
}
result.width = (int)(finalHeight*cameraRatio);
return result;
}
我的想法是把camera.setParameter(size.width, size.height)
圍繞try/catch
,但我不知道這是否會保持它在失敗時崩潰?
這裏是崩潰日誌已經發送到我的developer.android.com:
了java.lang.RuntimeException:setParameters在 未能在 android.hardware.Camera.native_setParameters(本機方法) android.hardware.Camera.setParameters(Camera.java:953)at net.feltpad.mosaic.Preview.surfaceChanged(CameraPreview.java:145)at android.view.SurfaceView.updateWindow(SurfaceView.java:557)at android.view.SurfaceView.dispatchDraw(SurfaceView.java:348)at android.view.ViewGroup.drawChild(ViewGroup.java:1730)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)在 android.view.ViewGroup.drawChild(ViewGroup.java:1730)在 android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)在 機器人.view.ViewGroup.drawChild(ViewGroup.java:1730)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)at android.view.View.draw(View.java:6988)at android.widget .FrameLayout.draw(FrameLayout.java:357)at android.view.ViewGroup.drawChild(ViewGroup.java:1732)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)at android.view.ViewGroup .drawChild(ViewGroup.java:1730)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)at and roid.view.ViewGroup.drawChild(ViewGroup.java:1730)在 android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)在 android.view.ViewGroup.drawChild(ViewGroup.java:1730)在 機器人。 view.ViewGroup.dispatchDraw(ViewGroup.java:1459)at android.view.ViewGroup.drawChild(ViewGroup.java:1730)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)at android.view。 View.draw(View.java:6988)at android.widget.FrameLayout.draw(FrameLayout.java:357)at android.view.ViewGroup.drawChild(ViewGroup.java:1732)at android.view.ViewGroup。 dispatchDraw(ViewGroup.java:1459)at android.view.View.draw(View.java:6988)at android.widget.FrameLayout.draw(FrameLayou t.java:357)at com.android.internal.policy.impl.PhoneWindow $ DecorView.draw(PhoneWindow.java:1961) at android.view.ViewRoot.draw(ViewRoot.java:1602)at android。 view.ViewRoot.performTraversals(ViewRoot.java:1323)at android.view.ViewRoot.handleMessage(ViewRoot.java:1961)at android.os.Handler.dispatchMessage(Handler.java:99)at android.os。 Looper.loop(Looper.java:150)at android.app.ActivityThread.main(ActivityThread.java:4333)at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method .invoke(Method.java:507)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839) at com.android.internal.os.Z ygoteInit.main(ZygoteInit.java:597)在 dalvik。system.NativeStart.main(Native Method)
它與鏈接中的問題具有相同的「LogCat」嗎?如果不同,請在此處發佈。 – Nerd
我所擁有的是我可以在developer.android.com的Android開發者控制檯中看到的崩潰錯誤 - 儘管我確實看到它在人身上崩潰。我會將該日誌添加到原始帖子中。 – Chris