Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Get Folder Names Inside External Storage Having mp3 Files in Android - How to Guide

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 2.24k
    Comment on it

    To print all the Folder names inside your external storage that contains mp3 files, first we need permission to read and Write external Storage on your android device.

     

     

    So, in this tutorial, I will be coding for the method that will print all the Folder names inside your external storage that contains mp3 files and below are the premissions to read write externak storage:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

     

    Below is the code that ask for the run-time permission to read external storage.

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_landing_screen);
            recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
            String[] PERMISSIONS = {Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED ||
                    ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
                    ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, PERMISSIONS, 100);
            } else {
                getAllFolder();
            }
    
        }
    

     

    Now to handle the event that user will perform when the app will ask for permission below code comes in action

    @Override
        public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
            if (grantResults.length > 0) {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED &&
                        grantResults[1] == PackageManager.PERMISSION_GRANTED &&
                        grantResults[2] == PackageManager.PERMISSION_GRANTED) {
    //                startForResult();
                    getAllFolder();
                } else {
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage("To read songs in your device Allow AmiPlayer to access to read your storage.")
                            .setCancelable(false)
                            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    //do things
    
                                }
                            });
                    AlertDialog alert = builder.create();
                    alert.show();
    //                Toast.makeText(Dashboard.this, AppConstant.sPermissionDenied, Toast.LENGTH_LONG).show();
    
                }
            }
            return;
        }

     

    In the method shown below the method calls itself if it founds a directory within the directory itself, if there exist a .mp3 file than it adds the directory to the list containing mp3 files.

    private void getAllStuff(File file, String name) {
            File list[] = file.listFiles();
            boolean folderFound = false;
            File mFile = null;
            String directoryName = "";
    
            for (int i = 0; i < list.length; i++) {
    
                mFile = new File(file, list[i].getName());
                if (mFile.isDirectory()) {
                    directoryName = list[i].getName();
    
                     getAllStuff(mFile, directoryName);
    
    
                } else {
                    if (list[i].getName().toLowerCase(Locale.getDefault()).endsWith(".mp3")) {
                        myList.add(list[i].getName());
    
                        folderFound = true;
                    }
    
    
                }
            }
    
            if (folderFound) {
    
                folderName.add(a, name);
                Log.e("Folder Name", name);
                a++;
            }
        }

     

    There are two parameters in the above method first is file on which we will be searching for .mp3 file and second parameter is name of the parent directory and here I'm making call to this mehode using another method shown below.

      private void getAllFolder() {
            
            String root_sd = Environment.getExternalStorageDirectory().toString();
            File file = new File(root_sd);
            //list content of root sd
            getAllStuff(file, " ");
    
        }

     

    Get Folder Names Inside External Storage Having mp3 Files in Android - How to Guide

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: