Android開發的文件瀏覽器

openkk 12年前發布 | 36K 次閱讀 Android Android開發 移動開發

源碼下載地址:

 

http://download.csdn.net/detail/nuptboyzhb/4485545

 

該文件瀏覽器極易擴展,文件瀏覽器實現的功能是:主UI顯示用戶選擇的文件的路徑。

完成這一功能的主要是FXExplore.java文件。如下圖解析:

Android開發的文件瀏覽器

SelectFilesActivity.java文件

 

package com.example.com.njupt.zhb.selectfiles;

import android.os.Bundle; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; / @作者:鄭海波 http://blog.csdn.net/nuptboyzhb / public class SelectFilesActivity extends Activity { private Button selectBtn; private TextView pathView; private static final String DYNAMICACTION = "njupt.zhb.sendpath"; private OnClickListener listener=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_select_files); listener=new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent=new Intent(SelectFilesActivity.this,FSExplorer.class);
            startActivity(intent);
        }
    };
    selectBtn=(Button)findViewById(R.id.selectFilesBtn);
    pathView=(TextView)findViewById(R.id.filepath);
    selectBtn.setOnClickListener(listener);
    IntentFilter filter_dynamic = new IntentFilter();
    filter_dynamic.addAction(DYNAMICACTION);
    registerReceiver(dynamicReceiver, filter_dynamic);
}
// 2 自定義動態廣播接收器,內部類,接收選擇的路徑
private BroadcastReceiver dynamicReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("MainActivity", "接收自定義動態注冊廣播消息");
        if(intent.getAction().equals(DYNAMICACTION)){
            String path = intent.getStringExtra("path");
            Toast.makeText(context, path, Toast.LENGTH_SHORT).show();
            String text="Path:"+path;
            pathView.setText(text);
        }
    }
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_select_files, menu);
    return true;
}

}</pre>

FXExplore.java文件


 

package com.example.com.njupt.zhb.selectfiles;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.R.string; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; / @作者:鄭海波 http://blog.csdn.net/nuptboyzhb 參考:Google Android開發入門與實戰/ public class FSExplorer extends Activity implements OnItemClickListener { private static final String TAG = "FSExplorer"; private static final int IM_PARENT = Menu.FIRST + 1; private static final int IM_BACK = IM_PARENT + 1; private static final String DYNAMICACTION = "njupt.zhb.sendpath"; ListView itemlist = null; String path = "/"; List<Map<String, Object>> list; public void sendPathToActivity(String path){ Intent intent = new Intent(); intent.setAction(DYNAMICACTION); intent.putExtra("path", path); sendBroadcast(intent); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.files); setTitle("文件瀏覽器"); itemlist = (ListView) findViewById(R.id.itemlist); refreshListItems(path); } /根據path更新路徑列表/ private void refreshListItems(String path) { setTitle("文件瀏覽器 > "+path); list = buildListForSimpleAdapter(path); SimpleAdapter notes = new SimpleAdapter(this, list, R.layout.file_row, new String[] { "name", "path" ,"img"}, new int[] { R.id.name, R.id.desc ,R.id.img}); itemlist.setAdapter(notes); itemlist.setOnItemClickListener(this); itemlist.setSelection(0); } /根據路徑生成一個包含路徑的列表/ private List<Map<String, Object>> buildListForSimpleAdapter(String path) { File[] files = new File(path).listFiles(); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(files.length); Map<String, Object> root = new HashMap<String, Object>(); root.put("name", "/"); root.put("img", R.drawable.file_root); root.put("path", "go to root directory"); list.add(root); Map<String, Object> pmap = new HashMap<String, Object>(); pmap.put("name", ".."); pmap.put("img", R.drawable.file_paranet); pmap.put("path", "go to paranet Directory"); list.add(pmap); for (File file : files){ Map<String, Object> map = new HashMap<String, Object>(); if(file.isDirectory()){ map.put("img", R.drawable.directory); }else{ map.put("img", R.drawable.file_doc); } map.put("name", file.getName()); map.put("path", file.getPath()); list.add(map); } return list; } /跳轉到上一層/ private void goToParent() { File file = new File(path); File str_pa = file.getParentFile(); if(str_pa == null){ Toast.makeText(FSExplorer.this, "已經是根目錄", Toast.LENGTH_SHORT).show(); refreshListItems(path); }else{ path = str_pa.getAbsolutePath(); refreshListItems(path); } } /實現OnItemClickListener接口/ @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Log.i(TAG, "item clicked! [" + position + "]"); if (position == 0) { path = "/"; refreshListItems(path); }else if(position == 1){ goToParent(); } else { path = (String) list.get(position).get("path"); File file = new File(path); if (file.isDirectory()) refreshListItems(path); else { Toast.makeText(FSExplorer.this,path,Toast.LENGTH_SHORT).show(); sendPathToActivity(path); finish(); }

    }

}

}</pre>

Files.xml文件

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="

<ListView 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:id="@+id/itemlist" />

</LinearLayout></pre>

File_row.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="

<ImageView android:id="@+id/img"
    android:layout_width="32px"
    android:layout_margin="4px"
    android:layout_height="32px"/>

<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical">

    <TextView android:id="@+id/name"
        android:textSize="18sp"
        android:textStyle="bold"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <TextView android:id="@+id/desc"
        android:textSize="14sp"
        android:layout_width="fill_parent"
        android:paddingLeft="10px"
        android:layout_height="wrap_content"/>

</LinearLayout>

</LinearLayout></pre>

AndroidManifest.xml文件

 

<manifest xmlns:android="

<uses-sdk
    android:minSdkVersion="4"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".SelectFilesActivity"
        android:label="@string/title_activity_select_files" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".FSExplorer"> </activity>
</application>

</manifest></pre>轉自:http://blog.csdn.net/nuptboyzhb/article/details/7844893

 


 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!