Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8093991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:39:04+00:00 2026-06-05T20:39:04+00:00

Hey i can’t seem to understand this error. I’m trying to select an image

  • 0

Hey i can’t seem to understand this error.
I’m trying to select an image by taking a picture or selecting from gallery.
When i try the method on a selected image it works fine but when i take an image from camera i get the error on the cursor.close() line

I have this code to capture image from gallery :

    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {  
    Uri selectedImage = mImageUri;
    getContentResolver().notifyChange(selectedImage, null);
    ImageView imageView = (ImageView) findViewById(R.id.chosenImage2);
    ContentResolver cr = getContentResolver();

    try {
         bitmap = android.provider.MediaStore.Images.Media
         .getBitmap(cr, selectedImage);
         //flip image if needed
         bitmap = Helpers.flipBitmap(bitmap, Helpers.getOrientation(this, selectedImage));

        imageView.setImageBitmap(bitmap);

    } catch (Exception e) {
        Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                .show();
        e.printStackTrace();
        Log.e("Camera", e.toString());

    }

}

and this is the getOrientation code :

  public static int getOrientation(Context context, Uri photoUri) {
        Cursor cursor = context.getContentResolver().query(photoUri,
                new String[] { MediaStore.Images.ImageColumns.ORIENTATION },
                null, null, null);

        try {
            if (cursor.moveToFirst()) {
                return cursor.getInt(0);
            } else {
                return -1;
            }
        } finally {
            cursor.close();
        }
    }

This producing null pointer exception and i can’t understand why.

Any help?

EDIT:

This is how i call the Intent :

     ImageView imageView = (ImageView) findViewById(R.id.chosenImage2);
     if(imageView.getDrawable() == null){
         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
         File photo = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis()+ ".jpg");
         intent.putExtra(MediaStore.EXTRA_OUTPUT,
         Uri.fromFile(photo));
         mImageUri = Uri.fromFile(photo);
         startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
     }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:39:05+00:00Added an answer on June 5, 2026 at 8:39 pm

    ContentResolver.query(…) may return null as you can find in the documentation.

    Very likely cursor.moveToFirst() throws an NullPointerException stopping execution of the try block but running the finally code:
    cursor.close() which is null.close() => KABAM.

    You could check for cursor != null at various places. E.g. before entering the try block or in the finally block.

    However safest way to get across this would be catching the NullPointerException.

    public static int getOrientation(Context context, Uri photoUri) {
        Cursor cursor = context.getContentResolver().query(photoUri,
                new String[] { MediaStore.Images.ImageColumns.ORIENTATION },
                null, null, null);
        //cursor might be null!
    
        try {
            int returnMe;
            if (cursor.moveToFirst()) {
                returnMe = cursor.getInt(0);
            } else {
                returnMe = -1;
            }
            cursor.close();
            return returnMe;
        } catch(NullPointerException e) {
            //log: no cursor found returnung -1!
            return -1;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey, I can't seem to access the returned value from a method I called
Hey this is probably a dumb question but i can't seem to find a
Hey guys I can't seem to get the syntax here right, I'm just trying
hey I'm trying to build this simple debugger class so i can see flash
Hey this is a simple question but i can't seem to find an answer
Hey this was probably asked a couple of times but I can't seem to
Hey all how can i set this up for a loop? data.row9_1 I cant
Hey everyone I got a javascript problem I can't seem to find a specific
Hey guys I'm trying to make a GUI which can navigate through JTextAreas when
Hey guys, this is simple but I can't make it workn... blah! I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.