其中说道要用户选择文件的保存目录,在此展示:
首先看FileBrowserActivity.java
- package com.knowhow.android.client;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.knowhow.android.client.ui.adapter.FileAdapter;
- public class FileBrowserActivity extends KHActivity {
- private List<String> items=null;
- private List<String> paths=null;
- private String rootPath="/sdcard/";
- private TextView mPath;
- private ListView list;
- private FileAdapter m_FileAdapter;
- private void getFileDir(String filePath){
- File f=new File(filePath);
- if(f.exists() && f.canWrite()){
- mPath.setText(filePath);
- items=new ArrayList<String>();
- paths=new ArrayList<String>();
- File[] files=f.listFiles();
- if(!filePath.equals(rootPath)){
- items.add("goroot");
- paths.add(rootPath);
- items.add("goparent");
- paths.add(f.getParent());
- }
- for(int i=0;i<files.length;i++){
- File file=files[i];
- if(file.isDirectory()){
- items.add(file.getName());
- paths.add(file.getPath());
- }
- }
- m_FileAdapter = new FileAdapter(this,items,paths);
- list.setAdapter(m_FileAdapter);
- list.setOnItemClickListener(new OnItemClickListener(){
- @Override
- public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
- if(items.get(position).toString().equals("goparent")){
- getFileDir(paths.get(position));
- }else if(items.get(position).toString().equals("goroot")){
- getFileDir(paths.get(position));
- return;
- }else{
- File file=new File(paths.get(position));
- if(file.canWrite()){
- if (file.isDirectory()){
- getFileDir(paths.get(position));
- }
- }else{
- LinearLayout lay = new LinearLayout(FileBrowserActivity.this);
- lay.setOrientation(LinearLayout.HORIZONTAL);
- ImageView image = new ImageView(FileBrowserActivity.this);
- TextView text = new TextView(FileBrowserActivity.this);
- text.setTextColor(Color.RED);
- text.setTextSize(20);
- text.setText("很抱歉您的权限不足!");
- Toast toast = Toast.makeText(FileBrowserActivity.this, text.getText().toString(), Toast.LENGTH_LONG);
- image.setImageResource(android.R.drawable.stat_sys_warning);
- lay.addView(image);
- lay.addView(text);
- toast.setView(lay);
- toast.show();
- }
- }
- }
- });
- }else{
- LinearLayout lay = new LinearLayout(FileBrowserActivity.this);
- lay.setOrientation(LinearLayout.HORIZONTAL);
- ImageView image = new ImageView(FileBrowserActivity.this);
- TextView text = new TextView(FileBrowserActivity.this);
- text.setTextColor(Color.RED);
- text.setTextSize(20);
- text.setText("无SD卡,无法完成下载!");
- Toast toast = Toast.makeText(FileBrowserActivity.this, text.getText().toString(), Toast.LENGTH_LONG);
- image.setImageResource(android.R.drawable.stat_sys_warning);
- lay.addView(image);
- lay.addView(text);
- toast.setView(lay);
- toast.show();
- this.finish();
- }
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.setContentView(R.layout.filelist);
- mPath = (TextView)this.findViewById(R.id.mPath);
- list = (ListView)this.findViewById(R.id.filelist);
- getFileDir(rootPath);
- mPath.setTextColor(this.getResources().getColor(R.color.text_color));
- this.setTitle("请选择保存目录:");
- Button ok = (Button)this.findViewById(R.id.fileok);
- ok.setPadding(0, 5, 0, 5);
- ok.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- /*if(mPath.getText().toString().equals(rootPath)){
- LinearLayout lay = new LinearLayout(FileBrowserActivity.this);
- lay.setOrientation(LinearLayout.HORIZONTAL);
- ImageView image = new ImageView(FileBrowserActivity.this);
- TextView text = new TextView(FileBrowserActivity.this);
- text.setTextColor(FileBrowserActivity.this.getResources().getColor(R.color.text_color));
- text.setTextSize(16);
- text.setText("很抱歉您的权限不足!");
- Toast toast = Toast.makeText(FileBrowserActivity.this, text.getText().toString(), Toast.LENGTH_SHORT);
- image.setImageResource(android.R.drawable.stat_sys_warning);
- lay.addView(image);
- lay.addView(text);
- toast.setView(lay);
- toast.show();
- }else{*/
- Intent i = new Intent();
- Bundle b = new Bundle();
- b.putString("savePath", mPath.getText().toString());
- b.putString("url", FileBrowserActivity.this.getIntent().getStringExtra("url"));
- b.putString("fileName", FileBrowserActivity.this.getIntent().getStringExtra("fileName"));
- i.putExtras(b);
- FileBrowserActivity.this.setResult(RESULT_OK, i);
- FileBrowserActivity.this.finish();
- //}
- }
- });
- Button cancel = (Button)this.findViewById(R.id.filecancel);
- cancel.setPadding(0, 5, 0, 5);
- cancel.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- FileBrowserActivity.this.finish();
- }
- });
- }
- }
filelist.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent" android:layout_height="fill_parent"
- android:orientation="vertical" android:background="@drawable/white">
- <LinearLayout android:layout_width="fill_parent" android:gravity="center"
- android:layout_height="wrap_content" android:orientation="horizontal">
- <Button android:layout_weight="1.0" android:textColor="@color/button_text_color"
- android:textSize="16dip" android:background="@drawable/bg_alibuybutton_states"
- android:layout_height="wrap_content" android:layout_width="wrap_content"
- android:id="@+id/fileok"
- android:text="确定" />
- <Button android:layout_marginLeft="10dip"
- android:layout_weight="1.0" android:textColor="@color/button_text_color"
- android:textSize="16dip" android:background="@drawable/bg_alibuybutton_states"
- android:layout_height="wrap_content" android:layout_width="wrap_content"
- android:id="@+id/filecancel"
- android:text="取消"></Button>
- </LinearLayout>
- <TextView android:id="@+id/mPath" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:padding="5px"
- android:textSize="18sp" android:textColor="@drawable/blue" />
- <ListView android:id="@+id/filelist" android:layout_width="wrap_content"
- android:cacheColorHint="#00000000"
- android:layout_height="wrap_content" />
- </LinearLayout>
file_row.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout android:id="@+id/file_items"
- android:layout_width="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_height="wrap_content"
- android:paddingBottom="4dip"
- android:paddingLeft="12dip"
- android:paddingRight="12dip">
- <ImageView android:id="@+id/fileicon"
- android:layout_height="wrap_content"
- android:textSize="26dip"
- android:layout_alignParentLeft="true"
- android:layout_width="wrap_content"
- android:layout_below="@+id/file_items"/>
- <TextView android:id="@+id/filetext"
- android:layout_gravity="center_vertical"
- android:layout_width="fill_parent"
- android:layout_toRightOf="@id/fileicon"
- android:paddingLeft="20dip"
- android:layout_height="wrap_content"/>
- <CheckBox android:id="@+id/file_check"
- style="?android:attr/starStyle"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:layout_alignParentRight="true"
- android:focusable="false"
- android:focusableInTouchMode="false"
- android:clickable="false"
- android:layout_below="@+id/file_items"/>
- </RelativeLayout>
FileAdapter.java
- package com.knowhow.android.client.ui.adapter;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.CheckBox;
- import android.widget.ImageView;
- import android.widget.TextView;
- import com.knowhow.android.client.R;
- public class FileAdapter extends BaseAdapter{
- private LayoutInflater mInflater;
- private Bitmap mIcon1;
- private Bitmap mIcon2;
- private Bitmap mIcon3;
- private Bitmap mIcon4;
- private List<String> items;
- private List<String> paths;
- private Map<Integer, Boolean> isSelected;
- private Map<Integer, Integer> isVisibility;
- public Map<Integer, Integer> getIsVisibility() {
- return isVisibility;
- }
- private List<ViewHolder> VHs = new ArrayList<ViewHolder>();
- private Context context;
- public Map<Integer, Boolean> getIsSelected() {
- return isSelected;
- }
- public void setIsSelected(Map<Integer, Boolean> isSelected) {
- this.isSelected = isSelected;
- }
- public FileAdapter(Context context,List<String> it,List<String> pa){
- this.context = context;
- mInflater = LayoutInflater.from(context);
- if( it != null && it.size() > 0){
- isSelected = new HashMap<Integer, Boolean>();
- isVisibility = new HashMap<Integer, Integer>();
- for (int i = 0; i < it.size(); i++) {
- isSelected.put(i, false);
- isVisibility.put(i, View.GONE);
- }
- }
- items = it;
- paths = pa;
- mIcon1 = BitmapFactory.decodeResource(context.getResources(),
- R.drawable.backroot);
- mIcon2 = BitmapFactory.decodeResource(context.getResources(),
- R.drawable.backparent);
- mIcon3 = BitmapFactory.decodeResource(context.getResources(),
- R.drawable.folder);
- mIcon4 = BitmapFactory.decodeResource(context.getResources(),
- R.drawable.doc);
- }
- @Override
- public int getCount(){
- return items.size();
- }
- @Override
- public Object getItem(int position){
- return items.get(position);
- }
- @Override
- public long getItemId(int position){
- return position;
- }
- @Override
- public View getView(int position,View convertView,ViewGroup parent){
- ViewHolder holder;
- if(convertView == null){
- convertView = mInflater.inflate(R.layout.file_row, null);
- holder = new ViewHolder();
- VHs.add(holder);
- holder.text = (TextView) convertView.findViewById(R.id.filetext);
- holder.text.setTextColor(context.getResources().getColor(R.color.text_color));
- holder.icon = (ImageView) convertView.findViewById(R.id.fileicon);
- holder.cBox = (CheckBox) convertView.findViewById(R.id.file_check);
- convertView.setTag(holder);
- }else{
- holder = (ViewHolder) convertView.getTag();
- }
- File f=new File(paths.get(position).toString());
- if(items.get(position).toString().equals("goroot")){
- holder.text.setText("返回根目录");
- holder.icon.setImageBitmap(mIcon1);
- holder.cBox.setChecked(isSelected.get(position));
- holder.cBox.setVisibility(isVisibility.get(position));
- }else if(items.get(position).toString().equals("goparent")){
- holder.text.setText("返回上一级");
- holder.icon.setImageBitmap(mIcon2);
- holder.cBox.setChecked(isSelected.get(position));
- holder.cBox.setVisibility(isVisibility.get(position));
- }else{
- holder.text.setText(f.getName());
- if(f.isDirectory()){
- holder.icon.setImageBitmap(mIcon3);
- holder.cBox.setChecked(isSelected.get(position));
- holder.cBox.setVisibility(isVisibility.get(position));
- }else{
- holder.icon.setImageBitmap(mIcon4);
- holder.cBox.setChecked(isSelected.get(position));
- holder.cBox.setVisibility(isVisibility.get(position));
- }
- }
- return convertView;
- }
- public List<ViewHolder> getVHs() {
- return VHs;
- }
- public final class ViewHolder
- {
- public TextView text;
- public ImageView icon;
- public CheckBox cBox;
- }
- }
以上就是所有代码,文件夹以及文件的图片可以自己找,因为我不能上传图片所以,这2个文件以及效果图我会后续补上。(几天之后)
亲们可以根据自己的需要扩展 文件选择(多选或者单选都可以)如果需要我帮忙别客气。
原文地址:点击打开链接