Search code examples
androidsd-cardandroid-sdcard

opening sdcard directory and showing it on screen


I have a query. I need to show all the contents of sdcard specific folder. What I need to do is I need to tell the user that there are no files in a particular directory in sdcard so need to open that folder and show it on screen. For eg. I there is no internet we can show an alert to open internet and also open the settings screen. Same way I also want to open the sdcard/myFolder screen. Please guide me what to do if its possible in android? Thank you all in advance.


Solution

  • Try to use the following code to check if the directory is available and have files:

    String Directory = "";
    String[] mFileList ;
    //GET ROOT DIRECTORY OF EXTERNAL STORAGE + DIRECTORY THAT YOU WANT
    
    File dir = new File(Environment.getExternalStorageDirectory() + DIRECTORY);
    if(dir.exists()) {
      mFileList = dir.list(filter);   
      if(mFileList.length==0)
        //Inform that directory is empty
    }
    else {
      //Inform user that there is no such directory
    }
    

    Then you can open dialog:

    Dialog dialog = null;
    AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Holo));//this, R.style.AlertDialogCustom));
    builder.setTitle("Choose your file");
    if(mFileList == null) {
        //Log.e(TAG, "Showing file picker before loading the file list");
        dialog = builder.create();
    }
    builder.setItems(mFileList, new DialogInterface.OnClickListener() {
    
    public void onClick(DialogInterface dialog, int which) {
            mChosenFile = mFileList[which];
            //Do something after choosing the file
        }
    });    
    
    dialog = builder.show();
    return true;
    

    I hope it will help you.