Doubts about image routes

0

Inside the emulator, the path of an image appears to me:

  

/storage/emulated/0/Pictures/Screenshots/454980.png

But with the following code:

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK){
        path = data.getData();
        imagen.setImageURI(path);
    }
}

You get this route from the same image:

  

content: // media / external / images / media / 78

When trying to load the image with the previous path, the image is not visible and shows this error:

  

E / BitmapFactory: Unable to decode stream:   java.io.FileNotFoundException: /   content: / media / external / images / media / 78: open failed: ENOENT (Not such   file or directory)

I want to know why the method gives me a route which does not take me to the image I select.

With this code I try to load the image, I emphasize that the route obtained with the previous method I save it in database sqlite.

 private void buscar() {
    SQLiteDatabase db = mDbHelper.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT foto FROM " + Utilidades.Tabla_Contacto + " WHERE " + Utilidades.Campo_telef + " = '"+numero + "'",null);
    cursor.moveToFirst();
    Toast.makeText(this,cursor.getString(0),Toast.LENGTH_LONG).show();
    try{
        imagen.setImageURI(Uri.parse(cursor.getString(0)));

    }catch(Exception e){
        Toast.makeText(this,e+"",Toast.LENGTH_LONG).show();
    }

    db.close();
}
    
asked by Pedro Montenegro 25.04.2018 в 00:58
source

1 answer

0

First of all make sure you have the necessary permission to read the external directory path:

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

To get the correct Path, you must use the ContentResolver :

private String getPathFromUri(Uri uri){

    String[] filePathColumn = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContentResolver().query(uri,
                        filePathColumn, null, null, null);
    cursor.moveToFirst();
    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String imagePath = cursor.getString(columnIndex);
    cursor.close();

    return imagePath;

}

Your code should be more or less like this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK){
       path = data.getData(); // Path debe ser tipo Uri
       String imagePath = getPathFromUri(path); // Aqui te devuelve el path correcto como un String
       imagen.setImageURI(Uri.parse(imagePath)); // Conviertes el string a Uri para cargar la imagen
    }
}

More information: Link

Note: if you get warnings of possible cases of null, validate them to avoid them, greetings.

    
answered by 25.04.2018 / 10:38
source