Saturday 9 November 2013

get files name from sdcard and show in gridview example

1)ViewGridImagesActivity.java
=====================

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

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;

public class ViewGridImagesActivity extends TopBarActivity implements
OnItemClickListener {
private ImageView  img_btn_back;
private GridView mGridViewTicket;
private List TicketList;
private GridAdapter mGridAdapter;
private File file;
// private String strFilePath;
// private String strMonthTitle;
// private String strTicket_Path;
public static String strFolderName;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_grid_images);

img_btn_back = (ImageView) findViewById(R.id.img_btn_bck);
img_btn_back.setOnClickListener(this);

// SharedPreferences mSPreferences = getSharedPreferences(
// com.rws.setting.Constants.SHAREDPREFERENCES,
// MODE_WORLD_READABLE);

Intent mIntent = getIntent();
Bundle mBundle = mIntent.getExtras();
if (mBundle != null) {
strFolderName = mBundle.getString("month_name");
}
// strFolderName = getIntent().getExtras().getString("MonthName");
// Toast.makeText(this, "FolderNAme=="+strFolderName,
// Toast.LENGTH_LONG).show();

mGridViewTicket = (GridView) findViewById(R.id.gridview_images);
// file = new File(Environment.getExternalStorageDirectory(),
// "ParkingAlarm" + File.separator + strFolderName);
TicketList = getFileListfromSDCard();
mGridAdapter = new GridAdapter(this, strFolderName);
mGridViewTicket.setAdapter(mGridAdapter);
}

@Override
public void onClick(View v) {
super.onClick(v);
switch (v.getId()) {
case R.id.img_btn_bck:

finish();
break;

default:
break;
}
}

@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {

}

private List getFileListfromSDCard() {
List flLst = new ArrayList();
File file = new File(
android.os.Environment.getExternalStorageDirectory(),
"ParkingAlarm/");

if (file.isDirectory()) {
for (File subFile : file.listFiles()) {
flLst.add(subFile.getAbsolutePath());
}
}
return flLst;
}
}
2)GridAdapter.java
=============
import java.io.File;
import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GridAdapter extends BaseAdapter implements OnThumbDeleteClick {

private LayoutInflater mLayoutInflator;
private Context mContext;
// public ImageLoader mImageLoader = null;
private ArrayList fileList;

public GridAdapter(Context mContext, String mothsel) {
// this.mImageLoader = new ImageLoader(mContext, R.drawable.ic_process,
// 1,
// false, CommanClass.getImageDIP(mContext, 85),
// CommanClass.getImageDIP(mContext, 85));
this.mContext = mContext;
this.mLayoutInflator = LayoutInflater.from(mContext);

fileList = new ArrayList();
File file = new File(
android.os.Environment.getExternalStorageDirectory(),
"ParkingAlarm/" + mothsel);

if (file.isDirectory()) {
for (File subFile : file.listFiles()) {
fileList.add(subFile.getAbsolutePath());
}
}

}

@Override
public int getCount() {
return fileList.size();
}

@Override
public Object getItem(int position) {
return fileList.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

public class ViewHolder {
ImageView thumb_nail, delete_thumb_img;
TextView txtTicket;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

ViewHolder mHolder;
if (convertView == null) {
convertView = mLayoutInflator.inflate(R.layout.grid_cell_image,
null);
mHolder = new ViewHolder();
mHolder.thumb_nail = (ImageView) convertView
.findViewById(R.id.img_thumbnail);

mHolder.delete_thumb_img = (ImageView) convertView
.findViewById(R.id.img_delete_thumbnail);

mHolder.txtTicket = (TextView) convertView
.findViewById(R.id.txtTicket);

convertView.setTag(mHolder);
} else {
mHolder = (ViewHolder) convertView.getTag();
}

mHolder.thumb_nail.setImageBitmap(BitmapFactory.decodeFile(fileList
.get(position)));
mHolder.delete_thumb_img.setOnClickListener(new OnThumbDeletelistiner(
this, position));

return convertView;
}

@Override
public void onThumbDelete(int postion) {
// Toast.makeText(mContext, "Clickpostion " + postion,
// Toast.LENGTH_SHORT).show();
DeleteVideo(postion);

}

// This function is used for create dialog for delete
@SuppressWarnings("deprecation")
private void DeleteVideo(final int pos) {
AlertDialog mAlertDialog = new AlertDialog.Builder(mContext).create();
mAlertDialog.setTitle(Constants.DELETE_DIALOG.DELETE_DIALOG_TITLE);
mAlertDialog.setIcon(R.drawable.tick_unselected);
mAlertDialog.setMessage(Constants.DELETE_DIALOG.DELETE_DIALOG_MESSAGE);
mAlertDialog.setCancelable(false);
mAlertDialog.setButton(
Constants.DELETE_DIALOG.DELETE_DIALOG_BUTTON_YES,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Delete_byID(pos);
}
});
mAlertDialog.setButton2(
Constants.DELETE_DIALOG.DELETE_DIALOG_BUTTON_NO,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
mAlertDialog.show();
}

public void Delete_byID(int pos) {
DeleteFile(fileList.get(pos));
// System.out.println("Path=="+fileList.get(pos));
fileList.remove(pos);
notifyDataSetChanged();
}

private void DeleteFile(String Path) {
File mFile = new File(Path);
if (mFile.exists()) {
mFile.delete();
// Toast.makeText(mContext,
// Html.fromHtml(Constants.DELETE_DIALOG.DELETE_FILE_MESSAGE),
// Toast.LENGTH_SHORT).show();
}
}

}
3)OnThumbDeletelistiner.java
====================import android.view.View;
import android.view.View.OnClickListener;

public class OnThumbDeletelistiner implements OnClickListener {
public interface OnThumbDeleteClick {
public void onThumbDelete(int postion);

}

private final int postion;
private final OnThumbDeleteClick mOnThumbDeleteClick;

public OnThumbDeletelistiner(OnThumbDeleteClick mOnThumbDeleteClick,
int position) {
this.mOnThumbDeleteClick = mOnThumbDeleteClick;
this.postion = position;
}

@Override
public void onClick(View v) {
if (mOnThumbDeleteClick != null)
mOnThumbDeleteClick.onThumbDelete(postion);
}
}
4)Utility.java
===========
import java.io.InputStream;
import java.io.OutputStream;

public class Utility {
    public static void CopyStream(InputStream mFileInputStream, OutputStream mOutputStream)
    {
        final int buffer_size=1024;
        try
        {
            byte[] bytes=new byte[buffer_size];
            while(true)
            {
              int count=mFileInputStream.read(bytes, 0, buffer_size);
              if(count==-1)
                  break;
              mOutputStream.write(bytes, 0, count);
            }
        }
        catch(Exception ex){}
    }
}
5)grid_cell_image.xml
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="76dp"
    android:layout_height="76dp" >

   
        android:id="@+id/img_thumbnail"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_margin="6dp"
        android:scaleType="fitXY"
        android:contentDescription="@string/app_name"
        android:maxHeight="70dp"
        android:maxWidth="70dp"
        android:src="@drawable/logo" />

   
        android:id="@+id/img_delete_thumbnail"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_alignParentRight="true"
        android:layout_margin="10dp"
        android:contentDescription="@string/app_name"
        android:src="@drawable/tick_unselected" />

   
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

       
            android:id="@+id/txtTicket"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/White" />
   

6)view_grid_images.xml
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg" >

   
        android:id="@+id/action_settings"
        android:layout_marginBottom="10dp"
        layout="@layout/topbar_share" />
   
        android:id="@+id/btton_line"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:drawableLeft="@drawable/bell_icon"
        android:drawablePadding="10dip"
        android:text="@string/parking_permit"
        android:textColor="@color/White" />
   
        android:id="@+id/midlle_contect"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/btton_line"
        android:layout_below="@id/action_settings"
        android:text="@string/app_name" >

       
            android:id="@+id/txt_thumbnail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:text="@string/thumbnail_tickets"
            android:textSize="24sp"
            android:textStyle="bold" />

       
            android:id="@+id/layout_back"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dp"
            layout="@layout/back_button" />

       
            android:id="@+id/gridview_images"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="10dip"
            android:layout_above="@id/layout_back"
            android:layout_below="@id/txt_thumbnail"
            android:layout_margin="10dp"
            android:cacheColorHint="@android:color/transparent"
            android:divider="@null"
            android:listSelector="@android:color/transparent"
            android:numColumns="4" >
       
   

No comments:

Post a Comment