0

this doesn't always happen so i can't understand properly what's going on: my application take and modify a picture, then save it in external storage. If i try to open in the application a new saved pictures FROM A FILE MANAGER AND NOT FROM GALLERY, it crashes when executing cursor.getCount(), in DDMS i read the error:"cursor not closed before finally" this is the piece of code where the problem is, i can post more if necessary, thank you! ps this code is taken from other answers here in stackoverflow, as you could expect i'm not an expert so please be patient with me, thanks pps i can't see immediatly images in gallery after saving it, when they appear in gallery this error desappear.

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) { //HERE IS THE PROBLEM
            return -1;
        }

        cursor.moveToFirst();
        return cursor.getInt(0);
    }
samugi
  • 395
  • 5
  • 17

2 Answers2

2

You can use the

if(cursor.moveToFirst()) 
{
    //Your code here
}

instead off

cursor.getCount() 

it will return true if cursor size is greater then 0 else it will return false........so you can write like this.........

if (!cursor.moveToFirst())
   return -1;

else 
   return 1;
bashu
  • 1,710
  • 12
  • 16
0

Use this instead of the return statement. The cursor is getting leaked since you are not closing it

try{

    if (cursor.getCount() != 1) { //HERE IS THE PROBLEM
        return -1;
    }

    int i = 0;
    i++;
    cursor.moveToFirst();
   return cursor.getInt(0);

}finally{
if(cursor != null)
   cursor.close();
}

Edit:
When you open a file from File manager, the uri will be of the form file:///sdcard/filename but the Mediastore can only understand uri of the format content://media/audio/1. This is the reason you are getting cursor as null.

one way is to query the whole Mediastore and get the MediaStore.Images.Media.DATA column and compare with the path that you get from uri

nandeesh
  • 24,740
  • 6
  • 69
  • 79
  • ok this is good but doesn't solved my problem (maybe solved one and that wasn't the only one), i noticed now that the problem is that cursor is null. This happen only with new saved images, after some time when the image appears in the gallery everything works well. Is this line: Cursor cursor = context.getContentResolver().query(photoUri, new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null); correct? Thank you – samugi Aug 24 '12 at 14:26
  • Thank you very much, if you could make an example of how to do that it would be great, THANK YOU. (or just tell me where to look for, i don't want you to waste your time) – samugi Aug 24 '12 at 14:37
  • Immediately after you save the photo you need get the Mediascanning done . http://stackoverflow.com/questions/5107823/force-scan-files-after-taking-photo or this http://stackoverflow.com/questions/6673809/android-picture-saved-to-sd-card-not-showing-in-gallery?lq=1 – nandeesh Aug 24 '12 at 14:41
  • ok, now it's not a big problem, but images still don't appear immediately in gallery, even if i try to refresh it or close and open it again, is there something wrong with my device? – samugi Aug 24 '12 at 15:05
  • i guess it takes some time. not too sure – nandeesh Aug 24 '12 at 15:05
  • thank you for your help, i'll make a toast message in the application warning that could be necessary to wait some time if that happen. – samugi Aug 24 '12 at 15:12