Monday, February 15, 2016

Gallery App to display all images from SD card. Part 1

Hey friends,

It's 4:00 PM now  and I have roughly couple more hours before I start my new Android project. So, during this time, I wanted to discuss about my previous project, the gallery app. I have struggled to write the code for a gallery app because of two reasons. First of all it's been like two years without full fledged Android development. Secondly, the tutorials we find online are very rudely specific to resource drawable image galleries. I was like "Seriously? Who would want to see existing non-editable picture gallery?" I mean, don't get me wrong. Unless you wanted an app that showed art-work of Picasso or Leonardo da Vinci, resource drawable images are the way to go.

Then again, we all know that we want to see that selfie we took with Monica or that lucky click of our dog Casper chasing a butterfly or... you know OUR moments. And yes, we want to see it in our app. An app made in our laptop with our shared knowledge. If you are interested like I am, let's proceed.

The MAIN activity

package com.example.horopter.gallery;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;


public class MainActivity extends AppCompatActivity
{
    GridView lv;
    TextView tv;
    Context context;
    String file;
    ArrayList paths;
    ArrayList imageNames;
    ArrayList listFile;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        paths = new ArrayList<>();
        imageNames = new ArrayList<>();
        if (!Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED))
        {
            Toast.makeText(this, "Error! No SDCARD Found!", Toast.LENGTH_LONG)
                    .show();
        }
        else
        {
            file = "/storage/sdcard1/Images";//Environment.getExternalStorageDirectory().getAbsolutePath();
            listFile = new ArrayList<>();
            Toast.makeText(this,String.valueOf(file),Toast.LENGTH_LONG).show();
            Log.d("Santosh", String.valueOf(file));
        }
        listf(file,listFile);
        getImages(listFile);
        context = this;
        lv = (GridView) findViewById(R.id.lv1);
        tv = (TextView) findViewById(R.id.tv1);
        tv.setText(tv.getText()+" ("+listFile.size()+" files)");
        ScrollAdapter ga = new ScrollAdapter(this, paths, imageNames);
        lv.setAdapter(ga);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView parent, View view,
                                    int position, long id) {
                Intent i = new Intent(MainActivity.this, ImageViewer.class);
                i.putExtra("filepath",paths);
                i.putExtra("filename", imageNames);
                i.putExtra("position", position);
                startActivity(i);
            }

        });
    }
    public void listf(String directoryName, ArrayList files)
    {
        File directory = new File(directoryName);
        File[] fList = directory.listFiles();
        if(fList!=null)
        for (File file : fList)
        {
            if (file.isFile())
            {
              if(file.getName().endsWith(".jpg")||file.getName().endsWith(".jpeg")||file.getName().endsWith(".png")||file.getName().endsWith(".gif"))
                files.add(file);
            }
            else if (file.isDirectory())
            {
                listf(file.getAbsolutePath(), files);
            }
        }
    }
    public void getImages(ArrayList file)
    {
        for (File f : file)
            {
                paths.add(f.getAbsolutePath());
                imageNames.add(f.getName());
            }
    }
}

The MAIN layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.horopter.listgallery.MainActivity">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="25dp"
        android:text="Gallery" />
    <GridView
        android:id="@+id/lv1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/tv1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:stretchMode="columnWidth"
        android:numColumns="auto_fit"
        android:drawSelectorOnTop="true"
        android:columnWidth="100dp"
        android:fastScrollEnabled="true"
        android:isScrollContainer="true">
    </GridView>
</RelativeLayout>

[Draft update:] I think I shouldn't be doing this while I am working. I will explain the code next time. [4:21 PM] 

No comments:

Post a Comment