Android实现打开各种文件的intent方法小结

本文实例讲述了Android实现打开各种文件的intent方法。分享给大家供大家参考,具体如下:

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;
 }
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android控件用法总结》、《Android视图View技巧总结》、《Android文件操作技巧汇总》、《Android操作SQLite数据库技巧总结》、《Android操作json格式数据技巧总结》、《Android数据库操作技巧总结》、《Android编程之activity操作技巧总结》、《Android编程开发之SD卡操作方法汇总》、《Android开发入门与进阶教程》及《Android资源操作技巧汇总》

希望本文所述对大家Android程序设计有所帮助。

(0)

相关推荐

  • Android常用的intent action汇总

    本文总结讲述了Android常用的intent action功能.分享给大家供大家参考,具体如下: Android基本的设计理念是鼓励减少组件间的耦合,因此Android提供了Intent (意图) ,Intent提供了一种通用的消息系统,它允许在你的应用程序与其它的应用程序间传递Intent来执行动作和产生事件.Intent作为联系各Activity之间的纽带,其作用并不仅仅只限于简单的数据传递.通过其自带的属性,其实可以方便的完成很多较为复杂的操作.例如直接调用拨号功能.处理接收短信,诸如此

  • Android基于Intent实现Activity之间数据传递的方法

    本文实例讲述了Android基于Intent实现Activity之间数据传递的方法.分享给大家供大家参考,具体如下: MainActivity: package com.test.intentdemo; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.Menu; import andro

  • Android开发中Intent用法总结

    本文实例讲述了Android开发中Intent用法.分享给大家供大家参考,具体如下: Android手机软件开发中,Intent作为手机软件开发时很重要的对象需要引起我们的重视,实际上,intent也是体现Android开发具有其独特性的一个标志性的对象. 当一个Activity要启动另外一个Activity的时候,也许一个以前较为熟悉的模式是:调用一个new函数,直接创建具有窗口特征类的对象,又或者直接调用一个启动函数来启动.这种方式简洁.明了,但是却违背了Android开发的理念.Andro

  • Android 通过Intent使用Bundle传递对象详细介绍

    Android 通过Intent使用Bundle传递对象 Android开发中有时需要在应用中或进程间传递对象,下面详细介绍Intent使用Bundle传递对象的方法. 被传递的对象需要先实现序列化,而序列化对象有两种方式:java.io.Serializable和android.os.Parcelable Java中使用的是Serializable,而谷歌在Android使用了自定义的Parcelable. 两种序列化方式的区别: 1.在使用内存的时候,Parcelable比Serializa

  • Android Studio Intent隐式启动,发短信,拨号,打电话,访问网页等实例代码

    Android Studio Intent隐式启动,发短信,拨号,打电话,访问网页等实例代码 功能 创建5个按钮,隐式启动.发短信.拨号按钮.电话按钮.打开网页按钮.通过使用Intent来完成各自按钮下的功能 代码目录如下 详细代码如下: activity_main.xml代码如下 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://sc

  • Android编程使用Intent传递图片的方法详解

    本文实例讲述了Android编程使用Intent传递图片的方法.分享给大家供大家参考,具体如下: 基本思路是先把bitmap转化为byte数组,用Intent传递数组,在将数组转化为bitmap bitmap转化为byte数组的方法: private byte[] Bitmap2Bytes(Bitmap bm){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.

  • Android 标准Intent的使用详解

    一 Android系统用于Activity的标准Intent 1 根据联系人ID显示联系人信息 Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); //显示联系人信息 intent.setData(Uri.parse("content://contacts/people/492")); startActivity(intent); 2 根据联系人ID显示拨号面板 Intent intent = new

  • Android中Intent传递对象的3种方式详解

    前言 相信Intent的用法大家已经比较熟悉了,Intent可以用来启动Activity,Service等等,同时我们也可以通过Intent来进行传递数据,当我们使用Intent在Activity 间传递信息时发现putExtra() 方法并不支持自定义对象的传输,下面就来介绍三种解决方式. 一.Serializable 方式 这是最简单的一种方法,因为我们要做的就是让我们自定义的对象实现 Serializable 这个空接口. public class Person implements Se

  • Android实现打开各种文件的intent方法小结

    本文实例讲述了Android实现打开各种文件的intent方法.分享给大家供大家参考,具体如下: 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类, //可用于获取打开以下

  • Android手机内存中文件的读写方法小结

    本文实例总结了Android手机内存中文件的读写方法.分享给大家供大家参考.具体分析如下: 如何对手机内存中的文件数据进行读写呢? Context提供了领个方法来打开该应用程序的数据文件夹中的文件I/O流,具体如下: FileInputStream openFileInput(String name) 打开应用程序的数据文件夹下的name文件对应的数据流 FileOutputSream openFileOutput(String name, int mode) 打开应用程序数据文件夹下的name

  • 解决android studio 打开java文件 内容全变了的问题

    问题描述: 某天打开项目的activity的java文件界面突然变成下面这样了,但是用Notepad++打开代码什么的都正常,不知道什么原因造成的 解决办法 使用notepad++打开java文件,随便改个地方或者直接按俩空格再保存,返回AS一切恢复.... 补充知识:Android Studio 打开后无故爆红后解决办法,简单粗暴  有效治疗AndroidStudio大姨妈的方法. 今天打开AndroidSutudio后表示一脸蒙蔽,项目无故爆红,我本以为是哪里的代码有错导致 报错,于是乎逐个

  • OLEDB打开Excel文件的实现方法

    话不多说,上代码! class clsoledb { OleDbConnection connection; public void OpenConnection(string xlsFils) { if (!File.Exists(xlsFils)) { MessageBox.Show("文件" + xlsFils + "不存在", "提示"); return; } string conn = "Provider = Microsof

  • Android编程之include文件的使用方法

    本文实例分析了Android编程之include文件的使用方法.分享给大家供大家参考,具体如下: 记得很久以前,听一位大神说,程序员都很懒,不懒惰的程序员不是好程序员,当时不明白什么意思.后来慢慢的懂得了它的意思,好的程序员不要做重复的工作. 我们在android的布局文件中,常会遇到一些相同的布局,每个页面都写,一是比较麻烦,二是一旦有修改还得改多个文件.这个时候我们就可以用到include了. 非常简单的使用,下面看代码 include的文件scollandlisttitle.xml <?x

  • Android中实现延时执行操作的方法小结

    在Android开发中我们可能会有延时执行某个操作的需求,例如我们启动应用的时候,一开始呈现的是一个引导页面,过了两三秒后,会自动跳转到主界面.这就是一个延时操作. 下面是实现延时执行操作的几种方法: 1.使用线程的休眠实现延时操作 new Thread() { @Override public void run() { super.run(); Thread.sleep(3000);//休眠3秒 /** * 要执行的操作 */ } }.start(); 2.使用TimerTask实现延时操作

  • Android开发中Toast显示消息的方法小结

    本文实例总结了Android开发中Toast显示消息的方法.分享给大家供大家参考,具体如下: Android中提供一种简单的Toast消息提示框机制,可以在用户点击了某些按钮后,提示用户一些信息,提示的信息不能被用户点击,Toast的提示信息根据用户设置的显示时间后自动消失.Toast的提示信息可以在调试程序的时候方便的显示某些想显示的东西. 两种方法创建Toast 第一种方法的Java代码: makeText(Context context, int resId, int duration)

  • Android开发中TextView各种常见使用方法小结

    本文实例讲述了Android开发中TextView各种常见使用方法.分享给大家供大家参考,具体如下: 效果图: XML布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/root" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:

  • Python打包文件夹的方法小结(zip,tar,tar.gz等)

    本文实例讲述了Python打包文件夹的方法.分享给大家供大家参考,具体如下: 一.zip import os, zipfile #打包目录为zip文件(未压缩) def make_zip(source_dir, output_filename): zipf = zipfile.ZipFile(output_filename, 'w') pre_len = len(os.path.dirname(source_dir)) for parent, dirnames, filenames in os.

  • Android编程中延后处理事件的方法小结

    本文实例讲述了Android编程中延后处理事件的方法.分享给大家供大家参考,具体如下: 一.Handler和TimerTask相结合 1.首先定义TimerTask TimerTask task = new TimerTask(){ public void run() { Message message = new Message(); message.what = 1; handler.sendMessage(message); } }; 2.定义Handler Handler handler

随机推荐