Android: Not able to call onActivityResult() method after capturing photo by camera

无人久伴 提交于 2019-12-11 07:38:11

问题


In my application when I call camera intent by:

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);

it not call onActivityResult() method.

Problem in my application is that some time it calls this method, but some time after capturing photo it again come to photo capture screen.

Before capturing photo I am saving a lot of data in onSaveInstanceState() after that I am collecting this data by onRestoreInstanceState().

Here I don't know why some time onActivityResult() method calls but some time this method does not call after entering into the photo capture mode.

onActivityResult() code:

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

}

回答1:


I have created a Camera Library Project you can use this to take picture. I would suggest you to use this library to take picture

EDIT 2:

Look try placing a log between protected void onActivityResult(int requestCode, int resultCode, Intent data) and super.onActivityResult(requestCode, resultCode, data); to see if even that method is being called or not, If its called then check the value that is returned. AND The onActivityResult is not called by your activity but by the calling activity in this case the intent of MediaStore.ACTION_IMAGE_CAPTURE so your onActivityResult has to be called by the setResult() of the calling activity. But I hope your not trying to pass the bitmap via intent like here




回答2:


Check whether permissions are set in AndroidManifest.xml

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

The WRITE_EXTERNAL_STORAGE is for getting bigger bigmap where you have to store the image file in the disk first.




回答3:


I was using android:launchMode="singleInstance" in my activity in manifest.xml. Removed it and it started working again. Hope it helps.




回答4:


You can achieve this by creating an intent like this

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

and an onActivityResult like this

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

:)




回答5:


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 .

Note : if you are using tab activity then also you should not use android:noHistory="true" or else simply put android:noHistory="false" in activity inside manifest .



来源:https://stackoverflow.com/questions/13304385/android-not-able-to-call-onactivityresult-method-after-capturing-photo-by-cam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!