android 系统文件管理

2 篇文章 0 订阅
1 篇文章 0 订阅

1、前言:

在开发的过程中有时候我们会遇到对文件的操作:查找、读取、显示、管理等。下面对其做详细的说明:今天完成了记录下,方便自己以后看,也可以帮助有需要的朋友们。

2、效果图



图 1

图 2

 
图 3

图 4



图 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;

/**
* @author yuanhaiq
* @version 1.0
* @since 2014-11-29
* @category




* */
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;

/**
* @author yuanhaiq
* @version 1.0
* @since 2014-11-29
* @category


* */
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,完成了,其实很简单,记录下,整个源代码可以在我的资源中下载。

**下载代码**


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值