1

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

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

}
4

5 回答 5

3

我创建了一个相机库项目,您可以使用它来拍照。我建议你使用这个库来拍照

编辑2:

protected void onActivityResult(int requestCode, int resultCode, Intent data)看看尝试在和之间放置一个日志super.onActivityResult(requestCode, resultCode, data);,看看是否正在调用该方法,如果它被调用,则检查返回的值。并且onActivityResult 不是由您的活动调用,而是由调用活动在这种情况下调用intentMediaStore.ACTION_IMAGE_CAPTURE因此您的 onActivityResult 必须由setResult()调用活动的调用。但我希望你不要试图通过像这里这样的意图传递位图

于 2012-11-09T08:29:13.237 回答
1

检查AndroidManifest.xml中是否设置了权限

<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

WRITE_EXTERNAL_STORAGE 用于获得更大的大图,您必须首先将图像文件存储在磁盘中。

于 2013-08-21T03:22:45.693 回答
1

android:launchMode="singleInstance"在 manifest.xml 中的活动中使用。删除它,它又开始工作了。希望能帮助到你。

于 2015-02-24T19:58:22.327 回答
0

您可以通过创建这样的意图来实现这一点

Intent cameraIntent = new Intent(
                        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, 0);

onActivityResult这样的

public void onActivityResult(int intRequestCode, int intResultCode,
            Intent objresult) {
        if (intResultCode == RESULT_OK) {
        if (intRequestCode == 0) {
            try {
                Bitmap photo = (Bitmap) objresult.getExtras().get("data");
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();
                strAttachmentCoded = new String(byteArray);
                } catch (Exception e) {
                Toast.makeText(context, "Picture not stored. Please try again",
                        Toast.LENGTH_LONG).show();
            }
        }
    }

:)

于 2012-11-09T08:47:49.867 回答
0
I faced same problem , once check have you put any tag related to History ?
Do not put android:noHistory="true" tag in manifest 
if you use android:noHistory="true" in your activity inside manifest , it will
remove from stack after on stop .

注意:如果您使用的是选项卡活动,那么您也不应该使用 android:noHistory="true" 或者简单地将 android:noHistory="false" 放在清单内的活动中。

于 2013-07-03T10:52:02.203 回答