The Android programming approach to file browsing is "similar to FileDialog functionality" _android

Source: Internet
Author: User

This article is an example of how the Android program implements the file browsing function. Share to everyone for your reference, specific as follows:

Recently uploading files, at that time, how can realize the function of FileDialog, open the file, browse the file, and then select the file, check a lot of information, also saw a lot of forums, said there is no such function, it is really strange, there is no such function, of course, you need to add this function.

First of all, the simple implementation of this file browsing principle:

First select a directory as the root directory, and then open the directory, the common use of the file is the class, as follows:

File File=new file (path);

You can then obtain a list of all files and folders in this directory:

As follows:

file[] files = file.listfiles ();

And then based on the documents that were obtained, to determine whether it is a folder or a file, if it is a folder, then we will add the folder to the list, if it is a file to add files to the list to display, if you need to display the icon, then you need to according to the file suffix, set a different icon. I was shown in the ListView.

The basic code is as follows:

for (file currentfile:files) {//Judge whether it is a folder or a file if (Currentfile.isdirectory ()) {Currenticon = Getresources ()
  . getdrawable (R.drawable.folder);
    else {//get filename String filename = currentfile.getname (); Depending on the filename to determine the file type, set a different icon if (checkendswithinstringarray filename, getresources (). Getstringarray (
    R.array.fileendingimage))) {Currenticon = Getresources (). getdrawable (R.drawable.image);
      else if (Checkendswithinstringarray (FileName, Getresources (). Getstringarray (R.array.fileendingwebtext))} {
    Currenticon = Getresources (). getdrawable (R.drawable.webtext);
      else if (Checkendswithinstringarray (FileName, Getresources (). Getstringarray (R.array.fileendingpackage))} {
    Currenticon = Getresources (). getdrawable (r.drawable.packed); else if (Checkendswithinstringarray (FileName, Getresources (). Getstringarray (R.array.fileendingaudio))) {C
    Urrenticon = Getresources (). getdrawable (R.drawable.audio);
    }else if (Checkendswithinstringarray (FileName, Getresources (). Getstringarray (R.array.fileendingvideo))) {current
    Icon = Getresources (). getdrawable (R.drawable.video);
    else {Currenticon = Getresources (). getdrawable (R.drawable.text);

 }
  }
}

The following is a decision based on the suffix for the type of file:

What type of file is judged by file name
private Boolean Checkendswithinstringarray (String checkitsend, string[] fileendings)
{ For
    (String aend:fileendings)
    {
      if (Checkitsend.endswith (aend)) return
        true;
    }
    return false;
}

So we can display the list with the icon, now to the introduction of the subdirectory, the first to ListView add click Events, after the click, we click on the content to judge whether the file or folder, if it is a folder, then call again display, And will be displayed before the contents of the empty can be displayed, if it is a file, then this according to our needs to operate: such as open, select and so on operation.

What's written above is about how to make a file browser of your own, and where you can use it to display specific files. This needs readers in their own use of their own slowly familiar with the groping!

This listview is also based on their own needs of the layout, I believe that by learning this article, you will certainly use file and ListView to have an understanding, but also want to make their own practical file browser!

For more information on Android-related content readers can view the site: "Android file Operation tips Summary", "Android Database Operating skills summary", "Android programming activity Operation Skills Summary", " Android programming development of the SD card operation method Summary, "Android Development introduction and Advanced Course", "Android Resource Operation skills Summary", "Android View tips Summary" and "Android Control usage Summary"

I hope this article will help you with the Android program.

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.