一,概要:
Android中的File类和JAVA中File类是一样的,方法也可以去看看java的API(点击查看)。这里补充几点:
File file=this.getFilesDir();
//当前应用数据默认的数据存储路劲:/data/user/0/com.maeeage.administrator.androidlearn/files
i("getFilesDir--->",file.getAbsolutePath());
File file1=this.getCacheDir();
//当前应用数据默认的数据缓存存储路劲:/data/user/0/com.maeeage.administrator.androidlearn/cache
// 可以把一些不是非常重要的文件放在这里,
// 如果手机内存不足,系统会自动删除App的Cache目录下的数据
i("getCacheDir--->",file1.getAbsolutePath());
File file2=this.getDir("file2",MODE_PRIVATE);
//自定义目录:/data/user/0/com.maeeage.administrator.androidlearn/app_file2
i("getDir()--->",file2.getAbsolutePath());
//
File file3=this.getExternalCacheDir();
//得到外部的储存位置,该位置的数据与内置的使用时一样的:
// /storage/emulated/0/Android/data/com.maeeage.administrator.androidlearn/cache
i("getExternalCacheDir--->",file3.getAbsolutePath());
File file4=this.getExternalFilesDir("file4");
//在外部自定义的:storage/emulated/0/Android/data/com.maeeage.administrator.androidlearn/files/file4
i("getExternalFilesDir--->",file4.getAbsolutePath());
//如果开发者按照上述储存数据,在App写在后,数据也会跟着清除。
以上代码运行结果:
二,小demo
1,先看效果:
2,视图层:
file_manager.xml:
<?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="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/file_title"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="文件管理"
android:gravity="center"
/>
</LinearLayout>
<ListView
android:id="@+id/file_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
item_file.xml:(ImageView的背景是用来区分是文件还是目录的)
<?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">
<ImageView
android:id="@+id/file_image"
android:background="@drawable/isdictionary"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="5dp"
/>
<TextView
android:id="@+id/filename"
android:layout_width="35dp"
android:layout_height="35dp"
android:gravity="center"
android:layout_weight="10.5"/>
<TextView
android:id="@+id/isDictionary"
android:layout_width="35dp"
android:layout_height="35dp"
android:gravity="center_horizontal"
/>
</LinearLayout>
3,逻辑层:
package com.maeeage.administrator.androidlearn.filemanager;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.maeeage.administrator.androidlearn.R;
import com.maeeage.administrator.androidlearn.adapter.FileAdapter;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2018/2/5/005.
*/
public class FileManager extends Activity {
ListView listView;
TextView title;
String dir;
//用存放路劲
FileAdapter adapter;
//适配器
List<File> dateList;
//File 数据
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.file_manager);
findVById();
init();
//初始化
}
private void findVById() {
listView=(ListView)findViewById(R.id.file_listview);
title=(TextView)findViewById(R.id.file_title);
}
//初始化
private void init() {
Intent intent=getIntent();
//获取Intent的,接收activity传来的值,
dir= intent.getStringExtra("dir");
//如果为null,dir的值为 :Environment.getExternalStorageDirectory().getAbsolutePath();
//这个路劲就是一般打开手机文件管理文件目录的路劲
if (dir!=null)
;
else
dir= Environment.getExternalStorageDirectory().getAbsolutePath();
//获取title:让其显示文件路劲:如Android>data>com......
if(intent.getStringExtra("title")!=null)
title.setText(intent.getStringExtra("title"));
else
title.setText("文件管理");
//为listView注册上下文菜单,当长按某一个文件出现菜单:
this.registerForContextMenu(listView);
dateList=new ArrayList<>();
adapter=new FileAdapter(this,getDate());
listView.setAdapter(adapter);
//listView 点击事件,当点击的文件为目录时,
// 把dir的值赋值为:dir+点击的目录,再次跳到此页,既可以达到循环,不要再去新建一个activity在现实:
// intent.putExtra("dir",dir+"/"+dateList.get(i).getName());
//intent.putExtra("title",title.getText()+">"+dateList.get(i).getName());
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(dateList.get(i).isDirectory())
{
Intent intent=new Intent(FileManager.this,FileManager.class);
intent.putExtra("dir",dir+"/"+dateList.get(i).getName());
intent.putExtra("title",title.getText()+">"+dateList.get(i).getName());
startActivity(intent);
}
}
});
}
//为上下文菜单添加菜单项
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("文件操作");
menu.setHeaderIcon(R.drawable.ic_brightness_high_black_24dp);
menu.add(1,1,1,"复制");
menu.add(1,2,1,"粘贴");
menu.add(1,3,1,"剪切");
menu.add(1,4,1,"重命名");
}
//选中菜单项点击事件,这里就Toast一下,
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case 1:
Toast.makeText(FileManager.this,"已复制",Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(FileManager.this,"已粘贴",Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(FileManager.this,"剪切",Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(FileManager.this,"重命名",Toast.LENGTH_SHORT).show();
break;
}
return super.onContextItemSelected(item);
}
//获取dir下所有的文件
public List< File> getDate() {
File file=new File(dir);
if(file.exists())
{
File[] file1=file.listFiles();
for (File filename :
file1) {
dateList.add(filename);
}
}
return dateList;
}
}
FileAdapter.java(适配器):
package com.maeeage.administrator.androidlearn.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.maeeage.administrator.androidlearn.R;
import java.io.File;
import java.util.List;
/**
* Created by Administrator on 2018/2/5/005.
*/
public class FileAdapter extends BaseAdapter {
//传递File数据和上下文;
List<File> list;
Context context;
public FileAdapter(Context context,List<File> list) {
this.list = list;
this.context=context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view= View.inflate(context,R.layout.item_file,null);
File file=list.get(i);
ImageView image_photo= (ImageView) view.findViewById(R.id.file_image);
TextView tv_name= (TextView) view.findViewById(R.id.filename);
TextView tv_age= (TextView) view.findViewById(R.id.isDictionary);
//如果某个文件是目录:就在后面显示》;否则显示空
if (file.isDirectory()){
tv_age.setText("▶");
image_photo.setImageResource(R.drawable.isdictionary);
}
else {
tv_age.setText(" ");
image_photo.setImageResource(R.drawable.notdictionary);
}
tv_name.setText(file.getName());
return view;
}
}
三,总结:
掌握File的对软件开发很重要的!好好练习!