`
test_angel
  • 浏览: 51645 次
  • 性别: Icon_minigender_2
  • 来自: 西安
社区版块
存档分类
最新评论

Intent(open file) android

 
阅读更多

这周我就写写如何在android上打开各种文件的方法(前提:你安装了相应文件的阅读器,eg:pdf)

 

step1:我们将文件的后缀和相应的datatype存入hashmap中,例如:

			HashMap<String , String> mTypeMap = new HashMap<String, String>();
			mTypeMap.put("pdf", "application/pdf");
			mTypeMap.put("rar", "application/rar");
			mTypeMap.put("doc", "application/msword");
			mTypeMap.put("mp3", "audio/mpeg");

 

step2:根据文件名获取文件后缀:eg.  pdf

 

step3:根据后缀名获取datatype:eg. pdf  ---------->application/pdf,假设其方法名为:getMineType(String name)

 

step4:new Intent,并将文件的路径传递给准备跳转的activity:

String action = "android.intent.action.VIEW";
Intent intent = new Intent();
intent.setAction(action);
intent.setDataAndType(uri, getMineType("pdf"));
startActivity(intent);

 

 

 

注:以下是一些后缀对应的datatype,可以参考参考:

"application/ogg", "ogg"
"application/pdf", "pdf"
"application/rar", "rar"
"application/rdf+xml", "rdf"
"application/rss+xml", "rss"
"application/zip", "zip"
"application/vnd.android.package-archive",  "apk"
"application/vnd.oasis.opendocument.database", "odb"
"application/vnd.oasis.opendocument.formula", "odf"
"application/vnd.oasis.opendocument.graphics", "odg"
"application/vnd.oasis.opendocument.spreadsheet", "ods"
"application/vnd.oasis.opendocument.spreadsheet-template","ots"
"application/vnd.oasis.opendocument.text", "odt"
"application/vnd.oasis.opendocument.text-master", "odm"
"application/msword", "doc"
"application/msword", "dot"
"application/vnd.openxmlformats-officedocument.wordprocessingml.document","docx"
"application/vnd.ms-excel", "xls"
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet","xlsx"
"application/vnd.ms-powerpoint", "ppt"
"application/vnd.openxmlformats-officedocument.presentationml.presentation","pptx");
"application/vnd.visio", "vsd"
"audio/3gpp", "3gpp"
 "audio/mpeg", "mp3"
 "audio/x-ms-wma", "wma"
 "audio/x-pn-realaudio", "rm"
 "image/bmp", "bmp"
  "image/gif", "gif"
 "image/ico", "cur"
"image/ico", "ico"
  "image/x-ms-bmp", "bmp"
  "text/css", "css"
 "text/html", "htm"
"text/html", "html"
   "text/plain", "txt"
 "text/plain", "text"
"text/xml", "xml"
"text/x-c++src", "c++"
"text/x-c++src", "cpp"
"text/x-component", "htc"
"text/x-csh", "csh"
"text/x-csrc", "c"
"text/x-dsrc", "d"
"text/x-haskell", "hs"
"text/x-java", "java"
"text/x-literate-haskell", "lhs"
"text/x-moc", "moc"
"text/x-pascal", "p"
"text/x-pascal", "pas"
"text/x-pcs-gcd", "gcd"
"text/x-setext", "etx"
"text/x-tcl", "tcl"
"text/x-tex", "tex"
"text/x-tex", "ltx"
"text/x-tex", "sty"
"text/x-tex", "cls"
"text/x-vcalendar", "vcs");
"text/x-vcard", "vcf");
"video/3gpp", "3gpp"
"video/3gpp", "3gp"
"video/3gpp", "3g2"
"video/dl", "dl"
"video/dv", "dif"
"video/dv", "dv"
"video/fli", "fli"
"video/m4v", "m4v"
"video/mpeg", "mpeg"
"video/mpeg", "mpg"
"video/mpeg", "mpe"
"video/mp4", "mp4"
"video/mpeg", "vob"
"video/quicktime", "qt"
"video/quicktime", "mov"
"video/vnd.mpegurl", "mxu"
"video/x-la-asf", "lsf"
"video/x-la-asf", "lsx"
"video/x-mng", "mng"
"video/x-ms-asf", "asf"
"video/x-ms-asf", "asx"
"video/x-ms-wm", "wm"
"video/x-ms-wmv", "wmv"
"video/x-ms-wmx", "wmx"
"video/x-ms-wvx", "wvx"
"video/x-msvideo", "avi"
"video/x-sgi-movie", "movie"
"x-conference/x-cooltalk", "ice"
"x-epoc/x-sisx-app", "sisx"
            
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论
1 楼 liuy2012 2012-06-29  
很强大 学习了~~

相关推荐

    open_file:一个可以调用原生APP打开带字符串文件导致flutter的插件,支持iOS(UTI) android(intent) PC(ffi) web(dart)

    打开文件 一个可以调用原生APP打开带字符串文件导致flutter的插件,支持iOS(DocumentInteraction) / android(intent) / PC(ffi) / web(dart...//OpenFile.open("/sdcard/example.txt", type: "text/plain", uti: "publ

    Android中使用Intent获取其他应用程序信息的方法介绍.pdf

    private void openFile(File f) { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(android.content.Intent.ACTION_VIEW); String type = getMIMEType(f); ...

    打开附件.java

    public static void openFile(String fileName){ if(fileName!=null && !"".equals(fileName)){ String name = fileName.toLowerCase(); if(name.lastIndexOf("doc") &gt;=0 || name.lastIndexOf("docx") &gt;=0){ //...

    Android判断和读取图标列表iconFile实例

    判断图标被点击的动作,操作触屏手机的基本要点,使用自定义的MyAdapter来将数据传入ListActivity,设置ListItem被点击时要做的动作,如果是文件夹就再运行getFileDir() ,如果是文件就运行openFile() ,调用...

    Android应用程序源代码FileManager

    FileManager可能响应`ACTION_OPEN_DOCUMENT_TREE`或`ACTION_GET_CONTENT`等Intent,允许用户选择文件或目录。同时,理解Activity的生命周期,如`onCreate()`, `onStart()`, `onResume()`, `onPause()`, `onStop()`,...

    Android中安装asset下的apk

    intent.setDataAndType(Uri.fromFile(new File(fileApk.getPath())), "application/vnd.android.package-archive"); startActivity(intent); } ``` #### 四、代码解析 1. **读取APK文件**: - 使用`getAssets...

    Android 目录选择器实现

    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); startActivityForResult(intent, REQUEST_CODE_SELECT_DIR); ``` 在`onActivityResult`回调中,我们可以获取到用户选择的目录Uri: ```java @...

    android 打开各种文档的demo

    startActivityForResult(intent, REQUEST_CODE_OPEN_FILE); ``` 7. **处理返回结果** 当用户从文件选择器选择文件后,你需要在`onActivityResult()`中处理返回的结果: ```java @Override protected void ...

    android下文件选择

    toast(getText(R.string.open_file_error_format)); } } }; public boolean onKeyDown(int keyCode , KeyEvent event){ if(event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == ...

    Android 用工具打开word,pdf等文件

    本文将详细讲解如何在Android应用中调用系统内置或第三方工具来打开这些文件,并提供一个名为`OpenWord.java`的核心代码实现。 首先,Android系统支持通过Intent机制来启动其他应用程序并传递数据。当需要打开一个...

    Android U盘插拔监听详细版以及U盘写入权限解决办法

    UsbDeviceConnection connection = usbManager.openDevice(device); if (connection != null) { boolean claimSuccess = connection.claimInterface(intf, true); if (claimSuccess) { try { // 获取U盘根目录 ...

    Android SD数据存储

    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); startActivityForResult(intent, REQUEST_CODE_PICK_DIR); ``` 然后在`onActivityResult`中处理返回的`treeUri`,并使用`DocumentFile`进行文件...

    android开发版本更新下载

    通过`openFile`方法,可以利用系统默认的应用来打开并安装APK文件。 ```java private void openFile(File file) { Log.e("OpenFile", file.getName()); Intent intent = new Intent(); intent.addFlags(Intent....

    Android webview调用相机和相册

    android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"&gt; ``` 然后,在res/xml目录下创建一个file_paths.xml文件,配置文件路径: ```xml &lt;paths xmlns:android=...

    Android WebView 不支持 H5 input type="file" 解决方法

    Toast.makeText(getBaseContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show(); return false; } return true; } //For Android 4.1 only protected void openFileChooser(ValueCallback&lt;Uri&gt; ...

    Android获取SDCard路径/Android获取存储器挂载点

    如果需要让用户选择存储位置,可以使用Intent ACTION_OPEN_DOCUMENT_TREE,启动文件选择器,并通过ActivityResult APIs处理用户的选择。 7. Android Q及更高版本的适配: 从Android 10开始,应用通常不能直接写入...

    Android-使用Android Studio实现第三方QQ登录

    implementation fileTree(dir: 'libs', include: ['qqconnect.jar', 'open_sdk_rXXX.jar']) ``` 记得同步项目,这样Gradle会自动处理这些依赖。 2. **手动添加**:如果选择手动添加,右键点击项目中的`libs`目录...

    Android-使用基础的网络请求方式使用Service下载网络apk文件

    android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.file_provider" android:exported="false" android:grantUriPermissions="true"&gt; android:name="android.support...

    android下载word文档并打开

    intent.setDataAndType(Uri.fromFile(new File("/path/to/document.docx")), "application/vnd.openxmlformats-officedocument.wordprocessingml.document"); startActivity(intent); ``` 如果用户设备上没有...

    android studio 保存图片到本地相册

    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); mediaScanIntent.setData(Uri.fromFile(imageFile)); sendBroadcast(mediaScanIntent); } ``` 以上代码示例涵盖了从网络下载图片...

Global site tag (gtag.js) - Google Analytics