I am creating an app which uploads a selected image from the gallery and uploads it to a web service. The webservice requires the filename of selected image plus a base64 encoding of the file contents. I have managed to achieve this with a hardcoded file path. However, I am struggling to get the real filepath of the image. I have read around the web and have this code, but it does not work for me:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
String[] projection = {MediaStore.Images.Media.DATA};
try {
Cursor cursor = getContentResolver().query(selectedImageUri, projection, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Log.d("Picture Path", picturePath);
}
catch(Exception e) {
Log.e("Path Error", e.toString());
}
}
}
I get this error:
java.lang.NullPointerException
EDIT
Forgot to mention I am using Kitkat. It looks like my problem is KitKat related. I found this (see below) which helped me get my app working:
Android Gallery on KitKat returns different Uri for Intent.ACTION_GET_CONTENT
Try this:
And
Try this,
Use below code,
and below is your getRealPathFromURI function,
Hope this helps you
If you are like me and you dont have a local file ready, for instance you want to give user a chance to take a picture and then upload the picture this is how to do it. I am a noob in java but i have allot of experience in coding. Stackoverflow helped me allot so now it is my time to do something back.
Under class activity you have to declare these two items. asuming you have a working upload class and php script.
And the rest i will leave up to u, if you got this far, I am pretty sure you will finish.
A little late to the party but here's my code, hope this helps.