Like native Android File Manager and Android File Manager

Source: Internet
Author: User

Like native Android File Manager and Android File Manager

Developed based on the File Manager that comes with Android native. Here is a simple demo, which can be easily implemented based on the existing code, as shown below:






First, create a listview_item. If you have learned the adapter, you should know what you want to do later. This is to draw the interface of each list item. The icon + file name is used here.

Listview_item.xml


<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    
    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    
    <TextView 
        android:id="@+id/name"
        android:textSize="25sp"
        android:layout_marginLeft="10dp"
        android:layout_gravity="center_vertical"
        android:textColor="#080808"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
        
    

</LinearLayout>
</span>




Then we began to write our main interface layout, nesting of two layers of LinearLayout.

Activity_main.xml


<span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.filemanagerdemo.MainActivity" >
    

  <LinearLayout 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal">
      
      <Button 
          android:id="@+id/parent"
          android:text="return"
          android:background="#00EE76"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/>
      
      <TextView 
          android:id="@+id/textview"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"/>     
  </LinearLayout>
  
  <ListView 
      android:layout_width="wrap_content"
      android:layout_height="0dp"
      android:layout_marginTop="3dp"
      android:id="@+id/listview"
      android:dividerHeight="1dp"
      android:layout_weight="1">
  </ListView>

</LinearLayout>
</span>





Now all the static interfaces are ready. The most important main Activity code is explained below.

MainActivity. java


<span style = "font-size: 18px;"> package com.example.filemanagerdemo;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {

private Button parent;
private ListView listview;
private TextView textview;
private File currentParent; // Record the parent folder of the current file
private File [] currentFiles;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
init ();
}


/ *
* Initialize controls
* /
private void init () {
// TODO Auto-generated method stub
this.parent = (Button) findViewById (R.id.parent);
this.listview = (ListView) findViewById (R.id.listview);
this.textview = (TextView) findViewById (R.id.textview);
File root = new File (Environment.getExternalStorageDirectory (). GetPath ());
if (root.exists ()) {
currentParent = root;
currentFiles = root.listFiles ();
inflateListView (currentFiles);
}
this.parent.setOnClickListener (new OnClickListener () {

@Override
public void onClick (View v) {
// TODO Auto-generated method stub
try {
if (! currentParent.getCanonicalFile (). equals (Environment.getExternalStorageDirectory (). getPath ())) {
currentParent = currentParent.getParentFile (); // Get the parent directory
currentFiles = currentParent.listFiles (); // Get all files in the current layer
inflateListView (currentFiles); // Update list
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace ();
}
}
});

this.listview.setOnItemClickListener (new AdapterView.OnItemClickListener () {

@Override
public void onItemClick (AdapterView <?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
// If the click is a file, do nothing
if (currentFiles [position] .isFile ()) {
return;
}
File [] temp = currentFiles [position] .listFiles ();
if (temp == null || temp.length == 0) {
Toast.makeText (getApplicationContext (), "This folder is unavailable or empty", Toast.LENGTH_LONG) .show ();;
return;
}
currentParent = currentFiles [position];
currentFiles = temp;
inflateListView (currentFiles);

}
});
}




/ *
* Update listview
* /
private void inflateListView (File [] files) {
// TODO Auto-generated method stub
List <Map <String, Object >> list = new ArrayList <Map <String, Object >> ();
for (int i = 0; i <files.length; i ++) {
Map <String, Object> item = new HashMap <String, Object> ();
if (files [i] .isDirectory ()) {
item.put ("icon", R.drawable.folder);
} else {
item.put ("icon", R.drawable.file); // It can be written here to add different icons according to the specific file type
}
item.put ("name", files [i] .getName ());
list.add (item);
}
SimpleAdapter adapter = new SimpleAdapter (getApplicationContext (), list, R.layout.listview_item, new String [] {"icon", "name"}, new int [] {R.id.icon, R.id.name}) ;
this.listview.setAdapter (adapter);
try {
textview.setText (currentParent.getCanonicalPath ());
} catch (IOException e) {
e.printStackTrace ();
}
}
}
</ span>

It is not difficult to read the code from onCreate. OK. A file manager demo that is similar to the android native system has been developed. This demo is a very nonstandard writing habit for everyone to see clearly. For example, strings should be put into the string. xml, color is also better not to use RGB encoding directly, should create a new color under the value. xml specifies a name for this RGB encoding. I have used hard encoding for the sake of clarity, and the method for getting the root directory of the SD card is too simple and crude. Normally, I should write a special method to judge various exceptions, for example, determine whether there is an SD card or whether the current SD card is available. Otherwise, once the user performs an abnormal operation, our program will crash. This is the difference between a formal project and a demo.



OK that is all. If there is an error, I hope the viewer can correct it.

If you think it's good, please click here.

Top!


D = ( ̄ ▽  ̄ *) B


Or



OooO zookeeper Oooo
(Step on) → give you a try (dead)
\ (→ Success √ success )/
\ _) Too many rows (_/
.


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.