I can not get the Uri on some devices


I just migrated my code from sdk 22 to sdk 23, and in the test cell (moto g3) I had no problems, but it turns out that there are devices that present problems (Samsung Galaxy J2) ... Before when I used compileSdkVersion = 22, both phones worked, but now that I use 23, the samsung falls ...

At the code level I try to get a getPath from a Bitmap.

First I get the getUri of the bitmap, to then use it and get the getPath, in the following way:

Bitmap bm = ((BitmapDrawable) mImagenView.getDrawable()).getBitmap();
Uri tempUri = getImageUri(getApplicationContext(), bm);    //aca se caé


public Uri getImageUri(Context inContext, Bitmap inImage) {
    if(verificarPermisoAlmacenamiento()) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.PNG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);    //especificamente aqui
        return Uri.parse(path);

here path is null ... on the samsung.

Is it due to some change in the C library: ..... \ Android \ sdk \ sources \ android-23 \ android \ provider?

ALL help is welcome ...

asked by Maguz 19.08.2016 в 00:15

2 answers


Your problem is to get the path of the image:

String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);

If we review the documentation we find that:


insertImage () You can return null if the image had a problem to be   stored.

Firstly, make sure you have the permission:

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Remember that if your device uses Android 6.0 you should require permissions , this can also be a cause for which you get a value null .

You can add this code segment to require permissions on Android 6.0 devices

 int permissionCheck = ContextCompat.checkSelfPermission(
            this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        Log.i("Mensaje", "No se tiene permiso para escribir.");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 225);
    } else {
        Log.i("Mensaje", "Se tiene permiso!");
answered by 19.08.2016 / 02:37


Since I could not use GetImageUri () I was searching and it turns out that the Uri is obtained from the Intent

correct answer here

answered by 19.08.2016 в 17:27