編輯:關於Android編程
一、資源管理器介紹
現在在一些移動終端上面都會有自帶的資源管理器,其實其並非是Android系統自帶,而是手機產商與app開發商的合作而導致融合,借助第三方的開發軟件預裝在出廠的手機,是新時代下的另一個霸王條款,還不能自行刪除,十分麻煩。
背景鋪墊完畢,由於十分討厭這種不公平的手段,為此自行寫一個實現文件資源管理器,功能基本上實現,實用不美觀,不喜勿噴!
二、實現函數詳解
1、顯示文件列表
/** * 掃描顯示文件列表 * @param path */ private void showFileDir(String path) { mFileName = new ArrayList(); mFilePath = new ArrayList (); File file = new File(path); File[] files = file.listFiles(); //如果當前目錄不是根目錄 if (!ROOT_PATH.equals(path)) { mFileName.add("@1"); mFilePath.add(ROOT_PATH); mFileName.add("@2"); mFilePath.add(file.getParent()); } //添加所有文件 for (File f : files) { mFileName.add(f.getName()); mFilePath.add(f.getPath()); } this.setListAdapter(new MyAdapter(this, mFileName, mFilePath)); }
2、Item點擊事件
/** * 點擊事件 * @param l * @param v * @param position * @param id */ @Override protected void onListItemClick(ListView l, View v, int position, long id) { String path = mFilePath.get(position); File file = new File(path); // 文件存在並可讀 if (file.exists() && file.canRead()) { if (file.isDirectory()) { //顯示子目錄及文件 showFileDir(path); } else { //處理文件 fileHandle(file); } } //沒有權限 else { Resources res = getResources(); new AlertDialog.Builder(this).setTitle("Message") .setMessage(res.getString(R.string.no_permission)) .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "沒有權限刪除不了..", Toast.LENGTH_SHORT).show(); } }).show(); } super.onListItemClick(l, v, position, id); }
3、對文件進行重命名
public void onClick(DialogInterface dialog, int which) { // 打開文件 if (which == 0) { openFile(file); } //修改文件名 else if (which == 1) { LayoutInflater factory = LayoutInflater.from(MainActivity.this); view = factory.inflate(R.layout.rename_dialog, null); editText = (EditText) view.findViewById(R.id.editText); editText.setText(file.getName()); DialogInterface.OnClickListener listener2 = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub String modifyName = editText.getText().toString(); final String fpath = file.getParentFile().getPath(); final File newFile = new File(fpath + "/" + modifyName); if (newFile.exists()) { //排除沒有修改情況 if (!modifyName.equals(file.getName())) { new AlertDialog.Builder(MainActivity.this) .setTitle("注意!") .setMessage("文件名已存在,是否覆蓋?") .setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (file.renameTo(newFile)) { showFileDir(fpath); displayToast("重命名成功!"); } else { displayToast("重命名失敗!"); } } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .show(); } } else { if (file.renameTo(newFile)) { showFileDir(fpath); displayToast("重命名成功!"); } else { displayToast("重命名失敗!"); } } } }; AlertDialog renameDialog = new AlertDialog.Builder(MainActivity.this).create(); renameDialog.setView(view); renameDialog.setButton("確定", listener2); renameDialog.setButton2("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); renameDialog.show(); }
4、對文件進行刪除
public void onClick(DialogInterface dialog, int which) { // 打開文件 if (which == 0) { openFile(file); } //修改文件名 else if (which == 1) { LayoutInflater factory = LayoutInflater.from(MainActivity.this); view = factory.inflate(R.layout.rename_dialog, null); editText = (EditText) view.findViewById(R.id.editText); editText.setText(file.getName()); DialogInterface.OnClickListener listener2 = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub String modifyName = editText.getText().toString(); final String fpath = file.getParentFile().getPath(); final File newFile = new File(fpath + "/" + modifyName); if (newFile.exists()) { //排除沒有修改情況 if (!modifyName.equals(file.getName())) { new AlertDialog.Builder(MainActivity.this) .setTitle("注意!") .setMessage("文件名已存在,是否覆蓋?") .setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (file.renameTo(newFile)) { showFileDir(fpath); displayToast("重命名成功!"); } else { displayToast("重命名失敗!"); } } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .show(); } } else { if (file.renameTo(newFile)) { showFileDir(fpath); displayToast("重命名成功!"); } else { displayToast("重命名失敗!"); } } } }; AlertDialog renameDialog = new AlertDialog.Builder(MainActivity.this).create(); renameDialog.setView(view); renameDialog.setButton("確定", listener2); renameDialog.setButton2("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); renameDialog.show(); }
5、打開文件
//打開文件 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); }
6、onCreateOptionsMenu重寫,實現新建文件夾
public static void createMkdir(String folderPath) { File folder = new File(folderPath); if (!folder.exists()) { folder.mkdir(); } }
三、實現步驟
1、activity_main.xml,主界面布局,最重要的既是ListView浏覽方式。
2、file.xml,Item的布局方式。
3、rename_dialog.xml,自定義Dialog方式。
4、MainActivity.java,主Activity,所有事項函數。
package example.com.filemanager; import android.app.AlertDialog; import android.app.ListActivity; import android.content.DialogInterface; import android.content.Intent; import android.content.res.Resources; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; import android.widget.ListView; import android.widget.Toast; import java.io.File; import java.util.ArrayList; public class MainActivity extends ListActivity { private static final String ROOT_PATH = "/"; //存儲文件名稱 private ArrayListmFileName = null; //存儲文件路徑 private ArrayList mFilePath = null; //重命名布局xml文件顯示dialog private View view; private EditText editText; /** * Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //顯示文件列表 showFileDir(ROOT_PATH); } /** * 掃描顯示文件列表 * @param path */ private void showFileDir(String path) { mFileName = new ArrayList (); mFilePath = new ArrayList (); File file = new File(path); File[] files = file.listFiles(); //如果當前目錄不是根目錄 if (!ROOT_PATH.equals(path)) { mFileName.add("@1"); mFilePath.add(ROOT_PATH); mFileName.add("@2"); mFilePath.add(file.getParent()); } //添加所有文件 for (File f : files) { mFileName.add(f.getName()); mFilePath.add(f.getPath()); } this.setListAdapter(new MyAdapter(this, mFileName, mFilePath)); } /** * 點擊事件 * @param l * @param v * @param position * @param id */ @Override protected void onListItemClick(ListView l, View v, int position, long id) { String path = mFilePath.get(position); File file = new File(path); // 文件存在並可讀 if (file.exists() && file.canRead()) { if (file.isDirectory()) { //顯示子目錄及文件 showFileDir(path); } else { //處理文件 fileHandle(file); } } //沒有權限 else { Resources res = getResources(); new AlertDialog.Builder(this).setTitle("Message") .setMessage(res.getString(R.string.no_permission)) .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "沒有權限刪除不了..", Toast.LENGTH_SHORT).show(); } }).show(); } super.onListItemClick(l, v, position, id); } //對文件進行增刪改 private void fileHandle(final File file) { DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 打開文件 if (which == 0) { openFile(file); } //修改文件名 else if (which == 1) { LayoutInflater factory = LayoutInflater.from(MainActivity.this); view = factory.inflate(R.layout.rename_dialog, null); editText = (EditText) view.findViewById(R.id.editText); editText.setText(file.getName()); DialogInterface.OnClickListener listener2 = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub String modifyName = editText.getText().toString(); final String fpath = file.getParentFile().getPath(); final File newFile = new File(fpath + "/" + modifyName); if (newFile.exists()) { //排除沒有修改情況 if (!modifyName.equals(file.getName())) { new AlertDialog.Builder(MainActivity.this) .setTitle("注意!") .setMessage("文件名已存在,是否覆蓋?") .setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (file.renameTo(newFile)) { showFileDir(fpath); displayToast("重命名成功!"); } else { displayToast("重命名失敗!"); } } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .show(); } } else { if (file.renameTo(newFile)) { showFileDir(fpath); displayToast("重命名成功!"); } else { displayToast("重命名失敗!"); } } } }; AlertDialog renameDialog = new AlertDialog.Builder(MainActivity.this).create(); renameDialog.setView(view); renameDialog.setButton("確定", listener2); renameDialog.setButton2("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); renameDialog.show(); } //刪除文件 else { new AlertDialog.Builder(MainActivity.this) .setTitle("注意!") .setMessage("確定要刪除此文件嗎?") .setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (file.delete()) { //更新文件列表 showFileDir(file.getParent()); displayToast("刪除成功!"); } else { displayToast("刪除失敗!"); } } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).show(); } } }; //選擇文件時,彈出增刪該操作選項對話框 String[] menu = {"打開文件", "重命名", "刪除文件"}; new AlertDialog.Builder(MainActivity.this) .setTitle("請選擇要進行的操作!") .setItems(menu, listener) .setPositiveButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).show(); } //打開文件 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 private String getMIMEType(File file) { String type = ""; String name = file.getName(); //文件擴展名 String end = name.substring(name.lastIndexOf(".") + 1, name.length()).toLowerCase(); if (end.equals("m4a") || end.equals("mp3") || end.equals("wav")) { type = "audio"; } else if (end.equals("mp4") || end.equals("3gp")) { type = "video"; } else if (end.equals("jpg") || end.equals("png") || end.equals("jpeg") || end.equals("bmp") || end.equals("gif")) { type = "image"; } else { //如果無法直接打開,跳出列表由用戶選擇 type = "*"; } type += "/*"; return type; } private void displayToast(String message) { Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show(); } public boolean onCreateOptionsMenu(Menu menu) { //super.getMenuInflater().inflate(R.menu.main_menu, menu); menu.add(Menu.NONE, Menu.FIRST + 1, 7, "新建") .setIcon(android.R.drawable.ic_menu_send); menu.add(Menu.NONE, Menu.FIRST + 2, 3, "取消") .setIcon(android.R.drawable.ic_menu_edit); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case Menu.FIRST + 1:{ final EditText editText = new EditText(MainActivity.this); new AlertDialog.Builder(MainActivity.this).setTitle("請輸入文件名稱").setView(editText).setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub String mFileName = editText.getText().toString(); String IMAGES_PATH = Environment.getExternalStorageDirectory() + "/" + mFileName + "/"; //獲取根目錄 //String IMAGES_PATH = getApplicationContext().getFilesDir().getAbsolutePath() + "/" + mFileName + "/"; createMkdir(IMAGES_PATH); } }).setNegativeButton("取消", null).show(); } break; case Menu.FIRST + 2: Toast.makeText(this, "取消", Toast.LENGTH_LONG).show(); break; } return false; } public static void createMkdir(String folderPath) { File folder = new File(folderPath); if (!folder.exists()) { folder.mkdir(); } } public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { finish(); return true; } else { return super.onKeyDown(keyCode, event); } } }
5、MyAdapter.java
package example.com.filemanager; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import java.io.File; import java.util.ArrayList; /** * Title: FileManager * Package example.com.filemanager * Description: ${TODO} * author Jimmy.li * Date: 2016-06-23 * Time: 14:16 * version V1.0 */ public class MyAdapter extends BaseAdapter { private LayoutInflater inflater; private Bitmap directory, file; //存儲文件名稱 private ArrayListnames = null; //存儲文件路徑 private ArrayList paths = null; //參數初始化 public MyAdapter(Context context, ArrayList na, ArrayList pa) { names = na; paths = pa; directory = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher); file = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher); //縮小圖片 directory = small(directory, 0.16f); file = small(file, 0.1f); inflater = LayoutInflater.from(context); } @Override public int getCount() { // TODO Auto-generated method stub return names.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return names.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder; if (null == convertView) { convertView = inflater.inflate(R.layout.file, null); holder = new ViewHolder(); holder.text = (TextView) convertView.findViewById(R.id.textView); holder.image = (ImageView) convertView.findViewById(R.id.imageView); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } File f = new File(paths.get(position).toString()); if (names.get(position).equals("@1")) { holder.text.setText("/"); holder.image.setImageBitmap(directory); } else if (names.get(position).equals("@2")) { holder.text.setText(".."); holder.image.setImageBitmap(directory); } else { holder.text.setText(f.getName()); if (f.isDirectory()) { holder.image.setImageBitmap(directory); } else if (f.isFile()) { holder.image.setImageBitmap(file); } else { System.out.println(f.getName()); } } return convertView; } private class ViewHolder { private TextView text; private ImageView image; } private Bitmap small(Bitmap map, float num) { Matrix matrix = new Matrix(); matrix.postScale(num, num); return Bitmap.createBitmap(map, 0, 0, map.getWidth(), map.getHeight(), matrix, true); } }
四、添加權限
五、實現效果圖
使用PopupWindow來實現彈出框,並且帶有動畫效果首先自定義PopupWindowpublic class LostPopupWindow extends Popu
平時對牛逼動畫,高級UI,都深入的不多!近日,某頭條,推了一個android技術類視頻(平時在頭條關注技術比較多),講的是加載動畫效果,是動腦學院講的公開課,160分鐘,
一、在AndroidStudio下配置androidannotations首先,准備好你的project。AndroidStudio下的androidannotation
每家公司的app都有自己的統一的特色,這就涉及到theme和style,當然他們只不是僅僅為了這個特點存在,想activity的進入動畫等都能通過theme和style實