天天看點

android 系統檔案管理1、前言:2、效果圖3、源代碼**下載下傳代碼**

1、前言:

在開發的過程中有時候我們會遇到對檔案的操作:查找、讀取、顯示、管理等。下面對其做詳細的說明:今天完成了記錄下,友善自己以後看,也可以幫助有需要的朋友們。

2、效果圖

android 系統檔案管理1、前言:2、效果圖3、源代碼**下載下傳代碼**

圖 1

android 系統檔案管理1、前言:2、效果圖3、源代碼**下載下傳代碼**

圖 2

android 系統檔案管理1、前言:2、效果圖3、源代碼**下載下傳代碼**

  圖 3

android 系統檔案管理1、前言:2、效果圖3、源代碼**下載下傳代碼**

圖 4

android 系統檔案管理1、前言:2、效果圖3、源代碼**下載下傳代碼**

圖 5

3、源代碼

3.1 XML檔案

3.1.1 file_row.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#ffffff"

android:orientation="horizontal" >

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal"

android:padding="6px" >

<ImageView

android:id="@+id/icon"

android:layout_width="40dip"

android:layout_height="40dip" >

</ImageView>

<TextView

android:id="@+id/text"

android:layout_width="fill_parent"

android:layout_height="40dip"

android:layout_marginLeft="10dp"

android:gravity="center|left" >

</TextView>

</LinearLayout>

</LinearLayout>

3.1.2 fileselect.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical"

android:padding="5dip" >

<!-- -->

<TextView

android:id="@+id/mPath"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="5dp"

android:padding="5dp"

android:textSize="18sp" />

<ListView

android:id="@android:id/list"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@drawable/background_shape" >

</ListView>

</LinearLayout>

4 、Java代碼

4.1、MyAdapter.java

package com.example.fileexample;

import java.io.File;

import java.util.List;

import android.annotation.SuppressLint;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.TextView;

public class MyAdapter extends BaseAdapter {

private String TAG = "MyAdapter";

private LayoutInflater mInflater;

private Bitmap mIcon1;

private Bitmap mIcon2;

private Bitmap mIcon3;

private Bitmap mIcon4;

private Bitmap mIcon5;

private Bitmap mIcon6;

private Bitmap mIcon7;

private Bitmap mIcon8;

private Bitmap mIcon9;

private Bitmap mIcon10;

private Bitmap mIcon11;

private Bitmap mIcon12;

private Bitmap mIcon13;

private Bitmap mIcon14;

private List<String> items;

private List<String> paths;

public MyAdapter(Context context, List<String> it, List<String> pa)

{

mInflater = LayoutInflater.from(context);

items = it;

paths = pa;

mIcon1 = BitmapFactory.decodeResource(context.getResources(),

R.drawable.iconfont_backroot);

mIcon2 = BitmapFactory.decodeResource(context.getResources(),

R.drawable.iconfont_backup);

mIcon3 = BitmapFactory.decodeResource(context.getResources(),

R.drawable.iconfont_folderc);

mIcon4 = BitmapFactory.decodeResource(context.getResources(),

R.drawable.iconfont_txt);

mIcon5 = BitmapFactory.decodeResource(context.getResources(),

R.drawable.iconfont_doc);

mIcon6 = BitmapFactory.decodeResource(context.getResources(),

R.drawable.iconfont_html);

mIcon7 = BitmapFactory.decodeResource(context.getResources(),

R.drawable.iconfont_c);

mIcon8 = BitmapFactory.decodeResource(context.getResources(),

R.drawable.iconfont_pdf);

mIcon9 = BitmapFactory.decodeResource(context.getResources(),

R.drawable.iconfont_zip);

mIcon10 = BitmapFactory.decodeResource(context.getResources(),

R.drawable.iconfont_photos);

mIcon11 = BitmapFactory.decodeResource(context.getResources(),

R.drawable.iconfont_mp3);

mIcon12 = BitmapFactory.decodeResource(context.getResources(),

R.drawable.iconfont_mp4);

mIcon13 = BitmapFactory.decodeResource(context.getResources(),

R.drawable.iconfont_apk);

mIcon14 = BitmapFactory.decodeResource(context.getResources(),

R.drawable.iconfont_other);

}

public int getCount()

{

return items.size();

}

public Object getItem(int position)

{

return items.get(position);

}

public long getItemId(int position)

{

return position;

}

public View getView(int position, View convertView, ViewGroup parent)

{

ViewHolder holder;

if (convertView == null)

{

convertView = mInflater.inflate(R.layout.file_row, null);

holder = new ViewHolder();

holder.text = (TextView) convertView.findViewById(R.id.text);

holder.icon = (ImageView) convertView.findViewById(R.id.icon);

convertView.setTag(holder);

} else

{

holder = (ViewHolder) convertView.getTag();

}

File f = new File(paths.get(position).toString());

MyFileManager file = new MyFileManager();

if (items.get(position).toString().equals("b1"))

{

holder.text.setText("傳回根目錄..");

holder.icon.setImageBitmap(mIcon1);

}

else if (items.get(position).toString().equals("b2"))

{

holder.text.setText("傳回上一層..");

holder.icon.setImageBitmap(mIcon2);

}

else

{

holder.text.setText(f.getName());

String type = checkShapeFile(f);

if (f.isDirectory()) {

holder.icon.setImageBitmap(mIcon3);

}

else if (checkFileIsMyNeedType(type)) {

if (type.equals("txt")) {

Log.d(TAG, "now type..txt." + type);

holder.icon.setImageBitmap(mIcon4);

}

if (type.equals("doc") || type.equals("wps")) {

Log.d(TAG, "now type..doc." + type);

holder.icon.setImageBitmap(mIcon5);

}

if (type.equals("html") || (type.equals("xml"))) {

Log.d(TAG, "now type..html." + type);

holder.icon.setImageBitmap(mIcon6);

}

if (type.equals("c")) {

Log.d(TAG, "now type..c." + type);

holder.icon.setImageBitmap(mIcon7);

}

if (type.equals("pdf")) {

Log.d(TAG, "now type..pdf." + type);

holder.icon.setImageBitmap(mIcon8);

}

if (type.equals("zip") || type.equals("rar")) {

Log.d(TAG, "now type..zip." + type);

holder.icon.setImageBitmap(mIcon9);

}

if (type.equals("jpg") || (type.equals("png"))

|| (type.equals("jpeg")) || (type.equals("bmp"))) {

Log.d(TAG, "now type..jpg." + type);

holder.icon.setImageBitmap(mIcon10);

}

if (type.equals("mp3")) {

Log.d(TAG, "now type..mp3." + type);

holder.icon.setImageBitmap(mIcon11);

}

if (type.equals("mp4") || type.equals("wav")

|| type.equals("swf")) {

Log.d(TAG, "now type..mp4." + type);

holder.icon.setImageBitmap(mIcon12);

}

if (type.equals("exe") || type.equals("apk")) {

Log.d(TAG, "now type..exe." + type);

holder.icon.setImageBitmap(mIcon13);

}

} else {

Log.d(TAG, "now type..pdf.last");

holder.icon.setImageBitmap(mIcon14);

}

}

return convertView;

}

private class ViewHolder

{

TextView text;

ImageView icon;

}

public String checkShapeFile(File file) {

String fileNameString = file.getName();

String endNameString = fileNameString.substring(

fileNameString.lastIndexOf(".") + 1, fileNameString.length())

.toLowerCase();

return endNameString;

}

public boolean checkFileIsMyNeedType(String strtype) {

boolean flags = false;

for (int i = 0; i < MyFileManager.FILESTYPE_CONTENTS.length; i++) {

if (strtype.equals(MyFileManager.FILESTYPE_CONTENTS[i])) {

flags = true;

break;

} else {

flags = false;

}

}

return flags;

}

}

4.2、MyFileManage.java

package com.example.fileexample;

import java.io.File;

import java.util.ArrayList;

import java.util.List;

import android.app.ListActivity;

import android.content.Intent;

import android.os.Bundle;

import android.os.Environment;

import android.util.Log;

import android.view.View;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.Toast;

public class MyFileManager extends ListActivity {

private String TAG = "MyFileManager";

private List<String> items = null;

private List<String> paths = null;

public static final int FILE_RESULT_BACKCODE = 1;

public static boolean IS_DIRECTORY = false;

public static boolean IS_FILE = false;

// 動态初始化...

public static String FILE_TYPE[] = new String[200];

public static final String FILESTYPE_CONTENTS[] = { "txt", "doc", "wps",

"html", "xml", "pdf", "zip", "rar", "bmp", "jpg", "png", "jpeg",

"wav", "mp3", "mp4", "avi", "mov", "swf", "exe", "c", "asm", "lib",

"bak", "dot", "bat" };

private String rootPath = getSDDir();

private String curPath = getSDDir();

private TextView mPath;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.fileselect);

mPath = (TextView) findViewById(R.id.mPath);

getFileDir(rootPath);

}

private void getFileDir(String filePath) {

mPath.setText(filePath);

items = new ArrayList<String>();

paths = new ArrayList<String>();

File f = new File(filePath);

File[] files = f.listFiles();

if (!filePath.equals(rootPath)) {

items.add("b1");

paths.add(rootPath);

items.add("b2");

paths.add(f.getParent());

}

for (int i = 0, j = 0; i < files.length; i++) {

File file = files[i];

String type = checkShapeFile(file).toString();

Log.d(TAG, "now type.." + i + ".." + type);

if (type != "isDirectory") {

FILE_TYPE[j] = type;

Log.d(TAG, "FILE_TYPE[ " + j + " ]" + FILE_TYPE[j]);

j++;

}

if (IS_DIRECTORY || IS_FILE) {

items.add(file.getName());

paths.add(file.getPath());

}

}

setListAdapter(new MyAdapter(this, items, paths));

}

// open allocate file

// 獲得選擇檔案的目錄,并用bundle打包,傳遞

@Override

protected void onListItemClick(ListView l, View v, int position, long id) {

File file = new File(paths.get(position));

if (file.isDirectory()) {

curPath = paths.get(position);

Log.d(TAG, "curpath...." + curPath);

getFileDir(paths.get(position));

} else {

Intent data = new Intent(MyFileManager.this,

MainActivity.class);

Bundle bundle = new Bundle();

bundle.putString("file", file.getPath());

data.putExtras(bundle);

setResult(2, data);

finish();

}

}

public String checkShapeFile(File file) {

String fileNameString = file.getName();

String endNameString = fileNameString.substring(

fileNameString.lastIndexOf(".") + 1, fileNameString.length())

.toLowerCase();

if (file.isDirectory()) {

} else if (file.isFile()) {

Log.d(TAG, "endnamestring....." + endNameString);

}

// file is directory or not

if (file.isDirectory()) {

IS_DIRECTORY = true;

endNameString = "isDirectory";

Log.d(TAG, " now endnamestring....." + endNameString);

return endNameString;

}

if (file.isFile()) {

for (int i = 0; i < FILESTYPE_CONTENTS.length; i++) {

if (endNameString.equals(FILESTYPE_CONTENTS[i])) {

IS_FILE = true;

break;

} else {

IS_FILE = false;

}

Log.d(TAG, " file endnamestring....." + endNameString);

}

} else {

IS_FILE = false;

Log.d(TAG, "nxjdvndjvnkdf");

return "";

}

return endNameString;

}

protected final String getSDDir() {

if (!checkSDcard()) {

Toast.makeText(this, "no sdcard", Toast.LENGTH_SHORT).show();

return "";

}

try {

String SD_DIR = Environment.getExternalStorageDirectory()

.getAbsolutePath()

.toString();

// String sdpath=Context.getExternalCacheDir().toString();

Log.d(TAG, "Environment.getExternalStorageDirectory()" + SD_DIR);

return SD_DIR;

} catch (Exception e) {

return "";

}

}

public boolean checkSDcard() {

String sdStutusString = Environment.getExternalStorageState();

if (sdStutusString.equals(Environment.MEDIA_MOUNTED)) {

return true;

} else {

return false;

}

}

}

4.3、MainActivity.java

package com.example.fileexample;

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.view.Menu;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Intent intent=new Intent();

intent.setClass(MainActivity.this, MyFileManager.class);

startActivity(intent);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

ok,完成了,其實很簡單,記錄下,整個源代碼可以在我的資源中下載下傳。

**下載下傳代碼**

http://download.csdn.net/detail/qwqwqw1214/8208619