在我的应用程序中,当我通过以下方式调用相机意图时:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
它不调用onActivityResult()
方法。
我的应用程序中的问题是,它有时会调用此方法,但在拍摄照片后一段时间,它会再次进入照片拍摄屏幕。
在拍摄照片之前,我会保存大量数据,onSaveInstanceState()
之后我会通过onRestoreInstanceState()
.
这里我不知道为什么有些时间onActivityResult()
方法调用,但有些时候进入照片捕获模式后这个方法没有调用。
onActivityResult() 代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String fileName = null;
try {
if (requestCode == CAMERA_REQUEST) {
roughBitmap = (Bitmap) data.getExtras().get("data");
final ContentResolver cr = getContentResolver();
final String[] p1 = new String[] {
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATE_TAKEN };
Cursor c1 = cr.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null,
null, p1[1] + " DESC");
if (c1.moveToFirst()) {
String uristringpic = "content://media/external/images/media/"
+ c1.getInt(0);
newuri = Uri.parse(uristringpic);
// Log.i("TAG", "newuri "+newuri);
snapName = getRealPathFromURI(newuri);
Uri u = Uri.parse(snapName);
File f = new File("" + u);
fileName = f.getName();
}
c1.close();
setImageParameter();
}
} catch (NullPointerException e) {
}
System.out.println("*** End of onActivityResult() ***");
}
public void setImageParameter() {
// decode full image
roughBitmap = BitmapFactory.decodeFile(snapName);
// calc exact destination size
Matrix matrix = new Matrix();
RectF inRect = new RectF(0, 0, roughBitmap.getWidth(),
roughBitmap.getHeight());
// RectF outRect = new RectF(0, 0, dstWidth, dstHeight);
RectF outRect = new RectF(0, 0, 640, 480);
matrix.setRectToRect(inRect, outRect, Matrix.ScaleToFit.CENTER);
float[] values = new float[9];
matrix.getValues(values);
// resize bitmap
resizedBitmap = Bitmap.createScaledBitmap(roughBitmap,
(int) (roughBitmap.getWidth() * values[0]),
(int) (roughBitmap.getHeight() * values[4]), true);
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setTextSize(16);
paint.setTextAlign(Paint.Align.LEFT);
Canvas canvas = new Canvas(resizedBitmap);
canvas.drawText(String.valueOf(lat), resizedBitmap.getWidth() - 290, resizedBitmap.getHeight() - 50, paint);
canvas.drawText(String.valueOf(lng), resizedBitmap.getWidth() - 140, resizedBitmap.getHeight() - 50, paint);
if (!editTextRoadName.getText().toString().equalsIgnoreCase("")) {
canvas.drawText(editTextRoadName.getText().toString(), resizedBitmap.getWidth() - 290, resizedBitmap.getHeight() - 30, paint);
}
canvas.drawText(new DateClass().getSysDateTimeForPhoto(), resizedBitmap.getWidth() - 290, resizedBitmap.getHeight() - 10, paint);
if (nFinalOrientation == 1) {
matrix.postRotate(90);
} else {
matrix.postRotate(0);
}
rotatedBitmap = Bitmap.createScaledBitmap(resizedBitmap,
(int) (resizedBitmap.getWidth() * values[0]),
(int) (resizedBitmap.getHeight() * values[4]), true);
if (booleanPhotoFlag) {
booleanPhotoFlag = false;
photoBitmap = rotatedBitmap.copy(Bitmap.Config.ARGB_8888, true);
imageViewPhoto.setImageBitmap(photoBitmap);
} else {
landmarkBitmap = rotatedBitmap.copy(rotatedBitmap.getConfig(), rotatedBitmap.isMutable() ? true : false);
imageViewLocationPhoto.setImageBitmap(landmarkBitmap);
}
}