当前位置: 移动技术网 > IT编程>移动开发>Android > Android中调用系统的文件浏览器及自制简单的文件浏览器

Android中调用系统的文件浏览器及自制简单的文件浏览器

2019年07月24日  | 移动技术网IT编程  | 我要评论

斩仙5200,w万年历,chinafoyou

调用系统自带的文件浏览器
这很简单:

/** 调用文件选择软件来选择文件 **/ 
private void showfilechooser() { 
  intent = new intent(intent.action_get_content); 
  intent.settype("*/*"); 
  intent.addcategory(intent.category_openable); 
  try { 
    startactivityforresult(intent.createchooser(intent, "请选择一个要上传的文件"), 
        file_select_code); 
  } catch (android.content.activitynotfoundexception ex) { 
    // potentially direct the user to the market with a dialog 
    toast.maketext(getactivity(), "请安装文件管理器", toast.length_short) 
        .show(); 
  } 
} 

在catch,我们可以做更多的操作,比如会跳转到一个下载文件管理器的页面或者等等。

对于返回的数据怎么处理呢。我项目中的上传是如下接收:

/** 根据返回选择的文件,来进行上传操作 **/ 
  @override 
  public void onactivityresult(int requestcode, int resultcode, intent data) { 
    // todo auto-generated method stub 
    if (resultcode == activity.result_ok) { 
      // get the uri of the selected file 
      uri uri = data.getdata(); 
      string url; 
      try { 
        url = ffileutils.getpath(getactivity(), uri); 
        log.i("ht", "url" + url); 
        string filename = url.substring(url.lastindexof("/") + 1); 
        intent = new intent(getactivity(), uploadservices.class); 
        intent.putextra("filename", filename); 
        intent.putextra("url", url); 
        intent.putextra("type ", ""); 
        intent.putextra("fuid", ""); 
        intent.putextra("type", ""); 
 
        getactivity().startservice(intent); 
 
      } catch (urisyntaxexception e) { 
        // todo auto-generated catch block 
        e.printstacktrace(); 
      } 
    } 
    super.onactivityresult(requestcode, resultcode, data); 
  } 

   
自制文件浏览器:
这里只加一些简单的图形:

2016424103446719.jpg (412×563)

来看代码:

<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> 


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; 
  //当前文件目录 
  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("当前目录为:"+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; 
  } 
 
} 


如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网