Android: Implement Universal Library Image Loader

2

What I'm trying to do is, that when I select images from my class PictureActivity I send them to AsyncTask that is in my UploadImage class but I'm also directly adding them to the CheckIn object (getImages) that is in my class CheckIn and when that process is finished PictureActivity sends me to my class where it is shown that IndexActivity is what happens when I select the image to delete it, the AsyncTask has not finished the process yet, I was investigating the way that when I launch to my activity where I see the images (IndexActivity) the user visualize the process of loading the images and I found the library Universal Image Loader , I want to implement it in my application but I have problems understanding how to do it in my code and I do not understand the documentation very well, I hope and you can help me.

  

IndexActivity.java

CheckIn checkIn = CheckIn.getInstance();//creo una instancia de CheckIn
ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
    if (checkIn.getImages().size() > 0) {//valido de que existan imagenes
        for (ImageData object: checkIn.getImages()) {
            bitmaps.add(object.getBitmap());//al ArrayList le agrego los Bitmaps
        }//for
        gridView.setAdapter(new ImageAdapter(this, bitmaps));//seteo las imagenes al gridView
    }//./if
  

PictureActivity.java

CheckIn checkIn = CheckIn.getInstance();//TODO YA NO

                    for (int i = 0; i < principalListOfImages.size(); i++) {
                        ImageData imageData = new ImageData();//creo una instancia de ImageData
                        Bitmap bitmap = BitmapFactory.decodeFile(principalListOfImages.get(i));//paso el path a Bitmap
                        imageData.setBitmap(bitmap);//le setteo el bitmap
                        imageData.setPath(principalListOfImages.get(i));//le setteo el path
                        checkIn.getImages().add(imageData);//recupero mi arreglo de objetos y le setteo mi nuevo objeto
                        listOfBitmaps.add(bitmap);//cada BItmap lo setteo al arraylist
                    }//for

                    new UploadImage().execute(listOfBitmaps);
  

UploadImage.java (AsyncTask)

@Override
protected Void doInBackground(Object... params) {//ejecuta nuestras tareas principales
    CloseableHttpClient httpClient;
    CloseableHttpResponse httpResponse;

    try {

        CheckIn checkIn = CheckIn.getInstance();//hago una instancia de checkin
        JSONObject jsonImage = new JSONObject();//creo un JSONObject

        ArrayList<Bitmap> listOfBitmaps = (ArrayList<Bitmap>) params[0];//Obtengo mi Arreglo de objetos para despues pasarlos a un array lis de Bitmap
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();//permitira la salida de los bytes

        for (int i = 0; i < listOfBitmaps.size(); i++) {//itero el arreglo

            listOfBitmaps.get(i).compress(Bitmap.CompressFormat.JPEG, 70, byteArrayOutputStream);//se comprime la imagen
            byte[] byteArray = byteArrayOutputStream.toByteArray();//codifica el path a un arreglo de byte
            String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);//codifico ese byteArray a base64 y despues a scrintg

            jsonImage.put("folio", checkIn.getFolio());//paso el folio al JSONObject
            jsonImage.put("images", encoded);//le paso al JSONObject los codigos de las iameges

            StringEntityHC4 entityHC4 = new StringEntityHC4(jsonImage.toString(), ContentType.create("json/application", "UTF-8"));//la direccion del servidor a la que va a apuntar
            HttpPutHC4 httpPutHC4 = new HttpPutHC4(DynamicUrl.BASE_URL + DynamicUrl.SERVER_HOST + ":" + DynamicUrl.SERVER_PORT + "/api/checkkin");//la direccion del dervidor al que va a apuntar

            httpPutHC4.setEntity(entityHC4);//seteo los datos que tengo en el JSONObject

            httpClient = HttpClients.createDefault();//La configuracion del servidor va a ser default
            httpResponse = httpClient.execute(httpPutHC4);//Obtengo la respuesta del servidor

            if (httpResponse.getStatusLine().getStatusCode() == 200) {//si el estatus de la respuesta es igual a 200
                JSONObject jsonRosponse = new JSONObject(EntityUtilsHC4.toString(httpResponse.getEntity()));//creo un JSONObject y le paso el JSON que recibio de la sespuesta del servidor

                if (jsonRosponse.getString("code").equals("OK")) {//checo que el JSON tenga la clave OK
                    System.out.println("Las imagenes se guardaron correctamente");
                } else {
                    System.out.println("Error al subir las imagenes");
                }
            } else {
                System.out.println("Ocurrio un error");
            }
        }//./for
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
    
asked by Javier fr 21.12.2016 в 20:25
source

0 answers