Click here to Skip to main content
15,882,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I'm trying to create a File from Uri(selected image). This is my code :
//get The real path from uri then save it (and then use it to create the file)

//Convert the image URI to the direct file system path of the image file
    private String getRealPathFromURI(Uri contentURI) {
        String result = "";
        try {
        Cursor cursor = getActivity().getContentResolver().query(contentURI, null, null, null, null);
        if (cursor == null) { // Source is Dropbox or other similar local file path
            result = contentURI.getPath();
        } else {
            int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            result = cursor.getString(idx); // Exception raised HERE
            cursor.close(); }
        } catch (Exception e) {
        return result;

But I got Always this exception :
java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it and the idx var == to -1
Updated 24-Nov-14 20:25pm
Afzaal Ahmad Zeeshan 24-Nov-14 15:04pm    
It is because that is a null, or there are no columns in it. Make sure you fill up the variable before using it.
Chlebta kaouach 24-Nov-14 15:25pm    
Which variable your talking about? the 'currImageURI' is filled from the selected image like this `currImageURI = data.getData();`
Afzaal Ahmad Zeeshan 24-Nov-14 15:25pm    
idx I am talking about.
Chlebta kaouach 24-Nov-14 15:40pm    
And how to fill it up ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900