How to create a Gridview with gallery images?

Asked

Viewed 137 times

0

I’m making a APP who works with images, I’m saving the images in a gallery folder called imagensAPP in this way:

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;
}

For GridView or otherwise, how can I pull these images to show inside the APP?

1 answer

0


After researching a little, I managed to solve this way.

I used the exeplo of GridView available for download from this source: http://stacktips.com/tutorials/android/android-gridview-example-building-image-gallery-in-android

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

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.

Retrieve 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 to GridView, changed the method getData() 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.