我有一个非常奇怪的问题。我下面的代码用于在按钮单击时拍照。它可以在Jelly Bean手机上正常运行,但不能在Kitkat上运行:
MainActivity.java:
package com.example.takepic;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private final static String DEBUG_TAG = "MakePhotoActivity";
private Camera camera;
private Button capture = null;
private int cameraId = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
capture = (Button)findViewById(R.id.captureBack);
capture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
camera.startPreview(); //After this, nothing gets printed, and picture does not get taken
System.out.println("Camera preview has started.");
camera.takePicture(null, null, new PhotoHandler(getApplicationContext()));
}
});
// do we have a camera?
if (!getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG)
.show();
} else {
cameraId = findBackFacingCamera();
if (cameraId < 0) {
Toast.makeText(this, "No back facing camera found.",
Toast.LENGTH_LONG).show();
} else {
camera = Camera.open(cameraId);
}
}
}
private int findBackFacingCamera() {
int cameraId = -1;
// Search for the front facing camera
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == CameraInfo.CAMERA_FACING_BACK) {
Log.d(DEBUG_TAG, "Camera found");
cameraId = i;
break;
}
}
return cameraId;
}
@Override
protected void onPause() {
if (camera != null) {
camera.release();
camera = null;
}
super.onPause();
}
}
PhotoHandler.java:
package com.example.takepic;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
public class PhotoHandler implements PictureCallback {
private final Context context;
public PhotoHandler(Context context) {
this.context = context;
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFileDir = getDir();
Toast.makeText(context, "Entered onPictureTaken", Toast.LENGTH_LONG).show();
if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
Log.d("Directory error", "Can't create directory to save image.");
Toast.makeText(context, "Can't create directory to save image.",
Toast.LENGTH_LONG).show();
return;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "Picture_" + date + ".jpg";
String filename = pictureFileDir.getPath() + File.separator + photoFile;
File pictureFile = new File(filename);
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
Toast.makeText(context, "New Image saved:" + photoFile,
Toast.LENGTH_LONG).show();
} catch (Exception error) {
Log.d("File saving error", "File" + filename + "not saved: "
+ error.getMessage());
Toast.makeText(context, "Image could not be saved.",
Toast.LENGTH_LONG).show();
}
}
private File getDir() {
// File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File sdDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
Toast.makeText(context, ("Path : "+sdDir.getAbsolutePath()), Toast.LENGTH_LONG).show();
return sdDir;
}
}
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.takepic"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.takepic.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
我已经投放了很多吐司消息和打印语句以用于调试。我没有在此处发布logcat,因为在KitKat手机上运行logcat时,logcat上我什么都没得到。没有例外或警告。
[当我在jellybean手机上运行它时,它会正常运行,显示所有吐司和照片并拍照。
当我在Kitkat上运行此命令时,>]之后没有收到任何调试消息。
camera.startPreview(); System.out.println("Camera preview has started.");
我怀疑takePicture API有问题,但是我无法对其进行调试。
经过进一步分析,我找到了问题的原因。 PhotoHandler对象已成功调用,但是未调用onPictureTaken方法,可能是因为它没有获取有关相机单击图片的信息。我不知道为什么。
我有一个非常奇怪的问题。我下面的代码用于在按钮单击时拍照。它可以在Jelly Bean手机上正常运行,但不能在Kitkat上运行:MainActivity.java:package com ....
我发现您没有为相机分配任何表面支架。为相机提供预览表面很重要。
KitKat的垃圾回收工作与以前的API不同。
在清单文件中添加uses-feature
: