1
I’m making an application where I search an image in the Android Nougat gallery and send it to a server. However I am not able to recover Uri with the image address to send.
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 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: http://www.journaldev.com/13270/android-capture-image-camera-gallery#comment-39227
When the image capture is done by the camera it is possible to get the path of Uri correctly, but when it is by Gallery the value in Uri is 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 some help to solve this problem.
Please see this link https://developer.android.com/training/permissions/requesting.html?hl=pt-br
– Thiago Luiz Domacoski
Voce needs to request permission at runtime
– Thiago Luiz Domacoski