Question

I am working on a project which contains a module to scan pdf, doc xls files from sd card and make list of them. I also want to make list of folders only.

As I am very new to android. Anyone have idea of achieving this.

Here is my code:

public class MediaScannerWrapper implements  
MediaScannerConnection.MediaScannerConnectionClient {
    private MediaScannerConnection mConnection;
    private String mPath;
    private String mMimeType;

    // filePath - where to scan; 
    // mime type of media to scan i.e. "image/jpeg". 
    // use "*/*" for any media
    public MediaScannerWrapper(Context ctx, String filePath, String mime){
        mPath = filePath;
        mMimeType = mime;
        mConnection = new MediaScannerConnection(ctx, this);
    }

    // do the scanning
    public void scan() {
        mConnection.connect();
    }

    // start the scan when scanner is ready
    public void onMediaScannerConnected() {
        mConnection.scanFile(mPath, mMimeType);
        Log.w("MediaScannerWrapper", "media file scanned: " + mPath);
    }

    public void onScanCompleted(String path, Uri uri) {
        // when scan is completes, update media file tags
    }
}
Was it helpful?

Solution

public void walkdir(File dir) {
    String pdfPattern = ".pdf";

    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(pdfPattern)){
                              //Do what ever u want

              }
            }
        }
    }    }

To search on the whole sdcard call this function usingwalkdir(Environment.getExternalStorageDirectory());

OTHER TIPS

Use getExternalStorageDirectory () to get the SD card path. (Do not hardcode it)

Loop through each subfolder, and check files names with your desired extension. Use String endswith() method to check if file name ends with the extension.

Here's a sample code that might help you.

I advice using commons.io library which handles symbolic links and extension resolution as well.

Task to scan for files with extensions:

import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
import org.apache.commons.io.FileUtils;

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

public class ScanTask extends AsyncTask<String,Void,ScanTask.Result> {

    @Override
    protected Result doInBackground(String... extensions) {

        if(extensions == null){
            extensions = new String[0];
        }

        List<File> files = new ArrayList<File>();
        boolean success = false;
        String status = "";

        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            try {
                files.addAll(FileUtils.listFiles(Environment.getExternalStorageDirectory(), extensions, true));
                success = true;
                status = "ok";
            } catch (Exception e) {
                Log.e("MyApp","File error:",e);
                success = false;
                status = "Scan error";
            }
        }else {
            success = false;
            status = "External storage not available";
        }


        return new Result(success,status,files);
    }

    public static class Result{
        public final boolean success;
        public final String message;
        public final List<File> files;

        public Result(boolean success, String message,List<File> files) {
            this.success = success;
            this.message = message;
            this.files = files;
        }
    }
}

Usage:

   ScanTask task = new ScanTask(){
        @Override
        protected void onPostExecute(Result result) {
            super.onPostExecute(result);

            if(result.success){
                List<File> files = result.files;
                //--do something with files--
            }else {
                String error = result.message;
                //--do something with error message--
            }
        }
    };

    task.execute("mp3","jpeg");

You can use this code:
here is the link.

/**
  * Class to filter files which are having .mp3 extension
  * */
//you can choose the filter for me i put .mp3
class FileExtensionFilter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return (name.endsWith(".pdf" ) || name.endsWith(".docs" )) || name.endsWith(".3gp" ) ;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top