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.