Android編程實(shí)現(xiàn)簡(jiǎn)單文件瀏覽器功能
本文實(shí)例講述了Android編程實(shí)現(xiàn)簡(jiǎn)單文件瀏覽器功能。分享給大家供大家參考,具體如下:
運(yùn)行效果:
布局:
<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="wrap_content" android:orientation="vertical" android:layout_gravity="center_horizontal" tools:context=".MainActivity" > <TextView android:id="@+id/txt1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageButton android:id="@+id/imageBt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/home"/> <ListView android:id="@+id/listFile" android:layout_width="wrap_content" android:layout_height="wrap_content" > </ListView> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ImageView android:id="@+id/images" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/txtview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
java代碼:
package com.android.xiong.sdfilelook; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; public class MainActivity extends Activity { private ListView listfile; //當(dāng)前文件目錄 private String currentpath; private TextView txt1; private ImageView images; private TextView textview; private ImageButton imagebt1; private int[] img = { R.drawable.file, R.drawable.folder, R.drawable.home }; private File[] files; private SimpleAdapter simple; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listfile = (ListView) findViewById(R.id.listFile); txt1 = (TextView) findViewById(R.id.txt1); imagebt1 = (ImageButton) findViewById(R.id.imageBt1); init(Environment.getExternalStorageDirectory()); listfile.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub // 獲取單擊的文件或文件夾的名稱 String folder = ((TextView) arg1.findViewById(R.id.txtview)) .getText().toString(); try { File filef = new File(currentpath + '/' + folder); init(filef); } catch (Exception e) { e.printStackTrace(); } } }); //回根目錄 imagebt1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { init(Environment.getExternalStorageDirectory()); } }); } // 界面初始化 public void init(File f) { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { // 獲取SDcard目錄下所有文件名 files = f.listFiles(); if (!files.equals(null)) { currentpath=f.getPath(); txt1.setText("當(dāng)前目錄為:"+f.getPath()); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); for (int i = 0; i < files.length; i++) { Map<String, Object> maps = new HashMap<String, Object>(); if (files[i].isFile()) maps.put("image", img[0]); else maps.put("image", img[1]); maps.put("filenames", files[i].getName()); list.add(maps); } simple = new SimpleAdapter(this, list, R.layout.fileimageandtext, new String[] { "image", "filenames" }, new int[] { R.id.images, R.id.txtview }); listfile.setAdapter(simple); } } else { System.out.println("該文件為空"); } } @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; } }
權(quán)限控制:
<!-- 在SDCard中創(chuàng)建與刪除文件權(quán)限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <!-- 往SDCard寫入數(shù)據(jù)權(quán)限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
更多關(guān)于Android權(quán)限控制可參考Android權(quán)限描述大全
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android文件操作技巧匯總》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android布局layout技巧總結(jié)》、《Android開(kāi)發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android編寫文件瀏覽器簡(jiǎn)單實(shí)現(xiàn)
- Android中調(diào)用系統(tǒng)的文件瀏覽器及自制簡(jiǎn)單的文件瀏覽器
- 微信或手機(jī)瀏覽器在線顯示office文件(已測(cè)試ios、android)
- 讀寫Android中assets目錄下的文件的方法詳解
- Android如何遍歷特定目錄下所有文件
- Android遍歷所有文件夾和子目錄搜索文件
- 讀取android根目錄下的文件或文件夾實(shí)例
- Android 將文件下載到指定目錄的實(shí)現(xiàn)代碼
- Android編程實(shí)現(xiàn)將壓縮數(shù)據(jù)庫(kù)文件拷貝到安裝目錄的方法
- Android開(kāi)發(fā)實(shí)現(xiàn)讀取assets目錄下db文件的方法示例
相關(guān)文章
基于RxJava實(shí)現(xiàn)酷炫啟動(dòng)頁(yè)
本文介紹怎樣利用RxJava來(lái)實(shí)現(xiàn)Android的啟動(dòng)頁(yè),啟動(dòng)頁(yè)的效果非???,有需要的朋友們可以參考。2016-07-07通過(guò)案例分析Android WindowManager解析與騙取QQ密碼的過(guò)程
Windows Manager是一款窗口管理終端,可以遠(yuǎn)程連接到Linux的X桌面進(jìn)行管理,與服務(wù)器端產(chǎn)生一個(gè)session相互通信,通過(guò)本文給大家分享Android WindowManager解析與騙取QQ密碼的過(guò)程,需要的朋友參考下2016-01-01Android中調(diào)用另一個(gè)Activity并返回結(jié)果(選擇頭像功能為例)
這篇文章主要介紹了Android中調(diào)用另一個(gè)Activity并返回結(jié)果,本文以模擬選擇頭像功能為例通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2020-01-01Android 使用ViewPager實(shí)現(xiàn)圖片左右循環(huán)滑動(dòng)自動(dòng)播放
這篇文章主要介紹了Android 使用ViewPager實(shí)現(xiàn)圖片左右循環(huán)滑動(dòng)自動(dòng)播放的相關(guān)資料,非常不錯(cuò),具有參考解決價(jià)值,需要的朋友可以參考下2016-08-08Android編程實(shí)現(xiàn)扭曲圖像的繪制功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)扭曲圖像的繪制功能,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android圖形扭曲的具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09Android實(shí)現(xiàn)背景圖滑動(dòng)變大松開(kāi)回彈效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)背景圖滑動(dòng)變大松開(kāi)回彈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android中實(shí)現(xiàn)詞組高亮TextView方法示例
高亮顯示大家應(yīng)該都不陌生,在開(kāi)發(fā)中經(jīng)常會(huì)遇到這個(gè)需求,所以下面這篇文章主要給大家介紹了關(guān)于Android中實(shí)現(xiàn)詞組高亮TextView的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-10-10