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

相关文章

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

Nexus安装和启动的实现教程

《Nexus安装和启动的实现教程》:本文主要介绍Nexus安装和启动的实现教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Nexus下载二、Nexus安装和启动三、关闭Nexus总结一、Nexus下载官方下载链接:DownloadWindows系统根

Java中实现线程的创建和启动的方法

《Java中实现线程的创建和启动的方法》在Java中,实现线程的创建和启动是两个不同但紧密相关的概念,理解为什么要启动线程(调用start()方法)而非直接调用run()方法,是掌握多线程编程的关键,... 目录1. 线程的生命周期2. start() vs run() 的本质区别3. 为什么必须通过 st

Oracle修改端口号之后无法启动的解决方案

《Oracle修改端口号之后无法启动的解决方案》Oracle数据库更改端口后出现监听器无法启动的问题确实较为常见,但并非必然发生,这一问题通常源于​​配置错误或环境冲突​​,而非端口修改本身,以下是系... 目录一、问题根源分析​​​二、保姆级解决方案​​​​步骤1:修正监听器配置文件 (listener.

MySQL版本问题导致项目无法启动问题的解决方案

《MySQL版本问题导致项目无法启动问题的解决方案》本文记录了一次因MySQL版本不一致导致项目启动失败的经历,详细解析了连接错误的原因,并提供了两种解决方案:调整连接字符串禁用SSL或统一MySQL... 目录本地项目启动报错报错原因:解决方案第一个:第二种:容器启动mysql的坑两种修改时区的方法:本地

MySQL启动报错:InnoDB表空间丢失问题及解决方法

《MySQL启动报错:InnoDB表空间丢失问题及解决方法》在启动MySQL时,遇到了InnoDB:Tablespace5975wasnotfound,该错误表明MySQL在启动过程中无法找到指定的s... 目录mysql 启动报错:InnoDB 表空间丢失问题及解决方法错误分析解决方案1. 启用 inno

无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案

《无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案》:本文主要介绍了无法启动此程序,详细内容请阅读本文,希望能对你有所帮助... 在计算机使用过程中,我们经常会遇到一些错误提示,其中之一就是"api-ms-win-core-path-l1-1-0.dll丢失

解决tomcat启动时报Junit相关错误java.lang.ClassNotFoundException: org.junit.Test问题

《解决tomcat启动时报Junit相关错误java.lang.ClassNotFoundException:org.junit.Test问题》:本文主要介绍解决tomcat启动时报Junit相... 目录tomcat启动时报Junit相关错误Java.lang.ClassNotFoundException

Redis在windows环境下如何启动

《Redis在windows环境下如何启动》:本文主要介绍Redis在windows环境下如何启动的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Redis在Windows环境下启动1.在redis的安装目录下2.输入·redis-server.exe