今天也是上次那网友叫我帮忙做文件相关的东西,于是开始学习Android文件管理

MyActivity:

package com.lhw.android;

import java.io.File;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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 {
/**
* 遇到个不懂得西先查下API,本人英语四级都没过悲剧
* 不过还是基本看得懂哈
* BaseAdapter是ListAdapter,和SpinerAdapter的抽象实现类
*
*
*/
private LayoutInflater inflater;
//回到根目录
private Bitmap iconRoot;
//回到上一层的图片
private Bitmap iconUp;
//目录图片
private Bitmap iconFolder;
//文件的图片
private Bitmap iconFile;

private List<String> itemList;
private List<String> pathList;

public MyAdapter(Context context,List<String> items,List<String> paths){
/**在Activity里使用LayoutInflater来载入界面,通过getSystemService(Context.LAYOUT_INFLATER_SERVICE)方法可以
* 获得一个LayoutInflater,然后使用inflater方法来载入layout的xml,对于一个没有被载入或者想要动态载入的界面,
* 都需要使用inflate来载入.
* 一个已载入的界面可以使用这个界面调用findViewById方法获得其中的子界面,就是我们常用的this.findViewById
*/
inflater=LayoutInflater.from(context);
itemList=items;
pathList=paths;
//初始化图标,从资源中获取位图(网上随便找个Bitmap资源http://www.pin5i.com/showtopic-26566.html,最好的还是API嘿嘿)
iconRoot=BitmapFactory.decodeResource(context.getResources(),R.drawable.back01);
iconUp=BitmapFactory.decodeResource(context.getResources(),R.drawable.back02);
iconFolder=BitmapFactory.decodeResource(context.getResources(),R.drawable.folder);
iconFile=BitmapFactory.decodeResource(context.getResources(),R.drawable.doc);

}

public int getCount() {
return itemList.size();
}

public Object getItem(int position) {
return itemList.get(position);
}

public long getItemId(int position) {
return position;
}
/**
* 我懒得翻译了
*@param position The position of the item within the adapter's data set of the item whose view we want.
*@param convertView The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view.
*@param parent The parent that this view will eventually be attached to
*/
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
//自定义file_row作为layout
convertView=inflater.inflate(R.layout.file_row,null);
//初始化holder的text与icon
holder=new ViewHolder();
holder.tv=(TextView)convertView.findViewById(R.id.tv);
convertView.setTag(holder);
}else{
holder=(ViewHolder)convertView.getTag();
}
File f=new File(pathList.get(position).toString());
//回到根目录
if("b1".equals(itemList.get(position).toString())){
holder.tv.setTag("Back to /");
holder.iv.setImageBitmap(iconRoot);
}else if("b2".equals(itemList.get(position).toString())){//回到上级目录
holder.tv.setText("Back to ..");
holder.iv.setImageBitmap(iconUp);
}else{
holder.tv.setText(f.getName());
if(f.isDirectory()){//目录
holder.iv.setImageBitmap(iconFolder);
}else{//文件
holder.iv.setImageBitmap(iconFile);
}
}
return convertView;
}

private class ViewHolder{
TextView tv;
//用于展示图片
ImageView iv;
}

}

MainActivity:

package com.lhw.android;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ListActivity {

//存储显示名称
private List<String> itemList=null;
//存储文件路径
private List<String> pathList=null;
//起始目录-(根目录)
private String rootPath="/";
//显示路径
private TextView tvPath;
private View view;
private EditText et;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

tvPath=(TextView)this.findViewById(R.id.mPath);
getFileDir(rootPath);
}

/**
* 获取当前文件列表
* @param filePath
*/
private void getFileDir(String filePath){
tvPath.setText(filePath);
itemList=new ArrayList<String>();
pathList=new ArrayList<String>();
//获取目录(java中文件和目录都用File表示)
File f=new File(filePath);
File[] files=f.listFiles();
if(!filePath.equals("/")){
//根路径
itemList.add("b1");
pathList.add(rootPath);
//回上级
itemList.add("b2");
pathList.add(f.getParent());
}
//保存所有文件
for(File file:files){
itemList.add(file.getName());
pathList.add(file.getPath());
}
this.setListAdapter(new MyAdapter(this,itemList,pathList));
}

/**
* ListItem单击事件
*/
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
File file=new File(pathList.get(position));
if(file.isDirectory()){//目录
getFileDir(pathList.get(position));//继续查找
}else{//文件
fileHanle(file);
}
}

/**
* 文件处理
* @param file 文件
*/
private void fileHanle(final File file){
//总是不习惯这样的代码风格,不知道google怎么搞的,呵呵
OnClickListener listener=new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if(which==0){
openFile(file);//打开文件
}else if(which==1){
//选择的item为更改档名
LayoutInflater factory=LayoutInflater.from(MainActivity.this);
view=factory.inflate(R.layout.rename_alert_dialog, null);//解释见MyActivity中
et=(EditText)view.findViewById(R.id.et);
//将原始文件名先放入EditText中
et.setText(file.getName());
//又来种风格编码,悲剧
//新建一个更改文件名的Dialog的确定按钮的Listener
OnClickListener listener2=new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//取得修改后的文件路径
String modName=et.getText().toString();
final String pFile=file.getParentFile().getPath()+"/";
final String newPath=pFile+modName;

//判断档名是否已存在
if(new File(newPath).exists()){
//排除修改档名时没修改直接送出的状况
if(!modName.equals(file.getName())){
//这种风格跟Ext相似,我想我会习惯的
new AlertDialog.Builder(MainActivity.this)
.setTitle("注意")
.setMessage("档名已经存在,是否要覆盖?")
.setPositiveButton("确定",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//档名重复仍然修改会覆盖已存在的文件
file.renameTo(new File(newPath));
//重新产生文件列表
getFileDir(pFile);
}
}).setNegativeButton("取消",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

}
}).show();
}
}else{
file.renameTo(new File(newPath));
getFileDir(pFile);
}
}
};/* create更改档名时跳出的Dialog */
AlertDialog renameDialog=new AlertDialog.Builder(MainActivity.this).create();
renameDialog.setView(view);

/* 设置更改档名点击确认后的Listener */
renameDialog.setButton("确定",listener2);
renameDialog.setButton2("取消",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){

}
});
renameDialog.show();
}else{
/* 选择的item为删除文件 */
new AlertDialog.Builder(MainActivity.this).setTitle("注意!")
.setMessage("确定要删除文件吗?")
.setPositiveButton("确定",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
/* 删除文件 */
file.delete();
getFileDir(file.getParent());
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){

}
}).show();
}
}
};

/* 选择一个文件时,跳出要如何处理文件的ListDialog */
String[] menu={"打开文件","更改文件名","删除文件"};
new AlertDialog.Builder(MainActivity.this)
.setTitle("你要做甚么?")
.setItems(menu,listener)
.setPositiveButton("取消",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){

}
}
).show();
}

/**
* 打开文件
* @param file 文件
*/
private void openFile(File file){
Intent intent=new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
String type=getMIMEType(file);
intent.setDataAndType(Uri.fromFile(file), type);
startActivity(intent);
}

/**
* 判断文件MimeType的方法
* @param f
* @return
*/
private String getMIMEType(File f)
{
String type="";
String fName=f.getName();
/* 取得扩展名 */
String end=fName.substring(fName.lastIndexOf(".")+1,
fName.length()).toLowerCase();

/* 依附档名的类型决定MimeType */
if(end.equals("m4a")||end.equals("mp3")||end.equals("mid")
||end.equals("xmf")||end.equals("ogg")||end.equals("wav"))
{
type = "audio";
}
else if(end.equals("3gp")||end.equals("mp4"))
{
type = "video";
}
else if(end.equals("jpg")||end.equals("gif")||end.equals("png")
||end.equals("jpeg")||end.equals("bmp"))
{
type = "image";
}
else
{
/* 如果无法直接打开,就跳出软件列表给用户选择 */
type="*";
}
type += "/*";
return type;
}
}

按照书上的代码,边敲边理解,结果不知知道什么原因,Run不起,看来,只能明晚在看了。上班一直敲得最多的是Flex代码,现在java代码总喜欢搞个var,function,():void,一样的东西,悲剧啦。睡觉了又1点了。好久没看日语了,唉!先睡觉了,早睡早起

更多相关文章

  1. Android Studio Gradle多渠道打包(动态设定App名称,应用图标,背景
  2. android用户界面详尽教程实例
  3. Android中的文件上传下载
  4. android 跳转到应用通知设置界面【Android 8.0 需要特殊处理】
  5. android客户端从服务器端下载文件,服务端返回文件流(文件不在项目

随机推荐

  1. 我勒个去,你们疯了吧?webOS比Android好多了
  2. android 模拟器的使用(Android模拟器的一
  3. Android(安卓)开发平台的搭建
  4. 【叫兽学堂--番外篇】淡解Android 1.5 SD
  5. Android客户端与PHP服务端的数据交互
  6. 关于Android悬浮窗要获取按键响应的问题
  7. Android之实用库xUtils四大模块以及用Vie
  8. 如何在开发时可以让Android应用程序支持
  9. Android的智能指针
  10. Android AudioRecord和AudioTrack介绍