android基于文件管理器的文件夹创建和文件夹删除

package com.example.file;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;


import com.example.file.adapter.FileAdapter;


import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;


public class MainActivity extends Activity implements OnClickListener, OnItemClickListener {
private FileAdapter fileAdapter;
private ArrayList<File> files;
// private static final String path="/";
private static final String path = Environment
.getExternalStorageDirectory().getPath();
private ListView listView;
private String A;
private EditText editText;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);


listView = (ListView) findViewById(R.id.list);
editText = (EditText) findViewById(R.id.edit);
findViewById(R.id.newdir).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);


doCreateView(path);
// doGetDataFromPath(path);
// fileAdapter =new FileAdapter(MainActivity.this, files);
// listView.setAdapter(fileAdapter);
}


void doCreateView(String path) {


A = path;
editText.setText(A);
doGetDataFromPath(path);
if (fileAdapter == null) {
fileAdapter = new FileAdapter(files, MainActivity.this);
}else{
fileAdapter.notifyDataSetChanged();

}
listView.setAdapter(fileAdapter);
listView.setOnItemClickListener(this);


}


void doGetDataFromPath(String path) {
// TODO Auto-generated method stub
// 初始化Arraylist,每次执行初始化都要清理
if (files == null) {
files = new ArrayList<File>();
} else {
files.clear();
}
// 根据路径创建目录文件
File file = new File(path);
// 取出目录文件里的所有文件
File[] fileArray = file.listFiles();
// 将数组转换成Arraylist
if (fileArray != null && fileArray.length != 0) {
for (int i = 0; i < fileArray.length; i++) {
files.add(fileArray[i]);


}
}


}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (A.equals(path)) {
MainActivity.this.finish();


} else {
back();
}


}
return true;
}


void back() {
File file = new File(A);
doCreateView(file.getParent());


}


@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;
}


@Override
// 注意传参
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
File file = files.get(arg2);
if (file.canRead()) {
if (file.isDirectory()) {
// 刷新
doCreateView(file.getPath());


} else {
if (file.getName().endsWith(".mp3")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" + file.getAbsolutePath()),
"audio/MP3");
startActivity(intent);
// Toast.makeText(MainActivity.this, "普通文件:"+file.getName(),
// Toast.LENGTH_SHORT).show();

if (file.getName().endsWith(".jpg")||file.getName().endsWith(".png")) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setDataAndType(
Uri.parse("file://" + file.getAbsolutePath()),
"image/JPG");
startActivity(intent);

if (file.getName().endsWith(".ok")) {
try {
FileInputStream fileInputStream = new FileInputStream(
file);
try {
byte[] byteArray = new byte[fileInputStream
.available()];
fileInputStream.read(byteArray);
fileInputStream.close();
String content = new String(byteArray);
Toast.makeText(MainActivity.this, content,
Toast.LENGTH_SHORT).show();


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} else {


Toast.makeText(MainActivity.this, "权限不足:" + file.getName(),
Toast.LENGTH_SHORT).show();
}
listView.setOnItemLongClickListener(new OnItemLongClickListener() {


@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
File deletefile = files.get(arg2);
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setIcon(
MainActivity.this.getResources().getDrawable(
R.drawable.file))
.setTitle("Title")
.setMessage("确定删除" + deletefile.getName().toString() + "?")
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {


@Override
public void onClick(DialogInterface arg0,
int arg1) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT)
.show();
              deleteDir();
doCreateView(A);
}



private void deleteDir() {
// TODO Auto-generated method stub
A = editText.getText().toString().trim();
File dir = new File(A);
if (dir == null || !dir.exists() || !dir.isDirectory())
return;
for (File file : dir.listFiles())
{
file.delete();   
}
dir.delete();


}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {


@Override
public void onClick(DialogInterface arg0,
int arg1) {
// TODO Auto-generated method stub
doCreateView(A);
}
}).create().show();


return true;
}
});


}






@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.button2:
String pathString = editText.getText().toString().trim();
File file = new File(pathString);
if (file.exists()) {
doCreateView(pathString);
} else {
Toast.makeText(MainActivity.this, "路径不正确", Toast.LENGTH_SHORT)
.show();
}


break;
case R.id.button1:
back();
break;
case R.id.newdir:
CreateDir();
break;


case R.id.button3:
String filename = "hello.ok";
String content = "hello android";
File file1 = new File(A, filename);

try {
FileOutputStream fileOutputStream = new FileOutputStream(file1);
fileOutputStream.write(content.getBytes());
fileOutputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block


e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
doCreateView(A);
break;
default:
break;
}
}
private void CreateDir(){
final EditText editText = new EditText(MainActivity.this);
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setIcon(
MainActivity.this.getResources().getDrawable(
R.drawable.file))
.setTitle("新建文件夹")
.setView(editText)
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {


@Override
public void onClick(DialogInterface arg0,
int arg1) {
// TODO Auto-generated method stub
String filename = editText.getText().toString().trim();
String Path= A + "/" + filename;
Toast.makeText(MainActivity.this, "新文件", Toast.LENGTH_SHORT)
.show();
File files = new File(Path);
if(!files.exists())
files.mkdir();
doCreateView(A);
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {


@Override
public void onClick(DialogInterface arg0,
int arg1) {
// TODO Auto-generated method stub
doCreateView(A);
}
}).create().show();


}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值