最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android編程實(shí)現(xiàn)簡單文件瀏覽器功能

 更新時(shí)間:2018年01月22日 11:38:35   作者:村頭那摳腳大叔  
這篇文章主要介紹了Android編程實(shí)現(xiàn)簡單文件瀏覽器功能,結(jié)合實(shí)例形式分析了Android文件管理器的布局、文件與目錄的遍歷、權(quán)限控制等相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Android編程實(shí)現(xià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開發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Android TabHost如何實(shí)現(xiàn)頂部選項(xiàng)卡

    Android TabHost如何實(shí)現(xiàn)頂部選項(xiàng)卡

    這篇文章主要介紹了Android TabHost如何實(shí)現(xiàn)頂部選項(xiàng)卡,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Android如何為按鍵添加聲音

    Android如何為按鍵添加聲音

    這篇文章主要告訴大家Android為按鍵添加聲音的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • android自定義Dialog彈框和背景陰影顯示效果

    android自定義Dialog彈框和背景陰影顯示效果

    這篇文章主要為大家詳細(xì)介紹了android自定義Dialog彈框和背景陰影顯示效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Android實(shí)現(xiàn)多次閃退清除數(shù)據(jù)

    Android實(shí)現(xiàn)多次閃退清除數(shù)據(jù)

    這篇文章主要介紹了Android實(shí)現(xiàn)多次閃退清除數(shù)據(jù)的相關(guān)資料,感興趣的小伙伴們可以參考一下
    2016-04-04
  • 老生常談Listview中onItemClick中的各個(gè)參數(shù)(推薦)

    老生常談Listview中onItemClick中的各個(gè)參數(shù)(推薦)

    下面小編就為大家?guī)硪黄仙U凩istview中onItemClick中的各個(gè)參數(shù)(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • 詳解ASP.NET Core MVC四種枚舉綁定方式

    詳解ASP.NET Core MVC四種枚舉綁定方式

    這篇文章主要介紹了詳解ASP.NET Core MVC四種枚舉綁定方式, 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • Kotlin fun函數(shù)使用方法

    Kotlin fun函數(shù)使用方法

    函數(shù)是執(zhí)行特定任務(wù)的一組相互關(guān)聯(lián)的代碼塊。函數(shù)用于將程序分解為不同的子模塊。它使代碼可重用,并使程序更易于管理,這篇文章主要介紹了Kotlin fun函數(shù)使用方法
    2022-12-12
  • 詳解Android提交數(shù)據(jù)到服務(wù)器的兩種方式四種方法

    詳解Android提交數(shù)據(jù)到服務(wù)器的兩種方式四種方法

    本篇文章主要介紹了Android提交數(shù)據(jù)到服務(wù)器的兩種方式四種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2016-11-11
  • Android Rreact Native 常見錯(cuò)誤總結(jié)

    Android Rreact Native 常見錯(cuò)誤總結(jié)

    這篇文章主要介紹了Android Rreact Native 常見錯(cuò)誤總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Android View 繪制流程(Draw)全面解析

    Android View 繪制流程(Draw)全面解析

    這篇文章主要為大家全面解析了Android View 繪制流程Draw,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02

最新評(píng)論

绥江县| 筠连县| 衡山县| 敦煌市| 凌源市| 澜沧| 三门县| 西昌市| 孝感市| 承德市| 明水县| 门源| 蕲春县| 永川市| 海阳市| 肥西县| 安阳市| 府谷县| 兴义市| 枣庄市| 阳泉市| 永宁县| 金堂县| 长治县| 萨嘎县| 台中市| 蒲城县| 兴仁县| 陵川县| 萨嘎县| 永新县| 上林县| 阳原县| 宜良县| 土默特右旗| 江永县| 原阳县| 迁西县| 米泉市| 遂宁市| 房产|