Obtain path of a selected image from the file browser

0

I need to obtain the path where the image is saved when the user selects the image, I have this code:

// Llamada al explorador de archivos
public void elegirFoto(View vista){
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    intent.setType("image/*");
    startActivityForResult(intent.createChooser(intent, "Elige una aplicación"),COD_SELECCIONA);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode){
        case COD_SELECCIONA:
            myPath = data.getData();
            Toast.makeText(this, String.valueOf(myPath), Toast.LENGTH_LONG).show();
            break;
    }
}

//Después guardo la ruta en la Base de Datos

I get a result like:

  

content: //com.android.providers.media.documents/document/image%643123

But I need something like this:

  

/storage/emulated/folder/folder/folder/image_name.jpg

Since the first way to save it in the database and show it later in image view does not find the route and the second way if, the way I show the image then is like this:

resultado = BaseDeDatos.rawQuery("select img from artics where codigo = '"+codigoR+"'", null);
path = (resultado .getString(0));
bitmap = BitmapFactory.decodeFile(path);
imgProducto.setImageBitmap(bitmap);
    
asked by Humberto Arciniega 22.09.2018 в 18:12
source

1 answer

-1

Hello here I leave you an example of code with which you can get the path as you are looking for and also the bitmap

super.onActivityResult(requestCode, resultCode, data);
   switch (requestCode){
      case COD_SELECCIONA:
         if (resultCode == RESULT_OK) {
            Uri uri = data.getData();
            String[] projection = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(projection[0]);
            String path = cursor.getString(columnIndex);
            cursor.close();

            bitmap = BitmapFactory.decodeFile(path);
            imgProducto.setImageBitmap(bitmap);
         }
         break;
      default:
         break;
   }
}
    
answered by 22.09.2018 в 18:44