編輯:Android開發實例
最近想寫一個選擇照片文件夾的功能,就寫了個選擇文件夾功能的demo,主要就是選擇目標目錄,順便看了下文件打開的方式
首先看效果圖
第一個Activity很簡單就是一個按鈕Button 加一個 TextView
見main.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"
- >
- <Button
- android:id="@+id/button"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="選擇文件"
- />
- <TextView
- android:id="@+id/fileText"
- android:gravity="center"
- android:textSize="20px"
- android:textColor="#219ac6"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- />
- </LinearLayout>
Button彈出顯示文件夾選擇框
TextView就是用來顯示當前選擇的文件夾路徑
- public class MainAcivity extends Activity{
- public static final int FILE_RESULT_CODE = 1;
- private TextView textView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button button = (Button)findViewById(R.id.button);
- textView = (TextView)findViewById(R.id.fileText);
- button.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- Intent intent = new Intent(MainAcivity.this,MyFileManager.class);
- startActivityForResult(intent, FILE_RESULT_CODE);
- }
- });
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if(FILE_RESULT_CODE == requestCode){
- Bundle bundle = null;
- if(data!=null&&(bundle=data.getExtras())!=null){
- textView.setText("選擇文件夾為:"+bundle.getString("file"));
- }
- }
- }
- }
這裡通過startActivityForResult()彈出的Activity 所以要重寫onActivityResult方法
在目標Activity調用方法finish()的時候會回調
這裡需要注意在按返回鍵時也會調用該方法 所以這裡還需要判空
然後再 MyFileManager 這個Activity中通過ListView來顯示文件列表
布局文件 fileselect.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="250px"
- android:layout_height="400px"
- android:orientation="vertical"
- android:background="@drawable/white"
- >
- <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"
- >
- </TextView>
- <ListView
- android:id="@android:id/list"
- android:layout_width="fill_parent"
- android:layout_height="330px"
- >
- </ListView>
- <LinearLayout
- android:gravity="center"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:background="@drawable/white"
- >
- <Button
- android:id="@+id/buttonConfirm"
- android:layout_width="125px"
- android:layout_height="fill_parent"
- android:text="確定"
- />
- <Button
- android:id="@+id/buttonCancle"
- android:layout_width="125px"
- android:layout_height="fill_parent"
- android:text="取消"
- />
- </LinearLayout>
- </LinearLayout>
列表顯示Item 通過布局文件 file_row.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#ffffff"
- >
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/listview_selected"
- android:padding="6px"
- >
- <ImageView android:id="@+id/icon"
- android:layout_width="30dip"
- android:layout_height="30dip"
- >
- </ImageView>
- <TextView android:id="@+id/text"
- android:layout_gravity="center_horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textColor="@drawable/black"
- >
- </TextView>
- </LinearLayout>
- </LinearLayout>
這裡簡單實現點擊變換底色的效果 通過兩層LinearLayout 的方式實現 創建一個select可以自定義點擊效果 就不贅述了
文件選擇代碼
- package com.debby;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.ListActivity;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.Window;
- import android.widget.Button;
- import android.widget.ListView;
- import android.widget.TextView;
- public class MyFileManager extends ListActivity {
- private List<String> items = null;
- private List<String> paths = null;
- private String rootPath = "/";
- private String curPath = "/";
- private TextView mPath;
- private final static String TAG = "bb";
- @Override
- protected void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.fileselect);
- mPath = (TextView) findViewById(R.id.mPath);
- Button buttonConfirm = (Button) findViewById(R.id.buttonConfirm);
- buttonConfirm.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- Intent data = new Intent(MyFileManager.this, MainAcivity.class);
- Bundle bundle = new Bundle();
- bundle.putString("file", curPath);
- data.putExtras(bundle);
- setResult(2, data);
- finish();
- }
- });
- Button buttonCancle = (Button) findViewById(R.id.buttonCancle);
- buttonCancle.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- finish();
- }
- });
- getFileDir(rootPath);
- }
- private void getFileDir(String filePath) {
- mPath.setText(filePath);
- items = new ArrayList<String>();
- paths = new ArrayList<String>();
- File f = new File(filePath);
- File[] files = f.listFiles();
- if (!filePath.equals(rootPath)) {
- items.add("b1");
- paths.add(rootPath);
- items.add("b2");
- paths.add(f.getParent());
- }
- for (int i = 0; i < files.length; i++) {
- File file = files[i];
- items.add(file.getName());
- paths.add(file.getPath());
- }
- setListAdapter(new MyAdapter(this, items, paths));
- }
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- File file = new File(paths.get(position));
- if (file.isDirectory()) {
- curPath = paths.get(position);
- getFileDir(paths.get(position));
- } else {
- //可以打開文件
- }
- }
- }
- import java.io.File;
- import java.util.List;
- import com.debby.R;
- 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.ImageView;
- import android.widget.TextView;
- public class MyAdapter 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;
- public MyAdapter(Context context,List<String> it,List<String> pa)
- {
- mInflater = LayoutInflater.from(context);
- items = it;
- paths = pa;
- mIcon1 = BitmapFactory.decodeResource(context.getResources(),R.drawable.back01);
- mIcon2 = BitmapFactory.decodeResource(context.getResources(),R.drawable.back02);
- 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();
- holder.text = (TextView) convertView.findViewById(R.id.text);
- holder.icon = (ImageView) convertView.findViewById(R.id.icon);
- convertView.setTag(holder);
- }
- else
- {
- holder = (ViewHolder) convertView.getTag();
- }
- File f=new File(paths.get(position).toString());
- if(items.get(position).toString().equals("b1"))
- {
- holder.text.setText("返回根目錄..");
- holder.icon.setImageBitmap(mIcon1);
- }
- else if(items.get(position).toString().equals("b2"))
- {
- holder.text.setText("返回上一層..");
- holder.icon.setImageBitmap(mIcon2);
- }
- else
- {
- holder.text.setText(f.getName());
- if(f.isDirectory())
- {
- holder.icon.setImageBitmap(mIcon3);
- }
- else
- {
- holder.icon.setImageBitmap(mIcon4);
- }
- }
- return convertView;
- }
- private class ViewHolder
- {
- TextView text;
- ImageView icon;
- }
- }
在 onListItemClick 事件中改變ListView的Adapter實現打開文件夾
這裡點擊文件夾時可以打開目錄,如果選擇文件可以根據類型來啟用不同工具來打開
方法如下
- private void openFile(File f) {
- Intent intent = new Intent();
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.setAction(android.content.Intent.ACTION_VIEW);
- String type = getMIMEType(f);
- intent.setDataAndType(Uri.fromFile(f), type);
- startActivity(intent);
- }
- private String getMIMEType(File f) {
- String type = "";
- String fName = f.getName();
- String end = fName
- .substring(fName.lastIndexOf(".") + 1, fName.length())
- .toLowerCase();
- if (end.equals("m4a") || end.equals("mp3") || end.equals("mid")
- || end.equals("xmf") || end.equals("ogg") || end.equals("wav")) {
- type = "audio";
- } else if (end.equals("3gp") || end.equals("mp4")) {
- type = "video";
- } else if (end.equals("jpg") || end.equals("gif") || end.equals("png")
- || end.equals("jpeg") || end.equals("bmp")) {
- type = "image";
- } else {
- type = "*";
- }
- type += "/*";
- return type;
- }
根據文件的後綴名來確定type 然後打開文件,打開方式通過setDataAndType方法設置
智能終端設備的多點觸控操作為我們帶來了種種炫酷體驗,這也使得很多Android
Android提供了許多方法來控制播放的音頻/視頻文件和流。其中該方法是通過一類稱為MediaPlayer。Android是提供MediaPlayer類訪問內置的媒體播放
此方法適用於所有母控件無法獲取焦點的情況 開發中很常見的一個問題,項目中的listview不僅僅是簡單的文字,常常需要自己定義listview,自己的Adapte
本文實例講述了Android實現兩個ScrollView互相聯動的同步滾動效果代碼。分享給大家供大家參考,具體如下: 最近在做一個項目,用到了兩個ScrollVi