我有一个使用GCM从服务器接收消息的IntentService,一切正常,除非我尝试使用android.hardware.Camera拍照,InentService在调用onPictureTaken之前被销毁! ,那么在InentService停止之前我应该怎么做才能保存拍摄的照片?
public class GcmIntentService extends IntentService {
String function;
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
String msg = intent.getStringExtra("message");
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
System.out.println("message received");
try {
json = new JSONObject(msg);
function = json.getString("function");
} catch (JSONException e) {
e.printStackTrace();
}
if (function.equals("capture")) {
System.out
.println("capture entereeeeed =====================");
Capture cap = new Capture(getApplicationContext());
cap.captureNow();
}
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
@Override
public void onDestroy() {
if (Capture.camera != null) {
System.out.println("camera released");
Capture.camera.release();
}
super.onDestroy();
}
}
Capture类,来自vogella教程http://www.vogella.com/tutorials/AndroidCamera/article.html
public class Capture {
Context captureContext = null;
public static Camera camera;
private int cameraId = 0;
final static String DEBUG_TAG = "Capture Class";
public Capture(Context context) {
captureContext = context;
// do we have a camera?
if (!captureContext.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA)) {
System.out.println("no camera found on this device");
} else {
cameraId = findFrontFacingCamera();
if (cameraId < 0) {
System.out.println("no front camera found");
} else {
camera = Camera.open(cameraId);
}
}
}
public void captureNow() {
System.out.println("capture now entered");
camera.takePicture(null, null, new PhotoHandler(captureContext));
}
public int findFrontFacingCamera() {
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_FRONT) {
Log.d(DEBUG_TAG, "Camera found");
cameraId = i;
break;
}
}
return cameraId;
}
}
PhotoHandler类
public class PhotoHandler implements PictureCallback {
private final Context context;
public static String filename;
public PhotoHandler(Context context) {
System.out.println("enetered photoHandler");
this.context = context;
}
@Override
public void onPictureTaken(byte[] data, android.hardware.Camera camera) {
System.out.println("entered onPicture taken");
File pictureFileDir = getDir();
System.out.println("file directory = " + pictureFileDir);
if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
Log.d(Capture.DEBUG_TAG, "Can't create directory to save image.");
return;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "Picture_" + date + ".jpg";
filename = pictureFileDir.getPath() + File.separator + photoFile;
File pictureFile = new File(filename);
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
System.out.println("New Image saved: " + photoFile);
Capture.camera.release();
} catch (Exception error) {
Log.d(Capture.DEBUG_TAG,
"File" + filename + "not saved: " + error.getMessage());
System.out.println("image could not be saved");
}
}
private File getDir() {
File sdDir = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
return new File(sdDir, "blah");
}
}
答案 0 :(得分:0)
答案很简单只需在AsyncTask中执行相机操作即可。可能会有用。
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
String msg = intent.getStringExtra("message");
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
System.out.println("message received");
try {
json = new JSONObject(msg);
function = json.getString("function");
} catch (JSONException e) {
e.printStackTrace();
}
if (function.equals("capture")) {
System.out.println("capture entereeeeed =====================");
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void...params) {
Capture cap = new Capture(getApplicationContext());
cap.captureNow();
String result = "ok";
return result;
}
@Override
protected void onPostExecute(String result) {
}
}.execute();
}
}
}