MainActivity.java

package com.example.aimee.aimeetest3;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu; import java.io.File;
import java.util.Locale; public class MainActivity extends AppCompatActivity {
private static final String[][] MIME_MapTable={
//{后缀名,MIME类型}
{"txt","text/plain"},
{"xlsx","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} private void openFile(File file){
Intent intent=new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.setAction(Intent.ACTION_VIEW);
String type=getMIMEType(file);
intent.setDataAndType(Uri.fromFile(file),type);
startActivity(intent);
}
private String getMIMEType(File file){
String type="*/*";
String fName=file.getName();
int dotIndex=fName.lastIndexOf(".");
if(dotIndex<0){
return type;
}
String end=file.getName().substring(dotIndex+1,fName.length()).toLowerCase(Locale.getDefault());
if(end.equals(""))
return type;
for(int i=0;i<MIME_MapTable.length;i++){
if(end.equals(MIME_MapTable[i][0]))
type=MIME_MapTable[i][1];
}
return type;
}
}

  

AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aimee.aimeetest3"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file" />
<data android:scheme="content"/>
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
</application> </manifest>

  

第十一篇- 实现APK打开文件功能的更多相关文章

  1. iOS 用其他应用程序打开文件功能

    先摘抄一段我抄别人用的. <key>CFBundleDocumentTypes</key>    <array>        <dict>       ...

  2. android无后缀二进制执行文件替代apk实现程序功能

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha android无后缀二进制执行文件替代apk实现程序功能 实现将data/Android ...

  3. UWP入门(十一)--使用选取器打开文件和文件夹

    原文:UWP入门(十一)--使用选取器打开文件和文件夹 很漂亮的功能,很有趣 重要的 API FileOpenPicker FolderPicker StorageFile 通过让用户与选取器交互来访 ...

  4. Python基础【第十一篇】文件操作(file()、open()方法和fileinput模块)

    一.file/open 内置函数 file函数的方法: 注:file 和 open的用法和功能相同这里只对file进行分析 file(‘filename’,’mode’) file(‘filename ...

  5. APK扩展文件介绍、功能及用法

    APK扩展文件介绍 Android Market (Google Play Store)中每一个APK文件的最大限制是50MB.假设您的程序中包括大量的数据文件,曾经您仅仅能把这些数据文件放到自己的s ...

  6. 任意文件夹下打开cmd功能的设置(win10)

    win10中打开cmd的方法: 1."运行"中输入CMD打开,也可以按住win+R 2.选择命令行工具中"开始-->>所有应用-->>Window ...

  7. 关闭vscode打开新文件自动关闭预览文件功能

    经常碰到这个问题,我打开文件就是有用的,每次给我自动关闭了我还得去打开. 当然这个问题可以双击文件,接触那个文件的预览状态就可以解决了.不过还有一个更懒的方法,直接修改vscode配置就好了. // ...

  8. python学习之【第十一篇】:Python中的文件操作

    1.前言 在Python中,对文件的操作主要遵循以下流程: 打开文件,得到文件句柄并赋值给一个变量 通过文件句柄对文件进行操作 关闭文件 2.打开文件 使用open函数,可以打开一个已经存在的文件,或 ...

  9. 第十一篇 SQL Server代理维护计划

    本篇文章是SQL Server代理系列的第十一篇,详细内容请参考原文 在这一系列的上一篇,我们看了使用代理帐户模仿Windows安全上下文完成作业步骤的工作.大多数子系统支持代理账户,同时子系统限制代 ...

随机推荐

  1. python设计模式第七天【建造者模式】

    1. 建造者模式UML图 2.应用场景 (1)专门创建具有符合属性的对象 3.代码实现 #!/usr/bin/env python #! _*_ coding: UTF-8 _*_ from abc ...

  2. QTP键盘操作笔记

    micCtrlDwn  Presses the Ctrl key. micCtrlUp  Releases the Ctrl key. micLCtrlDwn  Presses the left Ct ...

  3. 二、两条Linux删除数据跑路命令

    一.rm rm -rf / 无提示循环删除根目录,,删除存在被恢复的可能 二.dd dd if=/dev/urandom of=/dev/hda1 随机填写数据到相应分区,直到填满为止.重写后的分区无 ...

  4. word2vec训练&IC分词(待)

    参考http://www.52nlp.cn/%E4%B8%AD%E8%8B%B1%E6%96%87%E7%BB%B4%E5%9F%BA%E7%99%BE%E7%A7%91%E8%AF%AD%E6%96 ...

  5. How to vi

    h:left,j:down,k:up,l:right.wq #write and quitx #cut one letterdd#cut one line/ #searchs/a/b/ #replac ...

  6. windows下ping端口

    上图的操作完成以后  进入dos控制台 输入telnet  ip地址  端口号   回车 标识已ping通 ping不通是这种提示

  7. solrcloud编辑zookeeper上的配置文件的方法

    solrcloud的配置文件是上传在zookeeper文件系统上的.这样就面临一个问题,每次需要修改配置文件,就没有只有之前本地单机solr那么简单,需要上传. 搭建solrcloud时候提交配置文件 ...

  8. SharePoint 2016 installation error The program can not start because api-ms-win-crt-runtime-l1-1-0.dll is missing

    In this post we will discuss how we can resolve the issue The program can not start because api-ms-w ...

  9. vuex2.0 基本使用(4) --- modules

    vue 使用的是单一状态树对整个应用的状态进行管理,也就是说,应用中的所有状态都放到store中,如果是一个大型应用,状态非常多, store 就会非常庞大,不太好管理.这时vuex 提供了另外一种方 ...

  10. Nginx IP地址透传

    L:119