Android: image path to Bitmap

1

I have a problem, in a JSON I bring some data and images, what I want to do is to pass those image routes to Bitmaps to be set to GridView, my question is how did I pass the image route to Bitmap. This is my JSON

  

{      'code': 'OK',      'data':{         'folio': 12345,         'customer': 'Jose Trinidad Ramos Trejo',         'delivered': '2016/10/30',         'images': [         ' link ', ' link ',' link '         ]      }   }

If you copy the route of the images you will take it to her

    
asked by Javier fr 12.12.2016 в 20:50
source

2 answers

1

As I answered in the previous question you can call this function

public static Bitmap getBitmapFromURL(String url_image) {
    try {
        URL url = new URL(url_image);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        Log.e("Bitmap","returned");
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        Log.e("Exception",e.getMessage());
        return null;
    }
}

And to this function you send the images that you get in your json

Bitmap bitMapFromJson = getBitmapFromURL(json.getString('images'));
    
answered by 12.12.2016 / 20:58
source
1

To obtain the image routes, according to your structure you have an object and another one that contains an array called images , for this you must access the Array and to store the image url you can create an arraylist:

  String contenidoJson = "{ 'code':'OK', 'data':{ 'folio':12345, 'customer':'Jose Trinidad Ramos Trejo', 'delivered':'2016/10/30', 'images':[ 'http://kuixx.com/wp-content/uploads/2015/01/Paisajes-5.jpg','http://www.paisajesbonitos.org/wp-content/uploads/2015/12/paisajes-bonitos-de-primavera-verde-lago-agua-natural-animales.jpg','https://wallpapershqpacks.files.wordpress.com/2013/04/049.jpg' ] } }";

        //crea lista para almacenar imagenes.
        List<String> listaImagenes = new ArrayList<String>();

        try {
            JSONObject mainObject = new JSONObject(contenidoJson);
            JSONObject objectData = mainObject.getJSONObject("data");
            JSONArray imagenes = objectData.getJSONArray("images");
            for (int i=0; i < imagenes.length(); i++) {
              //Agrega imagenes.
              listaImagenes.add(imagenes.get(i).toString());
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

Having your list of images you can get the bitmap from the url by using this method :

public static Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        // Log exception
        return null;
    }
}

by getting the bitmap you can add it in ImageView without problem.

    
answered by 12.12.2016 в 23:15