FilesListScreen.java:-
package com.dharani.android.snailmail.PL;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.dharani.android.snailmail.R;
import com.dharani.android.snailmail.SharedVariables;
import com.dharani.android.snailmail.BL.BLCommonOperations;
import com.dharani.android.snailmail.BL.BLFileOperations;
import com.dharani.android.snailmail.DL.Files.clsFile;
public class FilesListScreen extends Activity{
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK)
{
System.out.println("-------uuu------");
finish();
return super.onKeyDown(keyCode, event);
}
return super.onKeyDown(keyCode, event);
}
Dialog dialog;
Activity activity;
Button fileCloseBtn;
//private static final int DIALOG_LOAD_FILE = 1000;
BLCommonOperations objBLCommonOperations=null;
Dialog mDialog;
String cameraimageFullLocalPath;
private Item[] fileList;
FileListAdapter adapter;
private String chosenFile;
clsFile objfile;
ListView listView;
private File path = new File(Environment.getExternalStorageDirectory() + "");
// Check if the first level of the directory structure is the one showing
private Boolean firstLvl = true;
ArrayList<String> str = new ArrayList<String>();
BLFileOperations objBLFileOperations=null;
List<clsFile> filesList=new ArrayList<clsFile>();/*,temporaryFilesList*/
//FilesListScreenBroadCastReciever receiver;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.add_file_list);
getAllIds();
activity=FilesListScreen.this;
objBLCommonOperations=new BLCommonOperations();
objBLFileOperations=new BLFileOperations();
// Dialog for Selecting Image...
// Select Image Dialog...
System.out.println("---------dialog---------");
loadFileList();
listView.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int which, long id) {
// do your operation here
System.out.println("----i="+which+"---l="+id);
System.out.println("-----DIALOG_LOAD_FILE-----");
chosenFile = fileList[which].file;
File sel = new File(path + "/" + chosenFile);
if (sel.isDirectory()) {
firstLvl = false;
// Adds chosen directory to list
str.add(chosenFile);
fileList = null;
path = new File(sel + "");
loadFileList();
listView.setAdapter(adapter);
}
// Checks if 'up' was clicked
else if (chosenFile.equalsIgnoreCase("up") && !sel.exists()) {
// present directory removed from list
String s = str.remove(str.size() - 1);
// path modified to exclude present directory
path = new File(path.toString().substring(0,
path.toString().lastIndexOf(s)));
fileList = null;
// if there are no more directories in the list, then
// its the first level
if (str.isEmpty()) {
firstLvl = true;
}
loadFileList();
listView.setAdapter(adapter);
}
// File picked
else {
Here we reached this blok finally after selecting a file, means ur selecting .jpg/.pdf/.xl file and its not Directory..Then after selecting it do ur wanted action..Below i just did something i want to do it in my Project..
// Perform action with file picked String[] tokens =fileList[which].file.split("\\."); String fileextension=tokens[tokens.length-1]; System.out.println("*******FileTitle*******"+fileextension); if(objBLCommonOperations.isFileFormatSupported(fileList[which].file)){ objfile=new clsFile((long)0 , objBLCommonOperations.imagenamewithdate(), objBLCommonOperations.imagenamewithdate()+"."+fileextension, path.getAbsolutePath()+"/"+fileList[which].file, 0, false, false, false) ; //if file not exists in DB then insert it in DB.. if(!objBLFileOperations.checkFileLocalPathExistsInDB(path.getAbsolutePath()+"/"+fileList[which].file)){ objBLFileOperations.insertFileInDB(objfile); //for passing the file path to inflate the list adapter Intent i=new Intent(SharedVariables.globalContext.getString(R.string.addSelectedFileToList)); Bundle b=new Bundle(); b.putParcelable("fileObject",objfile); i.putExtras(b); SharedVariables.AddJobScreenActivity.sendBroadcast(i); finish(); }else{ //get credit card object from DB having given file path.. objfile=objBLFileOperations.getFileObjWithLocalPathInDB(path.getAbsolutePath()+"/"+fileList[which].file); //for passing the file path to inflate the list adapter Intent i=new Intent(SharedVariables.globalContext.getString(R.string.addSelectedFileToList)); Bundle b=new Bundle(); b.putParcelable("fileObject",objfile); i.putExtras(b); SharedVariables.AddJobScreenActivity.sendBroadcast(i); finish(); } }else{ SharedVariables.showAlertMessage("Sorry,File not supported", FilesListScreen.this); //Intent i=new Intent(SharedVariables.globalContext.getString(R.string.showDialogForNotSupportedFile)); //SharedVariables.AddJobScreenActivity.sendBroadcast(i); //finish(); } }//else..
}
});
}//onCreate()....
private void getAllIds() {
listView=(ListView)findViewById(R.id.listView);
fileCloseBtn=(Button)findViewById(R.id.fileCloseBtn);
fileCloseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
private void loadFileList() {
try {
path.mkdirs();
} catch (SecurityException e) {
}
// Checks whether path exists
if (path.exists()) {
FilenameFilter filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
File sel = new File(dir, filename);
// Filters based on whether the file is hidden or not
return (sel.isFile() || sel.isDirectory())
&& !sel.isHidden();
}
};
String[] fList = path.list(filter);
fileList = new Item[fList.length];
for (int i = 0; i < fList.length; i++) {
fileList[i] = new Item(fList[i], R.drawable.file_icon);
// Convert into file path
File sel = new File(path, fList[i]);
// Set drawables
if (sel.isDirectory()) {
fileList[i].icon = R.drawable.directory_icon;
Log.d("DIRECTORY", fileList[i].file);
} else {
Log.d("FILE", fileList[i].file);
}
}
if (!firstLvl) {
Item temp[] = new Item[fileList.length + 1];
for (int i = 0; i < fileList.length; i++) {
temp[i + 1] = fileList[i];
}
temp[0] = new Item("Up", R.drawable.directory_up);
fileList = temp;
}
} else {
}
adapter=new FileListAdapter(this,fileList);
System.out.println("----array size-----"+fileList.length);
listView.setAdapter(adapter);
}// loadFileList()
private class Item {
public String file;
public int icon;
public Item(String file, Integer icon) {
this.file = file;
this.icon = icon;
}
@Override
public String toString() {
return file;
}
}
public class FileListAdapter extends BaseAdapter
{
ViewHolder holder = null;
Item[] fileList;
@Override
public int getCount() {
int count=0;
try {
count=fileList.length;
System.out.println("----lengthg--"+count);
} catch (Exception e) {
e.printStackTrace();
}
return count;
}
Activity context;
public FileListAdapter(Activity context,Item[] fileList) {
super();
this.context = context;
this.fileList=fileList;
}
class ViewHolder {
TextView folderTxtViw;
ImageView folderImage;
}
public View getView(final int position, View convertView, ViewGroup parent){
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.add_file_list_row, null, true);
holder = new ViewHolder();
holder.folderTxtViw=(TextView) rowView.findViewById(R.id.folderTxtViw);
holder.folderImage=(ImageView) rowView.findViewById(R.id.folderImage);
rowView.setTag(holder);
}
else
{
holder = (ViewHolder) rowView.getTag();
}
holder.folderTxtViw.setText(fileList[position].file);
holder.folderImage.setImageResource(fileList[position].icon);
return rowView;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Object getItem(int position) {
return fileList[position];
}
}
//implementing the broadcast reciever to recieve the broadcasted intents
/* class AddJobBroadCastReciever extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
}
}*/
@Override
protected void onPause(){
super.onPause();
System.out.println("-----onPause------");
}
@Override
protected void onResume(){
super.onResume();
System.out.println("-----onResume------");
}
//Written by Pavan Tilak(15-11-2012)
@Override
protected void onDestroy() {
super.onDestroy();
objBLCommonOperations=null;
objBLFileOperations=null;
}
}
add_file_list.xml:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<RelativeLayout android:layout_height="40dip"
android:layout_width="fill_parent"
android:background="@color/blueColor"
>
<Button android:id="@+id/fileCloseBtn"
android:layout_height="35dip"
android:layout_width="35dip"
android:layout_centerVertical="true"
android:background="@drawable/close_new_button" />
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Choose your file"
android:layout_centerInParent="true"
android:paddingLeft="4dip"
android:layout_toRightOf="@+id/fileCloseBtn"
android:textSize="20dip" />
</RelativeLayout>
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="3dip"
android:background="@color/White"
/>
</LinearLayout>
add_file_list_row.xml:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="6dip"
android:orientation="vertical" >
<ImageView
android:id="@+id/folderImage"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/discover" />
<TextView
android:id="@+id/folderTxtViw"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/folderImage"
android:layout_marginLeft="8dip"
android:text="Choose your file"
android:textSize="20dip" />
</RelativeLayout>
See also:-
http://android-er.blogspot.in/2010/01/implement-simple-file-explorer-in.html
http://android-er.blogspot.in/2012/07/example-of-file-explorer-in-android.html
http://android-er.blogspot.in/2012/07/sort-directoryfile-in-alphabetical.html
Comments
Post a Comment