0

I want to set an image taken by my device camera on to an image view,When i do this ,the image is rotating .So i have tried this code ,But it throws a NullPointer Exception.I could not understand the problem ,can anyone help?

 java.lang.NullPointerException
        at android.content.ContentResolver.openInputStream(ContentResolver.java:490)
        at project1.me.com.kupdate.ImageUploadActivity.getCorrectlyOrientedImage(ImageUploadActivity.java:270)
        at project1.me.com.kupdate.ImageUploadActivity.onCreate(ImageUploadActivity.java:136)
        at android.app.Activity.performCreate(Activity.java:5372)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
        at android.app.ActivityThread.access$700(ActivityThread.java:159)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5419)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
        at dalvik.system.NativeStart.main(Native Method)

And My java code is

 public static int getOrientation(Context context, Uri photoUri) {
/* it's on the external media. */
    Cursor cursor = context.getContentResolver().query(photoUri,
            new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

    if (cursor.getCount() != 1) {
        return -1;
    }

    cursor.moveToFirst();
    return cursor.getInt(0);
}

public static Bitmap getCorrectlyOrientedImage(Context context, Uri photoUri) throws IOException {




       InputStream is = context.getContentResolver().openInputStream(photoUri);
       Log.e("Bitmap", "Bitmap ok ");
       BitmapFactory.Options dbo = new BitmapFactory.Options();
       dbo.inJustDecodeBounds = true;
       BitmapFactory.decodeStream(is, null, dbo);

       is.close();

       int rotatedWidth, rotatedHeight;
       int orientation = getOrientation(context, photoUri);

       if (orientation == 90 || orientation == 270) {
           rotatedWidth = dbo.outHeight;
           rotatedHeight = dbo.outWidth;
       } else {
           rotatedWidth = dbo.outWidth;
           rotatedHeight = dbo.outHeight;
       }

    Bitmap srcBitmap;
    is = context.getContentResolver().openInputStream(photoUri);
    if (rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION) {
        float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION);
        float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION);
        float maxRatio = Math.max(widthRatio, heightRatio);

        // Create the bitmap from file
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = (int) maxRatio;
        srcBitmap = BitmapFactory.decodeStream(is, null, options);
    } else {
        srcBitmap = BitmapFactory.decodeStream(is);
    }
    is.close();

/*
 * if the orientation is not 0 (or -1, which means we don't know), we
 * have to do a rotation.
 */
    if (orientation > 0) {
        Matrix matrix = new Matrix();
        matrix.postRotate(orientation);

        srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(),
                srcBitmap.getHeight(), matrix, true);
    }
    Log.e("Bitmap", "Bitmap ok ");
       return srcBitmap;


}

   ///Camera Image
    try {
        if (picturePath == null) {
            Intent intent3 = getIntent();
            state = intent3.getExtras().getInt("state_one");
            Log.e(TAG, "Camera Image ");
        }  if(state==1) {
            Intent intent3 = getIntent();
            Log.e(TAG, "Camera Image Inside ");
            picturePath = intent3.getExtras().getString("filePath");
            camImageUri = intent3.getParcelableExtra("filePath");
            imageView = (ImageView) findViewById(R.id.imgView);
            Log.e(TAG, "Before method ");
          //  decodeFile(picturePath);
            try {
                bitmap = getCorrectlyOrientedImage(ImageUploadActivity.this, camImageUri);
            }
            catch (Exception e)
            {
                e.toString();
               // e.printStackTrace();
                Log.e(e.getClass().getName(), e.getMessage(), e);
                 stackTrace = Log.getStackTraceString(e);
                Toast.makeText(getApplicationContext(), "stackTrace" +stackTrace, Toast.LENGTH_SHORT).show();
            }
            Toast.makeText(getApplicationContext(), "stackTrace" +stackTrace, Toast.LENGTH_SHORT).show();
            Log.e(TAG, "After method ");
           String temp= BitMapToString(bitmap);
            Toast.makeText(getApplicationContext(),
                    "Camera"+temp, Toast.LENGTH_SHORT).show();
            if(bitmap==null)
            {
                Log.e(TAG, "Bitmap is null ");

            }
            imageView.setImageBitmap(bitmap);
          //  Toast.makeText(getApplicationContext(), "PATH" + picturePath, Toast.LENGTH_SHORT).show();
            bitmap = ShrinkBitmap(picturePath, 300, 300);

        }

oncreate

Oncreate method-Here I am calling getCorrectlyOrientedImage() method 


   ///Camera Image
    try {
        if (picturePath == null) {
            Intent intent3 = getIntent();
            state = intent3.getExtras().getInt("state_one");
            Log.e(TAG, "Camera Image ");
        }  if(state==1) {
            Intent intent3 = getIntent();
            Log.e(TAG, "Camera Image Inside ");
            picturePath = intent3.getExtras().getString("filePath");
            camImageUri = intent3.getParcelableExtra("filePath");
            imageView = (ImageView) findViewById(R.id.imgView);
            Log.e(TAG, "Before method ");
          //  decodeFile(picturePath);
            try {
                bitmap = getCorrectlyOrientedImage(ImageUploadActivity.this, camImageUri);
            }
            catch (Exception e)
            {
                e.toString();
               // e.printStackTrace();
                Log.e(e.getClass().getName(), e.getMessage(), e);
                 stackTrace = Log.getStackTraceString(e);

            }




            imageView.setImageBitmap(bitmap);

            bitmap = ShrinkBitmap(picturePath, 300, 300);

        }
Tom
  • 296
  • 6
  • 22

1 Answers1

0

You have to store "picturePath", during image capture activity might be getting recreated so you are getting "picturePath" as null. You can store your image path using onSaveInstanceState and onRestoreInstanceState methods. Also go through this link

Community
  • 1
  • 1
Narendra
  • 609
  • 1
  • 12
  • 28
  • Picture path is not null here,Actually i am passing image Uri as a parameter on to that method – Tom Jul 24 '15 at 11:17
  • So could you please check the value "photoUri"? Is that file real path existing. – Narendra Jul 24 '15 at 11:21
  • As null pointer exception is coming for openInputStream so chances for "photoUri" being null are more or its not real file path. Please check it by logging it somewhere. – Narendra Jul 24 '15 at 11:23
  • camImageUri = intent3.getParcelableExtra("filePath"); – Tom Jul 24 '15 at 11:34
  • From above code snippet your camImageUri and picturePath are pointing to the same file. You can just simply convert this picturePath like Uri.parse(picturePath) to file Uri which is required for openInputStream. – Narendra Jul 24 '15 at 11:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/84179/discussion-between-narendra-and-jennie). – Narendra Jul 24 '15 at 12:30