gpt4 book ai didi

Android:通过相机拍摄照片后无法调用 onActivityResult() 方法

转载 作者:行者123 更新时间:2023-11-29 16:11:02 24 4
gpt4 key购买 nike

在我的应用程序中,当我通过以下方式调用相机 Intent 时:

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);
}

}

最佳答案

我创建了一个 Camera Library Project你可以用它来拍照。我建议你使用这个库来拍照

编辑 2:

试试在 protected void onActivityResult(int requestCode, int resultCode, Intent data)super.onActivityResult(requestCode, resultCode, data); 之间放置一个日志看看如果甚至正在调用该方法,如果它被调用,则检查返回的值。 AND onActivityResult 不是由您的 Activity 调用,而是由调用 Activity 调用,在这种情况下是 MediaStore.ACTION_IMAGE_CAPTUREintent,因此您的 onActivityResult 必须是由调用 Activity 的 setResult() 调用。但我希望你不要试图通过 Intent 传递位图 like here

关于Android:通过相机拍摄照片后无法调用 onActivityResult() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13304385/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com