Android:通过摄像头捕获照片后无法调用onActivityResult()方法

时间:2012-11-09 08:12:41

标签: android android-camera android-camera-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);
    }

}

5 个答案:

答案 0 :(得分:3)

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

编辑2:

尝试在protected void onActivityResult(int requestCode, int resultCode, Intent data)super.onActivityResult(requestCode, resultCode, data);之间放置一个日志,看看是否正在调用该方法,如果调用它,则检查返回的值。 AND onActivityResult不是由您的活动调用,而是由调用活动调用intent MediaStore.ACTION_IMAGE_CAPTURE,因此您的onActivityResult必须由setResult()调用呼叫活动。但我希望您不要尝试通过intent like here

传递位图

答案 1 :(得分: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用于获取更大的bigmap,您必须先将图像文件存储在磁盘中。

答案 2 :(得分:1)

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

答案 3 :(得分: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();
            }
        }
    }

:)

答案 4 :(得分:-1)

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 .

注意:如果您正在使用tab活动,那么您也不应该使用android:noHistory =“true”     或者简单地在清单中的活动中放入android:noHistory =“false”。