How to create a GridView with gallery images?

0

I'm doing a APP working with images, I'm saving the images in the gallery to a folder named imagensAPP like this:

SaveImage:

public File saveImage() {
    int imageNum = 0;

    //SALVANDO EM IMAGENSAPP . . .
    File imagesFolder = new File(Environment.getExternalStorageDirectory()+File.separator+"DCIM", "imagensAPP");  
    imagesFolder.mkdirs();
    //NOME DA IMG . . .
    String fileName = "Img_" + String.valueOf(imageNum) + ".png"; 
    File output = new File(imagesFolder, fileName);
    while (output.exists()){
        imageNum++;
        fileName = "Img_" + String.valueOf(imageNum) + ".png";
        output = new File(imagesFolder, fileName);
    }
    try {
        Bitmap bitmap = imagemFinalizada;
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 40, bytes);
        FileOutputStream fo = new FileOutputStream(output);
        fo.write(bytes.toByteArray());
        fo.flush();
        fo.close();
        MediaScannerConnection.scanFile(Teste.this, new String[]{output.getAbsolutePath()}, null, null);

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return output;
}

By GridView or otherwise, how can I pull these images to show within APP ?

    
asked by anonymous 12.12.2016 / 13:14

1 answer

0

After a bit of research, I was able to solve it this way.

I used the GridView available to download from this source: link

To pull the images from the gallery, I did this:

Attributes:

Bitmap auxbm = null; //guarda a imagem da galeria.
private static final String path = "/storage/emulated/0/DCIM/imagensAPP"; //caminho da galeria.
private int cont = 0; // controla quantidade de imagens da galeria.
private int verificador = 0; //informa se pegou todas imagens.

Take the Images:

 private Bitmap loadImageFromStorage() {
    try {
        File f=new File(path, "Img_" + cont +".png"); //PATH = caminho da imagem  ##  "HoloImg_" + cont +".png" = nome da imagem.
        auxbm = BitmapFactory.decodeStream(new FileInputStream(f)); //AUXBM = Bitmap que recebe a imagem. 
        return auxbm;
    }
    catch (FileNotFoundException e){
        e.printStackTrace();
    }
    return auxbm;
}

To add in GridView , I changed the getData() method of the example mentioned in the link leaving it this way.

Modifying getData:

private ArrayList<ImageItem> getData() {
    final ArrayList<ImageItem> imageItems = new ArrayList<>();
    TypedArray imgs = getResources().obtainTypedArray(R.array.image_ids);

    while(verificador == 0){ 
        Bitmap atual = loadImageFromStorage(); //Pega img.
        auxbm = null; 
        if(atual == null){ verificador = 1;} //se img não existe, finaliza.
        else{//se existe.
            imageItems.add(new ImageItem(atual, "ImagePick#" + cont));//add ela e seu nome na lista.
            cont++;
        }
    }
    return imageItems;
}
    
13.12.2016 / 15:26