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. Client将数据读写HDFS流程

    HDFS介绍 HDFS(Hadoop Distributed File System )Hadoop分布式文件系统.是根据google发表的论文翻版的. 什么是分布式文件系统 分布式文件系统(Dist ...

  2. jquery获取select选择的文本与值

    jquery获取select选择的文本与值获取select :获取select 选中的 text :    $("#ddlregtype").find("option:s ...

  3. Linux常见操作

    前面的话 本文将详细介绍Linux常见操作 基本概念 Linux严格区分大小写,所有内容以文件形式保存,包括硬件 Linux没有扩展名的概念,不靠扩展名来区分文件类型.但有一些约定俗成的扩展名 压缩包 ...

  4. cuda编程-并行规约

    利用shared memory计算,并避免bank conflict:通过每个block内部规约,然后再把所有block的计算结果在CPU端累加 代码: #include <cuda_runti ...

  5. mysql 集群方案

    试试基于Galera的MySQL高可用集群  mha  mgr

  6. Codeforces Round #415 Div. 1

    A:考虑每对最大值最小值的贡献即可. #include<iostream> #include<cstdio> #include<cmath> #include< ...

  7. Modelsim脚本仿真文件分析

    Do文件的实质是tcl脚本,本质上是ascii文件.所以扩展名可以任意的,不一定叫xx.do,也可以叫xx.tcl,甚至叫成你的名字也没有关系.看自己喜好,一般取do,不过我喜欢取成tcl,用代码编辑 ...

  8. 【CodeForces706E】Working routine(二维链表)

    BUPT2017 wintertraining(15) #6B 题意 q次操作,每次把两个给定子矩阵交换,求最后的矩阵.(2 ≤ n, m ≤ 1000, 1 ≤ q ≤ 10 000) 题解 用R[ ...

  9. SES 之全局搜索小记

    @2018-12-17 [小记] SES 使用全局搜索时,变量所在的头文件没有被检索出来,选中检索属性中的 Dependencies 就可以了

  10. 一种导致 emwin 中 EDIT 控件不显示的情况

    @2018-12-11 [小记] 设计界面中使用了 EDIT 控件,但在其初始化语句中误使用了 text-color 属性API,导致了控件 EDIT 中的 Text 无法显示,具体如下 hItem ...