0

i wanted to show only .xml files to user which is currently present in the sd card.i try the following but it shows all files in my sdcard including directories

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("text/xml");
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    startActivityForResult(Intent.createChooser(intent, "Select XML File"), SelectXMLFILE);
  • http://stackoverflow.com/questions/14626433/android-browse-file-intent-with-defined-extension `There is no guarantee that any "file manager" will honor your MIME type; they may elect to display all files anyway` – Selvin Apr 03 '13 at 10:55

2 Answers2

0

Use the following code and manipulate it according to your needs

File sdcardPath = new File(Environment.getExternalStorageDirectory().getPath() +"/SomeFolder");

List<String> list;
list = new ArrayList<String>();
list=sdcardPath.listFiles();

Now loop through the list and check each file name and apply endWith("xml"); function on the items to get all xml files. Hope it works.

Loop in it like that

List<String> XMLFiles= new ArrayList<String>();
for(int i=0; i<=list.size(); i++)
{
   XMLFiles.add(list.get(i).endsWith("xml"));

}
Jibran Khan
  • 3,236
  • 4
  • 37
  • 50
0

I have samsung galaxy s3 with android 4.1.2. My internal phone memory is named sdcard0 and my external card extSdCard.

 Environment.getExternalStorageDirectory()

So the above returns the path of sdcard0 which is internal phone memory

In such cases to get the actual path you can use the below

String externalpath = new String();
String internalpath = new String();

public  void getExternalMounts() {
Runtime runtime = Runtime.getRuntime();
try  
{
Process proc = runtime.exec("mount");
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
String line;
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
if (line.contains("secure")) continue;
if (line.contains("asec")) continue;

if (line.contains("fat")) {//external card
String columns[] = line.split(" ");
if (columns != null && columns.length > 1) {
    externalpath = externalpath.concat("*" + columns[1] + "\n");
}
} 
else if (line.contains("fuse")) {//internal storage
String columns[] = line.split(" ");
if (columns != null && columns.length > 1) {
    internalpath = internalpath.concat(columns[1] + "\n");
}
}
}
}
catch(Exception e)
{
 e.printStackTrace();
}
 System.out.println("Path  of sd card external............"+externalpath);
 System.out.println("Path  of internal memory............"+internalpath); 
}

Once you get the path.

File dir= new File(android.os.Environment.getExternalStorageDirectory());
//Instead of android.os.Environment.getExternalStorageDirectory() you can use internalpath or externalpath

Then call

walkdir(dir);

ArrayList<String> filepath= new ArrayList<String>();//contains list of all files ending with .xml

public void walkdir(File dir) {
String xmlPattern = ".xml";

File listFile[] = dir.listFiles();

if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {

    if (listFile[i].isDirectory()) {
        walkdir(listFile[i]);
    } else {
      if (listFile[i].getName().endsWith(xmlPattern)){
          //Do what ever u want
          filepath.add( listFile[i].getAbsolutePath());
       }
     }
    }
   }    
  }
Raghunandan
  • 132,755
  • 26
  • 225
  • 256