0

I have an imageview, and a button, starting an image intent.

I pass the uri from image intent to a function that should return the real path from the uri.

here's the activity result of image intent:

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);

    FindViewById<TextView>(Resource.Id.imguri1).Text = a.GetRealPathFromURI(data.Data);
}

and the function returning the real path:

public class GiocaUri : Activity
{
    public string GetRealPathFromURI(Android.Net.Uri a)
    {
        ICursor cursor;
        cursor = ContentResolver.Query(a, null, null, null, null);
        cursor.MoveToFirst();
        string documentId = cursor.GetString(0);
        documentId = documentId.Split(':')[1];
        cursor.Close();

        cursor = ContentResolver.Query(
            Android.Provider.MediaStore.Images.Media.ExternalContentUri,
            null, MediaStore.Images.Media.InterfaceConsts.Id + " = ? ", 
            new[] { documentId }, null);
        cursor.MoveToFirst();
        string path = cursor.GetString(
            cursor.GetColumnIndex(MediaStore.Images.Media.InterfaceConsts.Data));
        cursor.Close();

        return path;

    }
}

I test the app and after choosing image, i get error here:

ContentResolver.Query(a, null, null, null, null);

this exception:

Unhandled Exception:

Java.Lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference

I wrote uri in console and it's not null.

What i'm doing wrong? Thanks!

Edit: As far as I know, there is nothing NULL (well.. contenresolver.query needs only one value, others can bee NULL, i tried filling them with a projector and so on, obviously not null things, and problem was the same).

I understand why this post could be a duplicate, but I wrote variables in console and are not null.

Plus, I used this guide for image intent, and this for resolver.

Plus, I tried another method to query the database, always from second link, and the error is the same:

string path = null;

            string[] projection = new[] 
            { Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data };

            var loader = new CursorLoader(this, a, projection, null, null, null);

            var cursor = (ICursor)loader.LoadInBackground();
            if (cursor.MoveToFirst())
            {
                path = cursor.GetString(cursor.GetColumnIndex(
                    MediaStore.Images.Media.InterfaceConsts.Data));
            }
            cursor.Close();

                return path;

error is in

var loader = new CursorLoader(this, a, projection, null, null, null);

Don't mark this as duplicate please.

Raikoug
  • 347
  • 3
  • 16
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Vucko Jun 05 '16 at 20:30
  • Thanks for reply, but, this is a different thing. Uri is not null, as I wrote. The problem is in contentresolver but I don't know why. – Raikoug Jun 05 '16 at 23:07
  • I "solved" this issue putting the code of the class in mainclass. This way the uri was not passed, and worked very fine. The question is always the same, why the function gives that error when passing the uri? – Raikoug Jun 06 '16 at 22:13

0 Answers0