hi sandhu,
by using this code you easily search and access  all file store in your sd
card with in your application
main.xml
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android";

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

<TextView

 android:id="@+id/path"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    />
<EditText
                android:id="@+id/tin"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"

                android:lines="1"
                android:inputType="text" />

<ListView

 android:id="@android:id/list"

 android:layout_width="fill_parent"

    android:layout_height="wrap_content"

 />

<TextView

 android:id="@android:id/empty"

 android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="No Data"

 />

</LinearLayout>
-------------------------------------------------------------------------------------------------------------------------------
row.xml
<?xml version="1.0" encoding="utf-8"?>

<TextView

  xmlns:android="http://schemas.android.com/apk/res/android";

  android:id="@+id/rowtext"

  android:layout_width="fill_parent"

     android:layout_height="25px"

     android:textSize="23sp" />
--------------------------------------------------------------------------------------------------------------------------------
main activity
package com.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class Test77Activity extends ListActivity {
     private List<String> item = null;
     private List<String> path = null;
     private String root="/sdcard";
     private TextView myPath;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
           // Intent intent=getIntent();

            setContentView(R.layout.main);
            myPath = (TextView)findViewById(R.id.path);
            getDir(root);
        }

        private void getDir(String dirPath)
        {
         myPath.setText("Location: " + dirPath);

         item = new ArrayList<String>();
         path = new ArrayList<String>();

         File f = new File(dirPath);
         File[] files = f.listFiles();

         if(!dirPath.equals(root))
         {

          item.add(root);
          path.add(root);

          item.add("../");
          path.add(f.getParent());

         }

         for(int i=0; i < files.length; i++)
         {
           File file = files[i];
           path.add(file.getPath());
           if(file.isDirectory())
            item.add(file.getName() + "/");
           else
            item.add(file.getName());
         }

         ArrayAdapter<String> fileList =
          new ArrayAdapter<String>(this, R.layout.row, item);
         setListAdapter(fileList);
        }

     @Override
     protected void onListItemClick(ListView l, View v, int position, long
id) {

      File file = new File(path.get(position));

      if (file.isDirectory())
      {
       if(file.canRead())
        getDir(path.get(position));
       else
       {
        new AlertDialog.Builder(this)
        .setIcon(R.drawable.ic_launcher)
        .setTitle("[" + file.getName() + "] folder can't be read!")
        .setPositiveButton("OK",
          new DialogInterface.OnClickListener() {

           public void onClick(DialogInterface dialog, int which){
            // TODO Auto-generated method stub
               dialog.dismiss();
           }
          }).show();
       }
      }
      else
      {
          Intent intent = new Intent();
          intent.setAction(Intent.ACTION_VIEW);
          Uri uri = Uri.parse("file://" + file.getPath());
          String fname=file.getName();

if(fname.endsWith(".jpeg")||fname.endsWith("png")||fname.endsWith("jpg")||fname.endsWith(".gif")||fname.endsWith(".txt"))
          {
              intent.setDataAndType(uri, "image/*");
              startActivity(intent);
          }
          else if(fname.endsWith(".mp4")||fname.endsWith(".3gp"))
          {
              intent.setDataAndType(uri, "video/*");
              startActivity(intent);
          }
          else if(fname.endsWith(".mp3"))
          {
              intent.setDataAndType(uri, "audio/*");
              startActivity(intent);
          }
          else
             try {
                 EditText tv = (EditText)findViewById(R.id.tin);
                  StringBuilder text = new StringBuilder();

                    BufferedReader br = new BufferedReader(new
FileReader(file));
                   String line;

                    while ((line = br.readLine()) != null) {
                       text.append(line);
                        text.append('\n');

                        //Set the text
                       tv.setText(text);

                    }
                }//try
                catch (IOException e) {
                    //You'll need to add proper error handling here
                }//catch

      }
     }
    }
------------------------------------------------------------------------------------------------------------------------------
android manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android";
    package="com.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".Test77Activity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"
/>
<uses-permission android:name="android.permission.READ_CONTACTS" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
/>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to