22 Intent启动各种资源(源自 黑马程序员)

2024-05-05 07:48

本文主要是介绍22 Intent启动各种资源(源自 黑马程序员),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


 

http://hi.baidu.com/wishwingliao/blog/item/e2172297a880a87855fb96cb.html

 

import android.app.Activity;

import android.content.Intent;

import android.net.Uri;

import android.net.Uri.Builder;

import java.io.File;

import android.content.Intent;

 

//自定义android Intent类,

//可用于获取打开以下文件的intent

//PDF,PPT,WORD,EXCEL,CHM,HTML,TEXT,AUDIO,VIDEO

 

示例:

//这个不行,可能是因为PDF.apk程序没有权限访问其它APK里的asset资源文件,又或者是路径写错?

//Intent it = getPdfFileIntent("file:///android_asset/helphelp.pdf");

//下面这些都OK

//Intent it = getHtmlFileIntent("/mnt/sdcard/tutorial.html");//SD卡主目录

//Intent it = getHtmlFileIntent("/sdcard/tutorial.html");//SD卡主目录,这样也可以

Intent it = getHtmlFileIntent("/system/etc/tutorial.html");//系统内部的etc目录

//Intent it = getPdfFileIntent("/system/etc/helphelp.pdf");

//Intent it = getWordFileIntent("/system/etc/help.doc");

//Intent it = getExcelFileIntent("/mnt/sdcard/Book1.xls")

//Intent it = getPptFileIntent("/mnt/sdcard/download/Android_PPT.ppt");//SD卡的download目录下

//Intent it = getVideoFileIntent("/mnt/sdcard/ice.avi");

//Intent it = getAudioFileIntent("/mnt/sdcard/ren.mp3");

//Intent it = getImageFileIntent("/mnt/sdcard/images/001041580.jpg");

//Intent it = getTextFileIntent("/mnt/sdcard/hello.txt",false);

 

startActivity( it );

 

public class MyIntent

{

 

//android获取一个用于打开HTML文件的intent

  public static Intent getHtmlFileIntent( String param )

  {

    Uri uri = Uri.parse(param ).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(param ).build();

    Intent intent = new Intent("android.intent.action.VIEW");

    intent.setDataAndType(uri, "text/html");

    return intent;

  }

 

//android获取一个用于打开图片文件的intent

  public static Intent getImageFileIntent( String param )

  {

    Intent intent = new Intent("android.intent.action.VIEW");

    intent.addCategory("android.intent.category.DEFAULT");

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    Uri uri = Uri.fromFile(new File(param ));

    intent.setDataAndType(uri, "image/*");

    return intent;

  }

 

  //android获取一个用于打开PDF文件的intent

  public static Intent getPdfFileIntent( String param )

  {

    Intent intent = new Intent("android.intent.action.VIEW");

    intent.addCategory("android.intent.category.DEFAULT");

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    Uri uri = Uri.fromFile(new File(param ));

    intent.setDataAndType(uri, "application/pdf");

    return intent;

  }

 

//android获取一个用于打开文本文件的intent

 public static Intent getTextFileIntent( String param, boolean paramBoolean)

{

 Intent intent = new Intent("android.intent.action.VIEW");

 intent.addCategory("android.intent.category.DEFAULT");

 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

 if (paramBoolean)

 {

Uri uri1 = Uri.parse(param );

intent.setDataAndType(uri1, "text/plain");

 }

 else

 {

Uri uri2 = Uri.fromFile(new File(param ));

intent.setDataAndType(uri2, "text/plain");

 }

 

 return intent;

}

 

//android获取一个用于打开音频文件的intent

  public static Intent getAudioFileIntent( String param )

  {

    Intent intent = new Intent("android.intent.action.VIEW");

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    intent.putExtra("oneshot", 0);

    intent.putExtra("configchange", 0);

    Uri uri = Uri.fromFile(new File(param ));

    intent.setDataAndType(uri, "audio/*");

    return intent;

  }

 

  //android获取一个用于打开视频文件的intent

  public static Intent getVideoFileIntent( String param )

  {

    Intent intent = new Intent("android.intent.action.VIEW");

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    intent.putExtra("oneshot", 0);

    intent.putExtra("configchange", 0);

    Uri uri = Uri.fromFile(new File(param ));

    intent.setDataAndType(uri, "video/*");

    return intent;

  }

 

  //android获取一个用于打开CHM文件的intent

  public static Intent getChmFileIntent( String param )

  {

    Intent intent = new Intent("android.intent.action.VIEW");

    intent.addCategory("android.intent.category.DEFAULT");

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    Uri uri = Uri.fromFile(new File(param ));

    intent.setDataAndType(uri, "application/x-chm");

    return intent;

  }

 

//android获取一个用于打开Word文件的intent

  public static Intent getWordFileIntent( String param )

  {

    Intent intent = new Intent("android.intent.action.VIEW");

    intent.addCategory("android.intent.category.DEFAULT");

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    Uri uri = Uri.fromFile(new File(param ));

    intent.setDataAndType(uri, "application/msword");

    return intent;

  }

 

//android获取一个用于打开Excel文件的intent

  public static Intent getExcelFileIntent( String param )

  {

    Intent intent = new Intent("android.intent.action.VIEW");

    intent.addCategory("android.intent.category.DEFAULT");

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    Uri uri = Uri.fromFile(new File(param ));

    intent.setDataAndType(uri, "application/vnd.ms-excel");

    return intent;

  }

  

//android获取一个用于打开PPT文件的intent

  public static Intent getPptFileIntent( String param )

  {

    Intent intent = new Intent("android.intent.action.VIEW");

    intent.addCategory("android.intent.category.DEFAULT");

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    Uri uri = Uri.fromFile(new File(param ));

    intent.setDataAndType(uri, "application/vnd.ms-powerpoint");

    return intent;

  }

}

 

注意:

一定要在Manifest.xml中声明权限:

<!-- 在SDCard中创建与删除文件权限 -->

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

<!-- 往SDCard写入数据权限 -->

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

这篇关于22 Intent启动各种资源(源自 黑马程序员)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/961168

相关文章

一份LLM资源清单围观技术大佬的日常;手把手教你在美国搭建「百万卡」AI数据中心;为啥大模型做不好简单的数学计算? | ShowMeAI日报

👀日报&周刊合集 | 🎡ShowMeAI官网 | 🧡 点赞关注评论拜托啦! 1. 为啥大模型做不好简单的数学计算?从大模型高考数学成绩不及格说起 司南评测体系 OpenCompass 选取 7 个大模型 (6 个开源模型+ GPT-4o),组织参与了 2024 年高考「新课标I卷」的语文、数学、英语考试,然后由经验丰富的判卷老师评判得分。 结果如上图所

22.手绘Spring DI运行时序图

1.依赖注入发生的时间 当Spring loC容器完成了 Bean定义资源的定位、载入和解析注册以后,loC容器中已经管理类Bean 定义的相关数据,但是此时loC容器还没有对所管理的Bean进行依赖注入,依赖注入在以下两种情况 发生: 、用户第一次调用getBean()方法时,loC容器触发依赖注入。 、当用户在配置文件中将<bean>元素配置了 lazy-init二false属性,即让

加载资源文件失败

背景         自己以前装了一个海康的深度学习算法平台,试用期是一个月,过了一个月之后,因为没有有效注册码或者加密狗的支持了导致无法使用,于是打算卸载掉,在卸载一个软件的时候,无论是使用控制面板还是软件自带的卸载功能,总是卸载不掉,提示“加载资源文件失败”。该软体主要包括以下两部分: 用自带卸载功能卸载的时候分别提示如下:     用控制面板卸载的时候反应很慢,最后也是提示这个

【新闻】AI程序员要来了吗?阿里云官宣

内容提要 6 月 21 日,在阿里云上海 AI 峰会上,阿里云宣布推出首个AI 程序员。 据介绍,这个AI程序员具备架构师、开发工程师、测试工程师等多种岗位的技能,能一站式自主完成任务分解、代码编写、测试、问题修复、代码提交整个过程,最快分钟级即可完成应用开发,大幅提升研发效率。 近段时间以来,有关AI的实践应用突破不断,全球开发者加速研发步伐。有业内人士坦言,随着大模型性能逐渐提升,AI应

Docker启动异常

报错信息: failed to start daemon: Error initializing network controller: error creating default "bridge" network: cannot create network b8fd8c684f0ba865d4a13d36e5282fd694bbd37b243c7ec6c9cd29416db98d4b (d

小车启动底盘功能包

传感器与小车底盘的集成 新建功能包 catkin_create_pkg mycar_start roscpp rospy std_msgs ros_arduino_python usb_cam ydlidar_ros_driver 功能包下创建launch文件夹,launch文件夹中新建launch文件,文件名start.launch。 内容如下 <!-- 机器人启动文件:1.启动底盘2

Web容器启动时加载Spring分析

在应用程序web.xml中做了以下配置信息时,当启动Web容器时就会自动加载Spring容器。 [java]  view plain copy print ? <listener>          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

Groovy:程序员的 DSL

什么是DSL? 领域特定语言,针对一个特定的领域,具有受限表达性的一种计算机程序语言。可以看做是一种抽象处理的方式。 具有四个元素,第一个是计算机程序设计语言,使用DSL来指挥计算机做事情,语言性(一种特定的语言),受限的表达性,并不像同通用的设计语言那样具有广泛的能力,针对一个明确的领域。 分类有哪些? 外部DSL:不同于应用系统主要使用语言的语言,通常采用自定义语法,宿主应用的代码采用

Linux开机自动启动ORACLE设置

Redhat init简介: Linux启动时,会运行一个init程序,然后由init来启动后面的任务,包括多用户环境(inittab中设定)和网络等。运行级就是当前程序运行的功能级别,这个级别从1到6,具有不同的功能。这些级别在/etc/inittab(其他发行版这个文件位置不同)中指定,该文件就是init程序寻找的主要文件。最先运行的服务放在/etc/rc.d目录下。

Mac 本地启动 Dify

本地启动 dify 拉取 dify 文件 git clone https://github.com/langgenius/dify.git 启动底层服务 cd dify/dockerdocker-compose -f docker-compose.middleware.yaml -p dify up -d 启动后端 API 安装 poetry brew install poet