The simplest SD card file to traverse the Android program _android

Source: Internet
Author: User

This example for you to share the simplest SD card file traversal procedures for your reference, the specific content as follows

Package Com.wenhao.test.sddemo;

Import Java.io.File;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.os.Environment;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;

Import Android.widget.Toast; The public class Maindemo extends activity {/** called the ' when the ' is the ' The activity ' is the ' the '. */private Button button =
  Null
  
  Private File path;
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
      
    Setcontentview (R.layout.main);
    
    Button = (button) Findviewbyid (R.id.mybutton); Detect if the SD card exists if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {path = EnviR
    Onment.getexternalstoragedirectory ();
      }else{Toast.maketext (This, "No SD card", Toast.length_long). Show ();
    Finish (); Button.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {
        TODO auto-generated Method Stub getallfiles (path);
    
  }
    }); 
    //traversal receives a file path and then iterates through and out of all files in the subdirectory of the file private void Getallfiles (file root) {file files[] = Root.listfiles (); 
        if (Files!= null) {for (File f:files) {if (F.isdirectory ()) {getallfiles (f); 
        }else{System.out.println (f);
 } 
      } 
    } 
  } 
   
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.