仿手机文件夹管理器
仿手机文件夹管理器,可以打开显示SD卡下是文件,可以进入下一级和返回上一级。
效果图:
关键代码入下:
import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import com.stcyclub.zhimeng.R; import com.stcyclub.zhimeng.R.drawable; import com.stcyclub.zhimeng.R.id; import com.stcyclub.zhimeng.R.layout; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.content.Intent; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.Window; import android.widget.AdapterView; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class LocationFileLiabraryActivity extends Activity { private TextView tvpath; private ListView lvFiles; private Button btnParent; // 记录当前的父文件夹 File currentParent; // 记录当前路径下的所有文件夹的文件数组 File[] currentFiles; String SDCardPath; String folderPath; public String getSDPath() { File sdDir = null; boolean sdCardExist = Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在 if (sdCardExist) { sdDir = Environment.getExternalStorageDirectory();// 获取根目录 } return sdDir.toString(); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_sdcard_image); // Bundle bundle = new Bundle(); // bundle = this.getIntent().getExtras(); // if (bundle != null) { // imageORresfile = bundle.getBoolean("imageORresfile"); // } lvFiles = (ListView) this.findViewById(R.id.listView); tvpath = (TextView) this.findViewById(R.id.tvpath); btnParent = (Button) this.findViewById(R.id.btnParent); SDCardPath = getSDPath(); folderPath = SDCardPath + "/"; // 获取系统的SDCard的目录 File root = new File(SDCardPath); // 如果SD卡存在的话imageORresfile ture 获取图片 false获取文件 if (root.exists()) { // if (imageORresfile) { // currentParent = root; // currentFiles = root.listFiles(); // // } else { currentParent = new File(folderPath); currentFiles = currentParent.listFiles(); // } // 使用当前目录下的全部文件、文件夹来填充ListView inflateListView(currentFiles); } lvFiles.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { // 如果用户单击了文件,直接返回,不做任何处理 if (currentFiles[position].isFile()) { // 也可自定义扩展打开这个文件等 String name = currentFiles[position].getName(); //Log.d("TAG", "file...name.0...."+name); int index=name.lastIndexOf("."); //Log.d("TAG", "index...name.0...."+index); if(index>0){ String suffix=name.substring(index+1, name.length()); Log.d("TAG", "suffix.0...."+suffix); if (!(suffix.equals("exe")||suffix.equals("bat"))) { String imagePath = new String(); try { imagePath = currentParent.getCanonicalPath() + "/" + name; } catch (IOException e) { e.printStackTrace(); } Intent intent = new Intent(); // intent.setClass(SDCardImageActivity.this, // ChatActivity.class); Bundle bundle = new Bundle(); bundle.putString("filePath", imagePath); intent.putExtras(bundle); LocationFileLiabraryActivity.this.setResult(10002, intent); LocationFileLiabraryActivity.this.finish(); overridePendingTransition(R.anim.in_from_left, R.anim.out_to_rigth); } else{ Toast.makeText(LocationFileLiabraryActivity.this, "格式不支持", Toast.LENGTH_LONG).show(); } }else{ Toast.makeText(LocationFileLiabraryActivity.this, "格式不支持", Toast.LENGTH_LONG).show(); } return; } // 获取用户点击的文件夹 下的所有文件 File[] tem = currentFiles[position].listFiles(); if (tem == null || tem.length == 0) { Toast.makeText(LocationFileLiabraryActivity.this, "当前路径不可访问或者该路径下没有文件", Toast.LENGTH_LONG).show(); } else { // 获取用户单击的列表项对应的文件夹,设为当前的父文件夹 currentParent = currentFiles[position]; // 保存当前的父文件夹内的全部文件和文件夹 currentFiles = tem; // 再次更新ListView inflateListView(currentFiles); } } }); // 获取上一级目录 btnParent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { if (!currentParent.getCanonicalPath().equals(SDCardPath)) { // 获取上一级目录 currentParent = currentParent.getParentFile(); // 列出当前目录下的所有文件 currentFiles = currentParent.listFiles(); // 再次更新ListView inflateListView(currentFiles); } } catch (Exception e) { // TODO: handle exception } } }); } public void btnClick(View v){ switch (v.getId()) { case R.id.location_file_btn_back: LocationFileLiabraryActivity.this.finish(); overridePendingTransition(R.anim.in_from_left, R.anim.out_to_rigth); break; default: break; } } private void inflateListView(File[] files) { List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>(); for (int i = 0; i < files.length; i++) { Map<String, Object> listItem = new HashMap<String, Object>(); if (files[i].isDirectory()) { // 如果是文件夹就显示的图片为文件夹的图片 listItem.put("icon", R.drawable.folder_picture); } else { String name=files[i].getName(); int index=name.lastIndexOf("."); if(index>0){ String suffix=name.substring(index+1, name.length()); if(suffix.equals("jpg") || suffix.equals("bmp") || suffix.equals("jpeg") || suffix.equals("png")){ listItem.put("icon", R.drawable.picture); }else if(suffix.equals("avi")||suffix.equals("mkv")||suffix.equals("mp4") ||suffix.equals("flv") ||suffix.equals("3gp")||suffix.equals("rmvb")){ listItem.put("icon", R.drawable.video_picture); }else if(suffix.equals("apk")){ listItem.put("icon", R.drawable.apk_picture); }else if(suffix.equals("xls")||suffix.equals("doc")||suffix.equals("txt")){ listItem.put("icon", R.drawable.word_picture); }else if(suffix.equals("zip")||suffix.equals("rar")){ listItem.put("icon", R.drawable.zip_picture); }else if(suffix.equals("mp3")){ listItem.put("icon", R.drawable.music_picture); }else{ listItem.put("icon", R.drawable.unknow_picture); } }else{ listItem.put("icon", R.drawable.unknow_picture); } } // 添加一个文件名称 listItem.put("filename", files[i].getName()); File myFile = new File(files[i].getName()); // 获取文件的最后修改日期 long modTime = myFile.lastModified(); // SimpleDateFormat dateFormat = new SimpleDateFormat( // "yyyy-MM-dd HH:mm:ss"); //System.out.println(dateFormat.format(new Date(modTime))); // 添加一个最后修改日期 // listItem.put("modify", // "修改日期:" + dateFormat.format(new Date(modTime))); listItems.add(listItem); } // 定义一个SimpleAdapter SimpleAdapter adapter = new SimpleAdapter(LocationFileLiabraryActivity.this, listItems, R.layout.sdcard_image_list_item, new String[] { "filename", "icon", "modify" }, new int[] { R.id.file_name, R.id.icon, R.id.file_modify }); // 填充数据集 lvFiles.setAdapter(adapter); try { tvpath.setText("当前路径为:" + currentParent.getCanonicalPath()); } catch (Exception e) { e.printStackTrace(); } } @Override public void onStart() { super.onStart(); } @Override public void onResume() { super.onResume(); } @Override public void onPause() { super.onPause(); } @Override public void onStop() { super.onStop(); } @Override public void onDestroy() { super.onDestroy(); } @Override public void onRestart() { super.onRestart(); } }
xml布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RelativeLayout android:id="@+id/rl_layout" android:layout_width="fill_parent" android:layout_height="@dimen/activity_main_tab_title_height" android:background="@color/black_top" android:layout_alignParentTop="true" android:gravity="center_vertical" > <ImageButton android:id="@+id/location_file_btn_back" android:layout_width="@dimen/activity_btn_height_login_and_register" android:layout_height="fill_parent" android:layout_centerVertical="true" android:background="@drawable/btn_click_goback" android:src="@drawable/goback_left" android:onClick="btnClick" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="文件选择" android:layout_centerInParent="true" android:textSize="@dimen/activity_Textsize20_4.0" android:textColor="#ffffff" /> <Button android:id="@+id/btnParent" android:layout_width="60dp" android:layout_height="30dp" android:layout_alignParentRight="true" android:layout_marginRight="10dp" android:layout_centerInParent="true" android:background="@drawable/btn_click1" android:textColor="@color/login_btn_text_color" android:text="上一级"/> </RelativeLayout> <TextView android:id="@+id/tvpath" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/black20" android:textColor="@color/black" android:textSize="15sp" android:text="文件路径" /> <ListView android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="fill_parent"></ListView> </LinearLayout>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。