我的应用有 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();
}
}
}
日志文件已创建,但它是空的,即使我给它一点时间来加载它。