gpt4 book ai didi

android - 尝试拍照时应用程序崩溃

转载 作者:行者123 更新时间:2023-12-02 11:46:56 25 4
gpt4 key购买 nike

我的应用程序有 2 个问题:

-拍照时崩溃
-当我旋转设备时它崩溃

cameraPreview.java:

 public class CameraPreview extends Activity implements SurfaceHolder.Callback
{
Camera camera;
SurfaceView preview;
SurfaceHolder holder;
LayoutInflater controlInflater = null;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_camera_preview);
getWindow().setFormat(PixelFormat.UNKNOWN);

preview = (SurfaceView) findViewById(R.id.surface1);
holder = preview.getHolder();
holder.addCallback(this);
//holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

controlInflater = LayoutInflater.from(getBaseContext());
View viewControl = controlInflater.inflate(R.layout.control, null);
LayoutParams layoutParamsControl = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
this.addContentView(viewControl, layoutParamsControl); }
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
Camera camera = Camera.open();
Configuration c = getResources().getConfiguration();

if(c.orientation == Configuration.ORIENTATION_PORTRAIT)
{
camera.setDisplayOrientation(90);
}

if(camera!=null)
{
try
{
camera.setPreviewDisplay(holder);
}
catch (IOException e)
{
e.printStackTrace();
}
camera.startPreview();
}}

public void surfaceCreated(SurfaceHolder holder)
{

}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
if(camera!=null)
{
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera=null;
}
}

PictureCallback myPictureCallback_JPG = new PictureCallback()
{
@Override
public void onPictureTaken(byte[] arg0, Camera arg1)
{
Bitmap bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
}
};

public void take_picture_onClick(View view)
{
camera.takePicture(null, null, myPictureCallback_JPG);
} }

AndroidManifest.xml:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.paparazzi"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" android:required="true"/>

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:debuggable="true" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >

</activity>
<activity
android:name=".CameraPreview"
android:configChanges="orientation" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

起初我认为第二个问题可能是由于缺少这部分引起的:

public void surfaceDestroyed(SurfaceHolder holder) 
{
if(camera!=null)
{
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera=null;
}
}

但是添加后没有任何变化。

至于第一个问题,我不知道出了什么问题。该应用程序在 3 种不同的设备上进行了测试。如果有任何建议,我将不胜感激。

编辑

好的,所以我正在尝试按照建议获取日志,但我不确定我是否做对了:

public void take_picture_onClick(View view)
{
try
{
camera.takePicture(null, null, myPictureCallback_JPG);
}
catch(Exception ee)
{

try {
File filename = new File(Environment.getExternalStorageDirectory()+"/logfile4.log");
filename.createNewFile();
String cmd = "logcat -d -f "+filename.getAbsolutePath();
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

日志文件已创建,但它是空的,即使我给了它一点时间来加载。

最佳答案

好的,所以第二个问题是由这部分引起的(我认为)

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height)
{
Camera camera = Camera.open();

camera 应该是一个字段,而不是在每次调用 surfaceChanged 时创建。现在工作正常。

编辑
其实拍照也很好用。感谢您的建议。

关于android - 尝试拍照时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11499842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com