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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • RT-Thread中自定义 FinSH 命令「建议收藏」

    RT-Thread中自定义 FinSH 命令「建议收藏」在使用RT-Thread中的FinSH命令时,除了系统默认的FinSH命令以外,我们还可以自定义FinSH命令。下面就来演示一下如何自定义FinSH命令。关于FinSH命令的详细用法请参考官方资料https://www.rt-thread.org/document/site/programming-manual/finsh/finsh/。要使用FinSH命令首先要添加FinSH组件,添加组件的详细过程见手把手教你移植RT-Thread系统,FinSH组件添加成后,打开建立…

    2022年5月22日
    52
  • python保留小数位数_结果保留两位小数

    python保留小数位数_结果保留两位小数介绍3中方法:1、round(a,2)2、print(‘%.2f’%(a/b))3、format(a,’.2f’)

    2022年8月11日
    4
  • Docker(三)- 从镜像运行启动容器「建议收藏」

    Docker(三)- 从镜像运行启动容器「建议收藏」文章目录从镜像运行启动容器容器启动后运行的命令`ENTRYPOINT`和`CMD`启动容器时覆盖`ENTRYPOINT`和`CMD“-d`后台运行`dockerexec`进入容器,运行指定命令`–name`和`–restart=always“–rm`和`dockercp`从镜像运行启动容器从一个镜像可以运行启动一个或多个容器。所谓容器,我们可以理解为是一个虚拟的计算机,其中运行着操作系统,操作系统中运行着我们部署的应用。从tomcat镜像启动容器:do

    2022年9月2日
    2
  • c++反三角函数的使用(反三角函数求角度)

    最近写的东西用到了数学库中的acos函数,但是代码在运行的时候有时候会出莫名其妙的错误,比如返回值是个特别大的数。最后在debug的时候发现acos返回的数据很奇怪,但是传入的参数明明没有问题,可以保证是(-1,1)。回想起,double类型的末尾数据是不确定的,比如当double类型数据alpha=1.0时其真实值可能是1.00001;这明明是很早就知道的,但是在写代码的时候…

    2022年4月15日
    164
  • docker(4)解决pull镜像速度缓慢「建议收藏」

    docker(4)解决pull镜像速度缓慢「建议收藏」前言上一篇讲到pull镜像,但是pull镜像的时候下拉的速度实在感人,有什么解决办法吗?我们只需将docker镜像源修改为国内的将docker镜像源修改为国内的:在/etc/docker/d

    2022年7月28日
    42
  • html遮罩层动画制作,flash简单制作遮罩动画效果[通俗易懂]

    html遮罩层动画制作,flash简单制作遮罩动画效果[通俗易懂]flash简单制作遮罩动画效果QQ空间的开机动画大家应该都有,从最初的出现的一点到后面全部出现,如此神奇的效果到底是怎么做的呢,一起来看看吧!遮罩特效:由于百度只能上传500k以内的照片,所以效果图片质量不是很好,当然,我们一般做的特效是.swf,这里是为了方便大家观看,所以做成了gif.步骤:1、打开flash面板,创建新项目。2、点击文件——导入——导入一张图片。3、窗口——库,这里我们可以…

    2022年5月1日
    91

发表回复

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

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