Question

I Saw another questions about this problem but non of them helped.

In my app there two different ways for adding an image and displaying it inside an imageview. 1. Gallery - 2. Camera.

gallery way is working just fine.

But in Camera way (which I'm doing it with native android Camera) I have a confusing problem.

If I rotate my phone to portrait mode (so the portrait xml loads) and take the photo in portrait mode, it works fine.

If I rotate it to landscape and use use camera also in landscape, it works fine too.

now if my xml is in portrait and after going to camera I rotate it to landscape (to take a lansdcape photo) app crashes and gives me this Errors:

05-08 21:34:22.974: E/AndroidRuntime(19000): FATAL EXCEPTION: main
05-08 21:34:22.974: E/AndroidRuntime(19000): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.kpaxteam.babyalbum/com.tiktak.albums1.A2}: java.lang.NullPointerException
05-08 21:34:22.974: E/AndroidRuntime(19000):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2918)
05-08 21:34:22.974: E/AndroidRuntime(19000):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:2970)
05-08 21:34:22.974: E/AndroidRuntime(19000):    at android.app.ActivityThread.access$2000(ActivityThread.java:132)
05-08 21:34:22.974: E/AndroidRuntime(19000):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1068)
05-08 21:34:22.974: E/AndroidRuntime(19000):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 21:34:22.974: E/AndroidRuntime(19000):    at android.os.Looper.loop(Looper.java:150)
05-08 21:34:22.974: E/AndroidRuntime(19000):    at android.app.ActivityThread.main(ActivityThread.java:4277)
05-08 21:34:22.974: E/AndroidRuntime(19000):    at java.lang.reflect.Method.invokeNative(Native Method)
05-08 21:34:22.974: E/AndroidRuntime(19000):    at java.lang.reflect.Method.invoke(Method.java:507)
05-08 21:34:22.974: E/AndroidRuntime(19000):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-08 21:34:22.974: E/AndroidRuntime(19000):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-08 21:34:22.974: E/AndroidRuntime(19000):    at dalvik.system.NativeStart.main(Native Method)
05-08 21:34:22.974: E/AndroidRuntime(19000): Caused by: java.lang.NullPointerException
05-08 21:34:22.974: E/AndroidRuntime(19000):    at android.content.ContentResolver.acquireProvider(ContentResolver.java:745)
05-08 21:34:22.974: E/AndroidRuntime(19000):    at android.content.ContentResolver.query(ContentResolver.java:258)
05-08 21:34:22.974: E/AndroidRuntime(19000):    at com.tiktak.babyalbum.Helper.pathgal(Helper.java:161)
05-08 21:34:22.974: E/AndroidRuntime(19000):    at com.tiktak.babyalbum.Helper.resultpic(Helper.java:299)
05-08 21:34:22.974: E/AndroidRuntime(19000):    at com.tiktak.albums1.A2.onActivityResult(A2.java:242)
05-08 21:34:22.974: E/AndroidRuntime(19000):    at android.app.Activity.dispatchActivityResult(Activity.java:4053)
05-08 21:34:22.974: E/AndroidRuntime(19000):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2914)

My Intent for taking photo:

                       Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
                       cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);  
                       act.startActivityForResult(cameraIntent, int2);

My ActivityResult:

else if (requestCode == CAMERA_REQUEST) {
        if (resultCode == RESULT_OK) {
 Helper line 299--->        String capturedImageFilePath = Helper.pathgal(act, mCapturedImageURI);
            bmp = Helper.decodeFile(capturedImageFilePath, act);
            if(bmp != null )
            {
                try {
                    Helper.showpic(act, id1, bmp);
                    settings = act.getSharedPreferences(st1, 0);
                    settings.edit().putString(st2, capturedImageFilePath).putInt(st3, View.VISIBLE).putInt(st4, View.GONE).commit();;
                    img1 = (ImageView) act.findViewById(id1);
                    img2 = (ImageView) act.findViewById(id2);
                    img1.setVisibility(View.VISIBLE);
                    img2.setVisibility(View.GONE);
                } catch (OutOfMemoryError e) {
                    Toast.makeText(act.getApplicationContext(), "Try Again",
                            Toast.LENGTH_LONG).show();
                }


            }

getting image file path code:

public static String pathgal(Activity act, Uri uri){

    String[] filePathColumn = {MediaStore.Images.Media.DATA};

 Helper 161--->   Cursor cursor = act.getContentResolver().query(uri, filePathColumn, null, null, null);

    if (cursor != null) {
    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String filePath = cursor.getString(columnIndex);
    cursor.close();

    return filePath;
    }
    return null;

}

any idea?

Was it helpful?

Solution 2

I Should've Just add android:configChanges="orientation" in my manifest to activities.

Thanks everyvone.

OTHER TIPS

Use following code

I had same problem resolved by it`

private void dispatchTakePictureIntent() {
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

        timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        name=mode+"_"+img_type+"_"+timeStamp+".jpeg";
        output = new File(dir,name);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(output));
                    startActivityForResult(cameraIntent, 1);


    }

and in onActivityResult use output.getAbsolutePath()(where output is the file which we used to create uri to pass in camera intent)) to get that image

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {

            if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

String IMAGE_URL=output.getAbsolutePath();

            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top