android默认打开方式修改_setcontenttype方法

android默认打开方式修改_setcontenttype方法android打开文件,intent的使用

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

今天做项目遇到了下载更新APP后自动安装的功能,也就是说当下载之后打开该Apk文件。我们可以通过intent的setDataAndType方法实现,这里列举出更多的打开方式:

我的具体代码实现片段是:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
intent.setDataAndType(Uri.fromFile(new File(filePath)),"application/vnd.android.package-archive");
startActivity(intent);

filePath是该文件的绝对路径。

uri要换为你要打开的文件绝对路径,类型是”android.net.Uri“ 参考博主:Android 文件打开方式_关中一叶的专栏-CSDN博客_android 打开文件

1.打开所有文件类型

Intent intent = new Intent();    
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(uri,"*/*");

2.打开apk文件

Intent intent = new Intent();    
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    
intent.setAction(android.content.Intent.ACTION_VIEW);    
intent.setDataAndType(uri,"application/vnd.android.package-archive"); 

3.打开Video文件

Intent intent = new Intent("android.intent.action.VIEW");  
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
intent.setDataAndType(uri, "video/*");

4.打开audio文件

Intent intent = new Intent("android.intent.action.VIEW");  
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
intent.setDataAndType(uri, "audio/*"); 

5.打开HTML文件

Uri uri2 = Uri.parse(uri).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(param ).build();  
Intent intent = new Intent("android.intent.action.VIEW");  
intent.setDataAndType(uri2, "text/html");

6.打开Image文件

Intent intent = new Intent("android.intent.action.VIEW");  
intent.addCategory("android.intent.category.DEFAULT");  
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
intent.setDataAndType(uri, "image/*");

7.打开PPT文件

Intent intent = new Intent("android.intent.action.VIEW");     
intent.addCategory("android.intent.category.DEFAULT");     
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    
intent.setDataAndType(uri, "application/vnd.ms-powerpoint");

8.打开Excel文件

Intent intent = new Intent("android.intent.action.VIEW");     
intent.addCategory("android.intent.category.DEFAULT");     
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);     
intent.setDataAndType(uri, "application/vnd.ms-excel");

9.打开word文件

Intent intent = new Intent("android.intent.action.VIEW");     
intent.addCategory("android.intent.category.DEFAULT");     
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);intent.setDataAndType(uri, "application/msword");

10.打开CHM文件

Intent intent = new Intent("android.intent.action.VIEW");     
intent.addCategory("android.intent.category.DEFAULT");     
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);intent.setDataAndType(uri, "application/x-chm");

11.打开文本文件

Intent intent = new Intent("android.intent.action.VIEW");     
intent.addCategory("android.intent.category.DEFAULT");     
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri1, "text/plain");

12.打开PDF文件

Intent intent = new Intent("android.intent.action.VIEW");     
intent.addCategory("android.intent.category.DEFAULT");     
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri, "application/pdf");

有关Intent的知识请移步博客:基础总结篇之九:Intent应用详解_LiuHe-CSDN博客

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/182067.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • maven使用入门[通俗易懂]

    maven使用入门[通俗易懂]maven面临的问题一个项目就是一个工程。如果项目非常大,最好是每一个模块对应一个工程。借助maven可以将一个项目拆分成多个工程项目中需要的jar必须要手动”复制”,”粘贴”到WEB-INF/lib目录下,带来的问题是:同样的jar包文件重复出现在不同的项目工程中,浪费空间。maven可以将jar仅仅保存在”仓库”中,有需要使用的工程”引用”这个文件接口,并不需要真的把jar包复制过来jar包需要别人替我们准备好,或到官网下载。不同技术的官网提供jar包下载的形式是五花八门的,有些技术的官网就是

    2022年8月8日
  • 滴滴全民拼车日背后的运维技术揭秘

    滴滴全民拼车日背后的运维技术揭秘桔妹导读:抗击疫情,桔妹提醒大家出门带好口罩,勤洗手,多通风。武汉加油!中国加油!在大家开工之际,桔妹邀您阅读滴滴全民拼车日背后的运维技术揭秘。滴滴在2019年12月举办了空前盛大的全民…

    2022年5月5日
  • Android+NDK+OpenGLES开发环境配置

    Android+NDK+OpenGLES开发环境配置

    2022年1月6日
  • 彻底解决mysql报错:1030, ‘Got error 28 from storage engine‘

    彻底解决mysql报错:1030, ‘Got error 28 from storage engine‘恕我直言,网上文章千篇一律,没一个能解决的,全是说清一下内存就好了,但是并没有教不会的小白清理…==这个问题确实是服务器系统盘满了,mysql指定的临时文件目录满掉,大概就是这个意思.下面解决/dev/vda1系统盘满了,其实我压根不知道/dev/vda1这在哪,是什么,后来了解这是virtio-block类型的设备。科普一下:以’c’开头的一行表示该设备是一个……

    2022年10月21日
  • 《JavaScript 模式》读书笔记(8)— DOM和浏览器模式1

    在本书的前面章节中,我们主要集中关注于核心JavaScript(ECMAScript),而并没有太多关注在浏览器中使用JavaScript的模式。本章将探索一些浏览器特定的模式,因为浏览器是使用Jav

    2022年3月25日
  • java输入的方法_java输入一个数

    java输入的方法_java输入一个数java输入语句的方法:1、输入单个字符【charc=(char)System.in.read()】;2、输入整数或者字符串【inta=cin.nextInt()】;3、可以用BufferedReader类输入。java输入语句的方法:如果你要进行输入,请一定加上两个包importjava.util.*;importjava.io.*;请看下面例子用于输入单个字符importjava.i…

    2022年9月21日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号