本实例设置了两个按钮,分别用于添加和删除内存和存储卡中的文件,使用了3个Activity,分别用于主程序界面和处理内存卡和存储卡。当用户选择用内存卡和存储卡后会以列表的形式显示出里面所有的目录和文件名,并在MENU菜单中显示【添加】和【删除】按钮,分别添加文件和删除文件。具体实现:
step1:编写MainActivity文件:
定义myButton1、myButton2、fileDir和sdcardDir参数对象,通过findViewById构造两个对象myButton1、myButton2。具体代码如下:
private Button myButton1;
private Button myButton2;
private File fileDir;
private File sdcardDir;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton1 = (Button) findViewById(R.id.myButton1);
myButton2 = (Button) findViewById(R.id.myButton2);
使用getFilesDir()方法取得SD卡的目录,设置当SD卡没有插入时按钮myButton2处于不能用的状态,具体代码如下:
/* 取得目前File目录 */
fileDir = this.getFilesDir();
/* 取得SD Card目录 */
sdcardDir = Environment.getExternalStorageDirectory();
/* 当SD Card无插入时将myButton2设成不能按 */
if (Environment.getExternalStorageState().equals(Environment.MEDIA_REMOVED))
{
myButton2.setEnabled(false);
}
分别定义按钮单击事件setOnClickListene,代码实现如下:
myButton1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0)
{
String path = fileDir.getParent() + java.io.File.separator
+ fileDir.getName();
showListActivity(path);
}
});
myButton2.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0)
{
String path = sdcardDir.getParent() + sdcardDir.getName();
showListActivity(path);
}
});
}
在showListActivity方法中定义了一个Intent对象intent,然后将路径上传到content_1,具体代码如下:
private void showListActivity(String path)
{
Intent intent = new Intent();
intent.setClass(EX046.this, EX046_1.class);
Bundle bundle = new Bundle();
/* 将路径传到example111_1 */
bundle.putString("path", path);
intent.putExtras(bundle);
startActivity(intent);
}
}
step2:编写content_1.java文件:
将主Activity传来的path字符串作为传入路径,如果路径不存在,则使用java,io,File来创建,具体实现如下:
private List<String> items = null;
private String path;
protected final static int MENU_NEW = Menu.FIRST;
protected final static int MENU_DELETE = Menu.FIRST + 1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.ex111_1);
Bundle bunde = this.getIntent().getExtras();
path = bunde.getString("path");
java.io.File file = new java.io.File(path);
/* 当该目录不存在时将目录创建 */
if (!file.exists())
{
file.mkdir();
}
fill(file.listFiles());
}<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>
定义onOptionItemSelected,根据用户选择的MENU选项分别实现添加或删除操作,代码如下:
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch (item.getItemId())
{
case MENU_NEW:
/* 点击添加MENU */
showListActivity(path, "", "");
break;
case MENU_DELETE:
/* 点击删除MENU */
deleteFile();
break;
}
return true;
}
onCreateOptionMenu定义来添加需要的MENU,具体如下:
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
/* 添加MENU */
menu.add(Menu.NONE, MENU_NEW, 0, R.string.strNewMenu);
menu.add(Menu.NONE, MENU_DELETE, 0, R.string.strDeleteMenu);
return true;
}
当单击某一个文件时获取此文件的内容,代码如下:
protected void onListItemClick
(ListView l, View v, int position, long id)
{
File file = new File
(path + java.io.File.separator + items.get(position));
/* 点击文件取得文件内容 */
if (file.isFile())
{
String data = "";
try
{
FileInputStream stream = new FileInputStream(file);
StringBuffer sb = new StringBuffer();
int c;
while ((c = stream.read()) != -1)
{
sb.append((char) c);
}
stream.close();
data = sb.toString();
}
catch (Exception e)
{
e.printStackTrace();
}
showListActivity(path, file.getName(), data);
}
}
使用fill(Fill[] files)方法将内容填充到文件:
private void fill(File[] files)
{
items = new ArrayList<String>();
if (files == null)
{
return;
}
for (File file : files)
{
items.add(file.getName());
}
ArrayAdapter<String> fileList = new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1, items);
setListAdapter(fileList);
}
使用showListActivity方法来显示已经存在的文件列表:
private void showListActivity
(String path, String fileName, String data)
{
Intent intent = new Intent();
intent.setClass(EX046_1.this, EX046_2.class);
Bundle bundle = new Bundle();
/* 文件路径 */
bundle.putString("path", path);
/* 文件名 */
bundle.putString("fileName", fileName);
/* 文件内容 */
bundle.putString("data", data);
intent.putExtras(bundle);
startActivity(intent);
}
使用deleteFile()方法来显示用户选定的文件:
private void deleteFile()
{
int position = this.getSelectedItemPosition();
if (position >= 0)
{
File file = new File(path + java.io.File.separator +
items.get(position));
/* 删除文件 */
file.delete();
items.remove(position);
getListView().invalidateViews();
}
}
}
step3:编写content_2.java
使用myEditText对象来放置文件内容,定义Bundle对象bundle来获取路径path和数据data
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.ex111_2);
/* 放置文件内容的EditText */
myEditText1 = (EditText) findViewById(R.id.myEditText1);
Bundle bunde = this.getIntent().getExtras();
path = bunde.getString("path");
data = bunde.getString("data");
fileName = bunde.getString("fileName");
myEditText1.setText(data);
}
使用onOptionItemSelected根据用户选择进行操作,当选择MENU_SAVE时,保存这个文件;
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch (item.getItemId())
{
case MENU_SAVE:
saveFile();
break;
}
return true;
}
使用onCreateOptionMenu(Menu menu)来添加一个MENU:
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
/* 添加MENU */
menu.add(Menu.NONE, MENU_SAVE, 0, R.string.strSaveMenu);
return true;
}
定义saveFile()来保存文件。先定义LayoutInflater对象factory来跳出并存档,然后通过myDialogEditText来获取Dialog对话框中的EditText数据,实现存档处理:
private void saveFile()
{
/* 跳出存档的Dialog */
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate
(R.layout.dialog1, null);
Builder mBuilder1 = new AlertDialog.Builder(EX046_2.this);
mBuilder1.setView(textEntryView);
/* 取得Dialog里的EditText */
myDialogEditText = (EditText) textEntryView.findViewById
(R.id.myDialogEditText);
myDialogEditText.setText(fileName);
mBuilder1.setPositiveButton
(
R.string.str_alert_ok,new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{
/* 存档 */
String Filename = path + java.io.File.separator
+ myDialogEditText.getText().toString();
java.io.BufferedWriter bw;
try
{
bw = new java.io.BufferedWriter(new java.io.FileWriter(
new java.io.File(Filename)));
String str = myEditText1.getText().toString();
bw.write(str, 0, str.length());
bw.newLine();
bw.close();
}
catch (IOException e)
{
e.printStackTrace();
}
/* 返回到example111_1 */
Intent intent = new Intent();
intent.setClass(EX046_2.this, EX046_1.class);
Bundle bundle = new Bundle();
/* 将路径传到example111_1 */
bundle.putString("path", path);
intent.putExtras(bundle);
startActivity(intent);
finish();
}
});
mBuilder1.setNegativeButton(R.string.str_alert_cancel, null);
mBuilder1.show();
}
}