Search code examples
androidgridviewandroid-gallery

Opening images saved inside app folder


Relatively new to android and beginner with Gallery functionality . Having said that, In my app, I am saving images inside gallery folder (Gallery/MyAppFolder). I am displaying these folder inside a GridView. Till here it is working fine as expected. Now I am trying to implement a click listener on gridview which should display that specific image. Below is the code I am trying in my activity to achieve the said objective

public String FolderPath = Environment.getExternalStorageDirectory().getPath() + "/MyAppFolder";
private void setGridAdapter(String path) {
    // Create a new grid adapter
    gridItems = createGridItems(path);
    MyGridAdapter adapter = new MyGridAdapter(this, gridItems);

    // Set the grid adapter
    GridView gridView = (GridView) findViewById(R.id.gridView);
    gridView.setAdapter(adapter);

    // Set the onClickListener
    gridView.setOnItemClickListener(this);
}
private List<GridViewItem> createGridItems(String directoryPath) {
    List<GridViewItem> items = new ArrayList<GridViewItem>();

    // List all the items within the folder.
    files = new File(directoryPath).listFiles(new ImageFileFilter());

    for (File file : files) {

        // Add the directories containing images or sub-directories
        if (file.isDirectory()
                && file.listFiles(new ImageFileFilter()).length > 0) {

            items.add(new GridViewItem(file.getAbsolutePath(), true, null));
        }
        // Add the images
        else {
            Bitmap image = BitmapHelper.decodeBitmapFromFile(file.getAbsolutePath(),
                    50,
                    50);
            items.add(new GridViewItem(file.getAbsolutePath(), false, image));
        }
    }

    return items;
}

private boolean isImageFile(String filePath) {
    if (filePath.endsWith(".jpg") || filePath.endsWith(".png") || filePath.endsWith(".pdf"))
    // Add other formats as desired
    {

        return true;
    }
    return false;
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    if (gridItems.get(position).isDirectory()) {
        setGridAdapter(gridItems.get(position).getPath());
    }
    else {


  Intent intent = new Intent();
  intent.setType("image/*");
  intent.setAction(Intent.ACTION_GET_CONTENT);
  startActivityForResult(Intent.createChooser(intent,"Select Picture"),SELECT_PICTURE);

        Log.e("Path for clicked Item::",gridItems.get(position).getPath());


          }

}

private class ImageFileFilter implements FileFilter {

    @Override
    public boolean accept(File file) {
        if (file.isDirectory()) {
            return true;
        }
        else if (isImageFile(file.getAbsolutePath())) {
            return true;
        }
        return false;
    }
}

I have tried using intent and onActivityResult method but of no success. This is the solution I have followed for setting up my gridview and showing folder inside my app. Can you guide me what to do in onClick Listener in order to open image directly in my layout. I don't want to go through gallery and show images.


Solution

  • Create new Activity for full screen view or fragment and pass the image path in arguments or extra and just show this image on this activity. Also you should check this library Picasso. It can make work with images much easier.