根据文件的类型进行选择android应用程序打开该文件,不多说,贴代码:
DocmentViewActivity.java
import java.io.File;
import java.util.List;
import com.landtool.hdjsjc.R;
import com.landtool.hdjsjc.common.constants.Constant;
import com.landtool.hdjsjc.document.SystemHelpGridAdapter.HelpDocViewHolder;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;
public class DocmentViewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_system_helper);
intitUI();
}
private void intitUI(){
Button btnBack = (Button) findViewById(R.id.btn_back_help);
String docPath = Constant.DOCUMENTS_PATH;
GridView docGridView = (GridView) findViewById(R.id.help_doc_gridview);
List<File> files = HelperFileUtil.getHelpFiles(docPath);
SystemHelpGridAdapter gridAdapter = new SystemHelpGridAdapter(this, files);
docGridView.setAdapter(gridAdapter);
docGridView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HelpDocViewHolder mHelpDocViewHolder = (HelpDocViewHolder) view.getTag();
Intent intent = HelperFileUtil.openFile(mHelpDocViewHolder.file); //打开文件
startActivity(intent);
}
});
btnBack.setOnClickListener(new OnClickListener(){ //返回
@Override
public void onClick(View v) {
finish();
}
});
}
}
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import android.content.Intent;
import android.net.Uri;
public class HelperFileUtil {
/**
* MIME类型与文件后缀名的匹配表
*/
public static final String[][] MIME_MapTable={
//{后缀名, MIME类型}
{".3gp", "video/3gpp"},
{".apk", "application/vnd.android.package-archive"},
{".asf", "video/x-ms-asf"},
{".avi", "video/x-msvideo"},
{".bin", "application/octet-stream"},
{".bmp", "image/bmp"},
{".c", "text/plain"},
{".class", "application/octet-stream"},
{".conf", "text/plain"},
{".cpp", "text/plain"},
{".doc", "application/msword"},
{".exe", "application/octet-stream"},
{".gif", "image/gif"},
{".gtar", "application/x-gtar"},
{".gz", "application/x-gzip"},
{".h", "text/plain"},
{".htm", "text/html"},
{".html", "text/html"},
{".jar", "application/java-archive"},
{".java", "text/plain"},
{".jpeg", "image/jpeg"},
{".jpg", "image/jpeg"},
{".js", "application/x-javascript"},
{".log", "text/plain"},
{".m3u", "audio/x-mpegurl"},
{".m4a", "audio/mp4a-latm"},
{".m4b", "audio/mp4a-latm"},
{".m4p", "audio/mp4a-latm"},
{".m4u", "video/vnd.mpegurl"},
{".m4v", "video/x-m4v"},
{".mov", "video/quicktime"},
{".mp2", "audio/x-mpeg"},
{".mp3", "audio/x-mpeg"},
{".mp4", "video/mp4"},
{".mpc", "application/vnd.mpohun.certificate"},
{".mpe", "video/mpeg"},
{".mpeg", "video/mpeg"},
{".mpg", "video/mpeg"},
{".mpg4", "video/mp4"},
{".mpga", "audio/mpeg"},
{".msg", "application/vnd.ms-outlook"},
{".ogg", "audio/ogg"},
{".pdf", "application/pdf"},
{".png", "image/png"},
{".pps", "application/vnd.ms-powerpoint"},
{".ppt", "application/vnd.ms-powerpoint"},
{".prop", "text/plain"},
{".rar", "application/x-rar-compressed"},
{".rc", "text/plain"},
{".rmvb", "audio/x-pn-realaudio"},
{".rtf", "application/rtf"},
{".sh", "text/plain"},
{".tar", "application/x-tar"},
{".tgz", "application/x-compressed"},
{".txt", "text/plain"},
{".wav", "audio/x-wav"},
{".wma", "audio/x-ms-wma"},
{".wmv", "audio/x-ms-wmv"},
{".wps", "application/vnd.ms-works"},
//{".xml", "text/xml"},
{".xml", "text/plain"},
{".z", "application/x-compress"},
{".zip", "application/zip"},
{"", "*/*"}
};
/**
* 根据文件后缀名获得对应的MIME类型。
* @param file
*/
public static String getMIMEType(File file)
{
String type="*/*";
String fName=file.getName();
int dotIndex = fName.lastIndexOf(".");
if(dotIndex < 0){
return type;
}
//文件的后缀名
String end=fName.substring(dotIndex,fName.length()).toLowerCase(Locale.getDefault());
if(null == end || "".equals(end))
return type;
//在MIME和文件类型的匹配表中找到对应的MIME类型。
for(int i=0;i<MIME_MapTable.length;i++){
if(end.equals(MIME_MapTable[i][0]))
type = MIME_MapTable[i][1];
}
return type;
}
/**
* 打开文件
* @param file
*/
public static Intent openFile(File file){
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
String type = getMIMEType(file);
intent.setDataAndType(Uri.fromFile(file), type);
return intent;
}
/**
* 列出指定目录下的所有文件,不包括子目录
* @param parentPath
* 指定根目录
* @return
* 文件列表
*/
public static List<File> getHelpFiles(String parentPath){
File file = new File(parentPath);
if(!file.exists()){
file.mkdir();
}
if (!file.isDirectory()) {
return null;
}
File[] files = file.listFiles();
if(null == files || files.length < 1){
return null;
}
ArrayList<File> fileList = new ArrayList<File>();
for (File f : files) {
if(f.isDirectory()){
continue;
}
fileList.add(f);
}
return fileList.size() > 0 ? fileList : null;
}
}
SystemHelpGridAdapter.java
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.landtool.hdjsjc.R;
import android.content.Context;
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 SystemHelpGridAdapter extends BaseAdapter {
private List<File> files;
private Context mContext;
private LayoutInflater mLayoutInflater;
public SystemHelpGridAdapter(Context ctx, List<File> fls) {
this.mContext = ctx;
this.files = fls;
mLayoutInflater = LayoutInflater.from(mContext);
if(null == files){
files = new ArrayList<File>();
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return files.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return files.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
HelpDocViewHolder holder = null;
if (convertView == null) {
holder = new HelpDocViewHolder();
convertView = mLayoutInflater.inflate(R.layout.system_helper_gridview_item, null);
holder.name = (TextView) convertView.findViewById(R.id.help_file_name);
holder.fileIcon = (ImageView) convertView.findViewById(R.id.help_file_icon);
convertView.setTag(holder);
} else {
holder = (HelpDocViewHolder) convertView.getTag();
}
holder.name.setText(files.get(position).getName());
holder.fileIcon.setBackgroundResource(R.drawable.unknown_file_icon);
holder.file = files.get(position);
return convertView;
}
public final class HelpDocViewHolder {
public TextView name;
public ImageView fileIcon;
public File file;
}
}
activity_system_helper.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" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f0f5f8"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="@dimen/common_title_bar_height"
android:background="@drawable/pub_title_bg" >
<Button
android:id="@+id/btn_back_help"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/common_back_button"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
/>
<TextView
android:id="@+id/title_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="文档资料"
android:textColor="@android:color/white"
android:textSize="@dimen/common_title_font_size" />
</RelativeLayout>
<GridView
android:id="@+id/help_doc_gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:columnWidth="@dimen/image_thumbnail_size"
android:horizontalSpacing="@dimen/image_thumbnail_spacing"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="@dimen/image_thumbnail_spacing" >
</GridView>
</LinearLayout>
</LinearLayout>