So i am using retrofit to upload images to our server, and am using gallery chooser to choose the image. I am trying to get the path of the image, because uri.getPath() returns file not found exception. I have literally seen almost every stackoverflow article on this subject, and still have no answer. The following code that I have is similar to everything I have seen online, and it ALWAYS returns null, and I have no idea why. PLEASE HELP
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
try {
android.net.Uri selectedImage = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage);
// Log.d(TAG, String.valueOf(bitmap));
ImageView imageView = (ImageView) findViewById(R.id.expandedProfilePic);
imageView.setImageBitmap(bitmap);
String[] filePathColumn = {MediaStore.Images.Media.DATA};
android.database.Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor == null)
return;
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
// is always null
System.out.println("FILE PATH " + filePath);
} catch (IOException e) {
e.printStackTrace();
}
}
}