I am making an application where I look for an image in the Android Nougat gallery and send it to a server. But I can not retrieve Uri with the image address to be sent.
if(camera.equals("galeria")){
startActivityForResult(getPickImageChooserIntentGaleria(), 150);
}
else{
startActivityForResult(getPickImageChooserIntentGaleria(), 200);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap bitmap;
if (requestCode == 200) {
ImageView imageView = (ImageView) findViewById(R.id.iv_img_imagem);
if (getPickImageResultUri(data) != null) {
picUri = getPickImageResultUri(data);
try {
myBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), picUri);
myBitmap = rotateImageIfRequired(myBitmap, picUri);
myBitmap = getResizedBitmap(myBitmap, 500);
imageView.setImageBitmap(myBitmap);
} catch (IOException e) {
e.printStackTrace();
}
} else {
bitmap = (Bitmap) data.getExtras().get("data");
myBitmap = bitmap;
imageView.setImageBitmap(myBitmap);
}
}
if (requestCode == 150) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.iv_img_imagem);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
try {
Uri originalUri = data.getData();
String pathsegment[] = originalUri.getLastPathSegment().split(":");
String id = pathsegment[0];
final String[] imageColumns = { MediaStore.Images.Media.DATA };
Uri uri = getUri();
Cursor imageCursor = this.getContentResolver().query(uri, imageColumns, MediaStore.Images.Media._ID + "=" + id, null, null);
if (imageCursor.moveToFirst()) {
String value = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
picUri = Uri.parse(value);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private Uri getUri() {
String state = Environment.getExternalStorageState();
if(!state.equalsIgnoreCase(Environment.MEDIA_MOUNTED))
return MediaStore.Images.Media.INTERNAL_CONTENT_URI;
return MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}
For requestCode == 150 I get the following error message:
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-VINHOS.jpg: open failed: EACCES (Permission denied)
And for the requestCode == 200 I get the following error message:
java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference
I'm using this project as a base: link
When the image is captured by the camera it is possible to get the Uri path correctly, but when it is in the Gallery the value in Uri looks like this:
picUri = content://media/external/images/media/5168
picUri.getPath() = /external/images/media/5168
I need to receive Uri in this format
/storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-VINHOS.jpg
I would like help solving this problem.