这周我就写写如何在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"
分享到:
相关推荐
打开文件 一个可以调用原生APP打开带字符串文件导致flutter的插件,支持iOS(DocumentInteraction) / android(intent) / PC(ffi) / web(dart...//OpenFile.open("/sdcard/example.txt", type: "text/plain", uti: "publ
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); ...
public static void openFile(String fileName){ if(fileName!=null && !"".equals(fileName)){ String name = fileName.toLowerCase(); if(name.lastIndexOf("doc") >=0 || name.lastIndexOf("docx") >=0){ //...
判断图标被点击的动作,操作触屏手机的基本要点,使用自定义的MyAdapter来将数据传入ListActivity,设置ListItem被点击时要做的动作,如果是文件夹就再运行getFileDir() ,如果是文件就运行openFile() ,调用...
FileManager可能响应`ACTION_OPEN_DOCUMENT_TREE`或`ACTION_GET_CONTENT`等Intent,允许用户选择文件或目录。同时,理解Activity的生命周期,如`onCreate()`, `onStart()`, `onResume()`, `onPause()`, `onStop()`,...
intent.setDataAndType(Uri.fromFile(new File(fileApk.getPath())), "application/vnd.android.package-archive"); startActivity(intent); } ``` #### 四、代码解析 1. **读取APK文件**: - 使用`getAssets...
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); startActivityForResult(intent, REQUEST_CODE_SELECT_DIR); ``` 在`onActivityResult`回调中,我们可以获取到用户选择的目录Uri: ```java @...
startActivityForResult(intent, REQUEST_CODE_OPEN_FILE); ``` 7. **处理返回结果** 当用户从文件选择器选择文件后,你需要在`onActivityResult()`中处理返回的结果: ```java @Override protected void ...
toast(getText(R.string.open_file_error_format)); } } }; public boolean onKeyDown(int keyCode , KeyEvent event){ if(event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == ...
本文将详细讲解如何在Android应用中调用系统内置或第三方工具来打开这些文件,并提供一个名为`OpenWord.java`的核心代码实现。 首先,Android系统支持通过Intent机制来启动其他应用程序并传递数据。当需要打开一个...
UsbDeviceConnection connection = usbManager.openDevice(device); if (connection != null) { boolean claimSuccess = connection.claimInterface(intf, true); if (claimSuccess) { try { // 获取U盘根目录 ...
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); startActivityForResult(intent, REQUEST_CODE_PICK_DIR); ``` 然后在`onActivityResult`中处理返回的`treeUri`,并使用`DocumentFile`进行文件...
通过`openFile`方法,可以利用系统默认的应用来打开并安装APK文件。 ```java private void openFile(File file) { Log.e("OpenFile", file.getName()); Intent intent = new Intent(); intent.addFlags(Intent....
android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"> ``` 然后,在res/xml目录下创建一个file_paths.xml文件,配置文件路径: ```xml <paths xmlns:android=...
Toast.makeText(getBaseContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show(); return false; } return true; } //For Android 4.1 only protected void openFileChooser(ValueCallback<Uri> ...
如果需要让用户选择存储位置,可以使用Intent ACTION_OPEN_DOCUMENT_TREE,启动文件选择器,并通过ActivityResult APIs处理用户的选择。 7. Android Q及更高版本的适配: 从Android 10开始,应用通常不能直接写入...
implementation fileTree(dir: 'libs', include: ['qqconnect.jar', 'open_sdk_rXXX.jar']) ``` 记得同步项目,这样Gradle会自动处理这些依赖。 2. **手动添加**:如果选择手动添加,右键点击项目中的`libs`目录...
android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.file_provider" android:exported="false" android:grantUriPermissions="true"> android:name="android.support...
intent.setDataAndType(Uri.fromFile(new File("/path/to/document.docx")), "application/vnd.openxmlformats-officedocument.wordprocessingml.document"); startActivity(intent); ``` 如果用户设备上没有...
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); mediaScanIntent.setData(Uri.fromFile(imageFile)); sendBroadcast(mediaScanIntent); } ``` 以上代码示例涵盖了从网络下载图片...