我的应用有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();
}
}
}
创建了日志文件,但它是空的,即使我给它加载片刻。
答案 0 :(得分:1)
好的,所以第二个问题是由这部分引起的(我认为)
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height)
{
Camera camera = Camera.open();
摄像机应该是一个字段,而不是每次调用surfaceChanged时都要创建。现在工作正常。
EDIT
实际上,拍照也很好。谢谢你的建议。