First Add permission for external storage in Manifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.rajeshvijayakumar.fldemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="org.rajeshvijayakumar.fldemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
flist.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
fl_list_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/fname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
MainActivity.java
package org.rajeshvijayakumar.fldemo;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity implements OnItemLongClickListener {
private ListView mListView;
private List<String> fileNameList;
private FlAdapter mAdapter;
private File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.flist);
mListView = (ListView) findViewById(R.id.listView1);
file = Environment.getExternalStorageDirectory();
fileNameList = getFileListfromSDCard();
mAdapter = new FlAdapter(this, R.layout.fl_list_item, fileNameList);
mListView.setAdapter(mAdapter);
}
private List<String> getFileListfromSDCard() {
String state = Environment.getExternalStorageState();
List<String> flLst = new ArrayList<String>();
if (Environment.MEDIA_MOUNTED.equals(state) && file.isDirectory()) {
File[] fileArr = file.listFiles();
int length = fileArr.length;
for (int i = 0; i < length; i++) {
File f = fileArr[i];
flLst.add(f.getName());
}
}
return flLst;
}
public class FlAdapter extends ArrayAdapter<String> {
private List<String> fLst;
private Context adapContext;
public FlAdapter(Context context, int textViewResourceId,
List<String> fLst) {
super(context, textViewResourceId, fLst);
this.fLst = fLst;
adapContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
FHolder fHolder = null;
if (convertView == null) {
view = View.inflate(adapContext, R.layout.fl_list_item, null);
fHolder = new FHolder();
fHolder.fNameView = (TextView) view.findViewById(R.id.fname);
view.setTag(fHolder);
} else {
fHolder = (FHolder) view.getTag();
}
String fileName = fLst.get(position);
fHolder.fNameView.setText(fileName);
return view;
}
}
static class FHolder {
public TextView fNameView;
}
}
Output :
Source : Coming Soon to Github.............
your examples are outstanding plain and simple!!!
ReplyDeletemany thanks.
Your code is working good.
ReplyDeletebut how can I make listItem clickable and onClick how can I go to inner directory?
i want to display only .mp3 files
ReplyDelete