1

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

}
Vinit ...
  • 1,409
  • 10
  • 37
  • 66

5 Answers5

3

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

Community
  • 1
  • 1
Girish Nair
  • 5,148
  • 5
  • 40
  • 61
  • Links are not good answer; neither libraries; especially for a less complex task – Manoj Kumar Nov 09 '12 at 08:49
  • @MoJo The link is a project that i have created as library for camera handling. Why paste the code here when i can show a proper path, First at-least consider reading it or visiting the link before down voting – Girish Nair Nov 09 '12 at 08:52
  • i know you've given a library; but do you think that a library is required to perform the action tat vikash wanted? world is already complex dont make it even more complex; If you're able to write a library, then you should be able to pick the code for him to perform that task; thats what he wants too – Manoj Kumar Nov 09 '12 at 08:55
  • @MoJoThe MediaStore.ACTION_IMAGE_CAPTURE doesnt always work, I had once used and was getting similar result like him and some times it doesnt work only. That is why i had to create a such a library. Libraries are used to make things simple not complex. – Girish Nair Nov 09 '12 at 08:59
  • adding a library makes your project bulky; your prob with action image capture is due to handset default cam app's customization; you can avoid it if you think sharp; This conversation is done – Manoj Kumar Nov 09 '12 at 09:25
  • Thank you all.... but you guys not specify what is the problem. Why some time onActivityResult() method is called some times it not called. – Vinit ... Nov 09 '12 at 09:45
  • @VinitVikash : I need to see your OnActivityResult method to say exactly where ur problem is – Manoj Kumar Nov 09 '12 at 09:48
  • @MoJo I edited my question I mention full code in onActivityResult() method. – Vinit ... Nov 09 '12 at 10:15
  • @Vinit: Please check the EDIT 2 of my answer – Girish Nair Nov 12 '12 at 04:51
1

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.

tmin
  • 1,313
  • 13
  • 15
1

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

atulkhatri
  • 10,896
  • 3
  • 53
  • 89
0

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

:)

Manoj Kumar
  • 1,510
  • 3
  • 20
  • 40
  • are u testing on a device or emulator? – Manoj Kumar Nov 09 '12 at 11:51
  • In my application I am capturing two photo. When the first photo is capture then I resize it putting name on photo and then store into the bitmap same thing I am doing for second photo. So I think because of internal memory the problem occur – Vinit ... Nov 09 '12 at 11:58
  • i think you should capture a picture, get it back to your app using the onactivityresult i posted, then again go back from here with a different request id comeback and do the processing for both; – Manoj Kumar Nov 09 '12 at 12:45
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 .

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 .

sravan
  • 5,303
  • 1
  • 31
  • 33