├── .gitignore ├── .idea ├── codeStyles │ └── Project.xml ├── inspectionProfiles │ └── Project_Default.xml ├── markdown-navigator │ └── profiles_settings.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── LICENSE ├── Readme.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── assets │ ├── test.html │ └── test │ │ ├── TestDoc.doc │ │ ├── TestExcel.xls │ │ ├── TestPDF.pdf │ │ └── TestPPT.ppt │ ├── java │ └── com │ │ └── hanlyjiang │ │ └── library │ │ └── fileviewer │ │ └── demo │ │ ├── FileViewApplication.java │ │ ├── FileViewDemoMainActivity.java │ │ └── utils │ │ ├── FileUtils.java │ │ └── RunnableUtils.java │ └── res │ ├── drawable-v24 │ └── ic_launcher_foreground.xml │ ├── drawable │ └── ic_launcher_background.xml │ ├── layout │ └── activity_main.xml │ ├── mipmap-anydpi-v26 │ ├── ic_launcher.xml │ └── ic_launcher_round.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ └── values │ ├── colors.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── doc ├── fileviewer-demo-screenshots.gif └── 无法加载x5内核的解决方案.doc ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── lib_fileviewer ├── .gitignore ├── build.gradle ├── libs │ ├── lib_mupdf_1.11.1.jar │ └── tbs_sdk_thirdapp_v4.3.0.265_44165_sharewithdownloadwithfile_withoutGame_obfs_20220303_184913.jar ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── artifex │ │ └── mupdf │ │ └── fitz │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ ├── artifex │ │ │ └── mupdf │ │ │ │ └── viewer │ │ │ │ ├── CancellableAsyncTask.java │ │ │ │ ├── CancellableTaskDefinition.java │ │ │ │ ├── DocumentActivity.java │ │ │ │ ├── MuPDFCancellableTaskDefinition.java │ │ │ │ ├── MuPDFCore.java │ │ │ │ ├── OutlineActivity.java │ │ │ │ ├── PageAdapter.java │ │ │ │ ├── PageView.java │ │ │ │ ├── ReaderView.java │ │ │ │ ├── SearchTask.java │ │ │ │ ├── SearchTaskResult.java │ │ │ │ └── Stepper.java │ │ │ └── hanlyjiang │ │ │ └── library │ │ │ ├── fileviewer │ │ │ ├── FileViewer.java │ │ │ ├── tbs │ │ │ │ └── TBSFileViewActivity.java │ │ │ └── wps │ │ │ │ ├── WPSModel.java │ │ │ │ ├── WPSOpRecv.java │ │ │ │ └── WPSOpenUtils.java │ │ │ └── utils │ │ │ ├── AndroidUtils.java │ │ │ ├── FileViewerUtils.java │ │ │ └── IntentUtils.java │ ├── jniLibs │ │ ├── arm64-v8a │ │ │ └── libmupdf_java.so │ │ ├── armeabi-v7a │ │ │ └── libmupdf_java.so │ │ └── armeabi │ │ │ ├── liblbs.so │ │ │ └── libmupdf_java.so │ └── res │ │ ├── drawable-xxhdpi │ │ └── ic_go_back.png │ │ ├── drawable │ │ ├── button.xml │ │ ├── common_divider_shape.xml │ │ ├── ic_chevron_left_white_24dp.xml │ │ ├── ic_chevron_right_white_24dp.xml │ │ ├── ic_close_white_24dp.xml │ │ ├── ic_link_white_24dp.xml │ │ ├── ic_search_white_24dp.xml │ │ ├── ic_toc_white_24dp.xml │ │ ├── page_indicator.xml │ │ ├── seek_line.xml │ │ └── seek_thumb.xml │ │ ├── layout │ │ ├── activity_tbs_file_view_layout.xml │ │ └── document_activity.xml │ │ └── values │ │ ├── colors.xml │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── artifex │ └── mupdf │ └── fitz │ └── ExampleUnitTest.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | /.idea/workspace.xml 4 | /.idea/libraries 5 | #/.idea/misc.xml 6 | #/.idea/modules.xml 7 | .DS_Store 8 | /build 9 | /captures 10 | 11 | 12 | # Built application files 13 | *.apk 14 | *.ap_ 15 | 16 | # Files for the ART/Dalvik VM 17 | *.dex 18 | 19 | # Java class files 20 | *.class 21 | 22 | # Generated files 23 | bin/ 24 | gen/ 25 | out/ 26 | 27 | # Gradle files 28 | .gradle/ 29 | build/ 30 | 31 | # Local configuration file (sdk path, etc) 32 | local.properties 33 | 34 | # Proguard folder generated by Eclipse 35 | proguard/ 36 | 37 | # Log Files 38 | *.log 39 | 40 | # Android Studio Navigation editor temp files 41 | .navigation/ 42 | 43 | # Android Studio captures folder 44 | captures/ 45 | 46 | # Intellij 47 | *.iml 48 | .idea/workspace.xml 49 | .idea/tasks.xml 50 | .idea/gradle.xml 51 | .idea/dictionaries 52 | .idea/libraries 53 | 54 | # Keystore files 55 | *.jks 56 | 57 | # External native build folder generated in Android Studio 2.2 and later 58 | .externalNativeBuild 59 | 60 | # Google Services (e.g. APIs or Firebase) 61 | google-services.json 62 | 63 | # Freeline 64 | freeline.py 65 | freeline/ 66 | freeline_project_description.json 67 | 68 | 69 | # Built application files 70 | *.apk 71 | *.ap_ 72 | 73 | # Files for the ART/Dalvik VM 74 | *.dex 75 | 76 | # Java class files 77 | *.class 78 | 79 | # Generated files 80 | bin/ 81 | gen/ 82 | out/ 83 | 84 | # Gradle files 85 | .gradle/ 86 | build/ 87 | 88 | # Local configuration file (sdk path, etc) 89 | local.properties 90 | 91 | # Proguard folder generated by Eclipse 92 | proguard/ 93 | 94 | # Log Files 95 | *.log 96 | 97 | # Android Studio Navigation editor temp files 98 | .navigation/ 99 | 100 | # Android Studio captures folder 101 | captures/ 102 | 103 | # IntelliJ 104 | *.iml 105 | .idea/workspace.xml 106 | .idea/tasks.xml 107 | .idea/gradle.xml 108 | .idea/assetWizardSettings.xml 109 | .idea/dictionaries 110 | .idea/libraries 111 | .idea/caches 112 | 113 | # Keystore files 114 | # Uncomment the following line if you do not want to check your keystore files in. 115 | #*.jks 116 | 117 | # External native build folder generated in Android Studio 2.2 and later 118 | .externalNativeBuild 119 | 120 | # Google Services (e.g. APIs or Firebase) 121 | google-services.json 122 | 123 | # Freeline 124 | freeline.py 125 | freeline/ 126 | freeline_project_description.json 127 | 128 | # fastlane 129 | fastlane/report.xml 130 | fastlane/Preview.html 131 | fastlane/screenshots 132 | fastlane/test_output 133 | fastlane/readme.md 134 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | xmlns:android 14 | 15 | ^$ 16 | 17 | 18 | 19 |
20 |
21 | 22 | 23 | 24 | xmlns:.* 25 | 26 | ^$ 27 | 28 | 29 | BY_NAME 30 | 31 |
32 |
33 | 34 | 35 | 36 | .*:id 37 | 38 | http://schemas.android.com/apk/res/android 39 | 40 | 41 | 42 |
43 |
44 | 45 | 46 | 47 | .*:name 48 | 49 | http://schemas.android.com/apk/res/android 50 | 51 | 52 | 53 |
54 |
55 | 56 | 57 | 58 | name 59 | 60 | ^$ 61 | 62 | 63 | 64 |
65 |
66 | 67 | 68 | 69 | style 70 | 71 | ^$ 72 | 73 | 74 | 75 |
76 |
77 | 78 | 79 | 80 | .* 81 | 82 | ^$ 83 | 84 | 85 | BY_NAME 86 | 87 |
88 |
89 | 90 | 91 | 92 | .* 93 | 94 | http://schemas.android.com/apk/res/android 95 | 96 | 97 | ANDROID_ATTRIBUTE_ORDER 98 | 99 |
100 |
101 | 102 | 103 | 104 | .* 105 | 106 | .* 107 | 108 | 109 | BY_NAME 110 | 111 |
112 |
113 |
114 |
115 |
116 |
-------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 36 | -------------------------------------------------------------------------------- /.idea/markdown-navigator/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 24 | 41 | 42 | 43 | 44 | 45 | 46 | 48 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 蒋航 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | 2 | ## 在 Android 上查看word,excel,powerpoint,pdf 3 | 4 | 示例app效果: 5 | 6 | ![示例app效果](doc/fileviewer-demo-screenshots.gif) 7 | 8 | ## 如何使用? 9 | 参考: [示例](app) 10 | * 复制`lib_fileviewer`模块,加入到project 11 | * App中build.gradle中加入以下配置: 12 | ```groovy 13 | ndk { 14 | // 此处必须设置为 armeabi ,TBS 文件浏览不支持其他类型 15 | abiFilters "armeabi" 16 | } 17 | ``` 18 | * TBS初始化(在Application中): 19 | ```java 20 | QbSdk.initX5Environment(getApplicationContext(), new QbSdk.PreInitCallback() { 21 | @Override 22 | public void onCoreInitFinished() { 23 | Log.d(TAG, "onCoreInitFinished"); 24 | } 25 | 26 | @Override 27 | public void onViewInitFinished(boolean initResult) { 28 | Log.e(TAG, "onViewInitFinished" + initResult); 29 | } 30 | }); 31 | ``` 32 | 33 | * **查看文件统一入口:** 34 | ``` 35 | Uri uri = Uri.fromFile(new File(filePath)); 36 | FileViewer.viewFile(context,uri) 37 | ``` 38 | * **直接使用mupdf查看:** 39 | ```java 40 | FileViewer.viewPDFWithMuPDFByPath(Context context, String filePath) 41 | ``` 42 | 或: 43 | ```java 44 | FileViewer.startMuPDFActivityByUri(Context context, Uri documentUri) 45 | ``` 46 | 47 | * **直接使用TBS查看word文档:** 48 | ``` 49 | TBSFileViewActivity.viewFile(context, filePath); 50 | ``` 51 | 52 | ## 注意事项 53 | 1. office文件无法查看(TBS初始化失败),可以查看这个文档:[无法加载x5内核的解决方案.doc](doc/无法加载x5内核的解决方案.doc) 54 | 55 | 56 | ## 使用到的库: 57 | ### PDF查看: mupdf 58 | 59 | 版本: v1.11.1 60 | 61 | 62 | > 介绍: 63 | > MuPDF is an open source software framework for viewing and converting PDF, XPS, and E-book documents. There are viewers for various platforms, several command line tools, and a software library for building tools and applications. 64 | 65 | 项目地址: 66 | https://mupdf.com/docs/ 67 | 68 | Android 文档: 69 | https://mupdf.com/docs/android-sdk.html 70 | 71 | ### word等文件查看 : TBS(腾讯浏览服务) 72 | > **简介:** 73 | > web内核 74 | 75 | 76 | [官方页面](http://x5.tencent.com/) 77 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 30 5 | defaultConfig { 6 | applicationId "com.hanlyjiang.github.fileviewer.demo" 7 | minSdkVersion 19 8 | targetSdkVersion 22 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' 12 | ndk { 13 | abiFilters "armeabi", "armeabi-v7a" 14 | } 15 | } 16 | 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | compileOptions { 24 | targetCompatibility 1.8 25 | sourceCompatibility 1.8 26 | } 27 | } 28 | 29 | dependencies { 30 | implementation fileTree(dir: 'libs', include: ['*.jar']) 31 | implementation 'androidx.appcompat:appcompat:1.0.0' 32 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 33 | testImplementation 'junit:junit:4.12' 34 | androidTestImplementation 'androidx.test.ext:junit:1.1.1' 35 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' 36 | // https://github.com/googlesamples/easypermissions 37 | implementation 'pub.devrel:easypermissions:1.1.0' 38 | implementation project(':lib_fileviewer') 39 | } 40 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 22 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 35 | 36 | 37 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /app/src/main/assets/test/TestDoc.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hanlyjiang/AndroidDocumentViewer/efc4515aa3a14cda13fb348b783788a32cd41ede/app/src/main/assets/test/TestDoc.doc -------------------------------------------------------------------------------- /app/src/main/assets/test/TestExcel.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hanlyjiang/AndroidDocumentViewer/efc4515aa3a14cda13fb348b783788a32cd41ede/app/src/main/assets/test/TestExcel.xls -------------------------------------------------------------------------------- /app/src/main/assets/test/TestPDF.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hanlyjiang/AndroidDocumentViewer/efc4515aa3a14cda13fb348b783788a32cd41ede/app/src/main/assets/test/TestPDF.pdf -------------------------------------------------------------------------------- /app/src/main/assets/test/TestPPT.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hanlyjiang/AndroidDocumentViewer/efc4515aa3a14cda13fb348b783788a32cd41ede/app/src/main/assets/test/TestPPT.ppt -------------------------------------------------------------------------------- /app/src/main/java/com/hanlyjiang/library/fileviewer/demo/FileViewApplication.java: -------------------------------------------------------------------------------- 1 | package com.hanlyjiang.library.fileviewer.demo; 2 | 3 | import android.app.Application; 4 | import android.util.Log; 5 | 6 | import com.hanlyjiang.library.fileviewer.demo.utils.FileUtils; 7 | import com.tencent.smtt.sdk.QbSdk; 8 | 9 | import java.io.File; 10 | import java.io.IOException; 11 | 12 | 13 | /** 14 | * 测试Application,用于初始化X5内核 15 | * 16 | * @author hanlyjiang 17 | */ 18 | public class FileViewApplication extends Application { 19 | 20 | public static String FILE_DIR = "/sdcard/Downloads/test/"; 21 | public static final String TAG = "TBSInit"; 22 | 23 | @Override 24 | public void onCreate() { 25 | super.onCreate(); 26 | initX5Web(); 27 | FILE_DIR = new File(getFilesDir(), "test").getAbsolutePath() + File.separator; 28 | try { 29 | FileUtils.copyAssetsDir(this, "test", FILE_DIR); 30 | } catch (IOException e) { 31 | e.printStackTrace(); 32 | } 33 | } 34 | 35 | private void initX5Web() { 36 | Log.i(TAG, "QbSdk.initX5Environment"); 37 | QbSdk.initX5Environment(getApplicationContext(), new QbSdk.PreInitCallback() { 38 | @Override 39 | public void onCoreInitFinished() { 40 | Log.d(TAG, "onCoreInitFinished"); 41 | } 42 | 43 | @Override 44 | public void onViewInitFinished(boolean initResult) { 45 | Log.e(TAG, "onViewInitFinished:" + initResult); 46 | } 47 | }); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /app/src/main/java/com/hanlyjiang/library/fileviewer/demo/FileViewDemoMainActivity.java: -------------------------------------------------------------------------------- 1 | package com.hanlyjiang.library.fileviewer.demo; 2 | 3 | import android.Manifest; 4 | import android.graphics.PixelFormat; 5 | import android.net.Uri; 6 | import android.os.Bundle; 7 | import androidx.annotation.NonNull; 8 | import androidx.appcompat.app.AppCompatActivity; 9 | import android.util.Log; 10 | import android.widget.Toast; 11 | 12 | import com.hanlyjiang.library.fileviewer.FileViewer; 13 | import com.hanlyjiang.library.fileviewer.tbs.TBSFileViewActivity; 14 | import com.tencent.smtt.sdk.WebView; 15 | 16 | import java.io.File; 17 | 18 | import pub.devrel.easypermissions.AfterPermissionGranted; 19 | import pub.devrel.easypermissions.EasyPermissions; 20 | import pub.devrel.easypermissions.PermissionRequest; 21 | 22 | import static com.hanlyjiang.library.fileviewer.demo.FileViewApplication.FILE_DIR; 23 | 24 | 25 | public class FileViewDemoMainActivity extends AppCompatActivity { 26 | 27 | private static final int RC_WRITE_STOREGE = 1; 28 | private static final String TAG = "TBSInit"; 29 | protected WebView tbsWebView; 30 | 31 | @Override 32 | protected void onCreate(Bundle savedInstanceState) { 33 | super.onCreate(savedInstanceState); 34 | getWindow().setFormat(PixelFormat.TRANSLUCENT); 35 | super.setContentView(R.layout.activity_main); 36 | initView(); 37 | initWebView(); 38 | requestPermissions(); 39 | } 40 | 41 | private void requestPermissions() { 42 | String[] perms = new String[]{ 43 | Manifest.permission.WRITE_EXTERNAL_STORAGE, 44 | Manifest.permission.READ_EXTERNAL_STORAGE, 45 | Manifest.permission.READ_PHONE_STATE 46 | }; 47 | if (!EasyPermissions.hasPermissions(this, perms)) { 48 | EasyPermissions.requestPermissions( 49 | new PermissionRequest.Builder(this, RC_WRITE_STOREGE, perms) 50 | .setRationale("请求权限") 51 | .setPositiveButtonText("确认") 52 | .setNegativeButtonText("取消") 53 | .build()); 54 | } 55 | } 56 | 57 | 58 | private void initWebView() { 59 | tbsWebView = (WebView) findViewById(R.id.tbs_webView); 60 | tbsWebView.loadUrl("file:///android_asset/test.html"); 61 | tbsWebView.setDrawingCacheEnabled(true); 62 | } 63 | 64 | 65 | private void initView() { 66 | findViewById(R.id.btn_open_pdf_with_mupdf).setOnClickListener(view -> { 67 | String fileName = getFilePath("TestPDF.pdf"); 68 | startMuPDFActivityWithExampleFile(fileName); 69 | }); 70 | findViewById(R.id.btn_open_pdf_with_tbs).setOnClickListener((view) -> { 71 | openFileWithTbs(getFilePath("TestPDF.pdf")); 72 | }); 73 | findViewById(R.id.btn_open_doc_with_tbs).setOnClickListener(view -> openFileWithTbs(getFilePath("TestDoc.doc"))); 74 | findViewById(R.id.btn_open_ppt_with_tbs).setOnClickListener(view -> openFileWithTbs(getFilePath("TestPPT.ppt"))); 75 | findViewById(R.id.btn_open_excel_with_tbs).setOnClickListener(view -> openFileWithTbs(getFilePath("TestExcel.xls"))); 76 | } 77 | 78 | @NonNull 79 | private String getFilePath(String fileName) { 80 | return new File(FILE_DIR + fileName).getAbsolutePath(); 81 | } 82 | 83 | private void openFileWithTbs(String filePath) { 84 | Log.d(TAG, "Open File: " + filePath); 85 | TBSFileViewActivity.viewFile(this, filePath); 86 | } 87 | 88 | public void startMuPDFActivityWithExampleFile(String fileName) { 89 | File file = new File(fileName); 90 | if (!file.isFile()) { 91 | Toast.makeText(this, "文件不存在", Toast.LENGTH_SHORT).show(); 92 | return; 93 | } 94 | Uri uri = Uri.fromFile(file); 95 | FileViewer.startMuPDFActivityByUri(this, uri); 96 | } 97 | 98 | 99 | @Override 100 | public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 101 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 102 | // Forward results to EasyPermissions 103 | EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this); 104 | } 105 | 106 | @AfterPermissionGranted(RC_WRITE_STOREGE) 107 | private void methodRequiresTwoPermission() { 108 | String[] perms = {Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION}; 109 | if (EasyPermissions.hasPermissions(this, perms)) { 110 | // Already have permission, do the thing 111 | // ... 112 | } else { 113 | // Do not have permissions, request them now 114 | EasyPermissions.requestPermissions(this, "请求权限", 115 | RC_WRITE_STOREGE, perms); 116 | } 117 | } 118 | 119 | } 120 | -------------------------------------------------------------------------------- /app/src/main/java/com/hanlyjiang/library/fileviewer/demo/utils/FileUtils.java: -------------------------------------------------------------------------------- 1 | package com.hanlyjiang.library.fileviewer.demo.utils; 2 | 3 | import android.content.Context; 4 | 5 | import java.io.File; 6 | import java.io.FileOutputStream; 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | 10 | 11 | public class FileUtils { 12 | 13 | /** 14 | * 拷贝Asset文件夹到sd卡 15 | * 16 | * @param context 17 | * @param fromDir 如果拷贝assets ,则传入 "" 18 | * @param destDir 目的地 19 | * @throws IOException 20 | */ 21 | public static void copyAssetsDir(Context context, String fromDir, String destDir) throws IOException { 22 | String[] files = context.getAssets().list(fromDir); 23 | for (String f : files) { 24 | copyFile(context.getAssets().open(fromDir + File.separator + f), destDir + File.separator + f); 25 | } 26 | } 27 | 28 | public static void copyFile(InputStream in, String newPath) { 29 | new File(newPath).getParentFile().mkdirs(); 30 | try ( 31 | InputStream inStream = in; 32 | FileOutputStream fs = new FileOutputStream(newPath) 33 | ) { 34 | int byteread; 35 | byte[] buffer = new byte[4096]; 36 | while ((byteread = inStream.read(buffer)) != -1) { 37 | fs.write(buffer, 0, byteread); 38 | fs.flush(); 39 | } 40 | } catch (Exception e) { 41 | e.printStackTrace(); 42 | } 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /app/src/main/java/com/hanlyjiang/library/fileviewer/demo/utils/RunnableUtils.java: -------------------------------------------------------------------------------- 1 | package com.hanlyjiang.library.fileviewer.demo.utils; 2 | 3 | import android.os.Handler; 4 | import android.os.Looper; 5 | import android.os.Message; 6 | 7 | import java.util.concurrent.Callable; 8 | import java.util.concurrent.ExecutorService; 9 | import java.util.concurrent.Future; 10 | import java.util.concurrent.LinkedBlockingDeque; 11 | import java.util.concurrent.ThreadPoolExecutor; 12 | import java.util.concurrent.TimeUnit; 13 | 14 | 15 | /** 16 | * 替代之前封装在MapActivity中的 任务执行函数 17 | *
18 | * Created by hanlyjiang on 2016/12/5. 19 | */ 20 | public class RunnableUtils { 21 | 22 | private static final ExecutorService EXECUTOR = getExecutorService(); 23 | 24 | private static ExecutorService getExecutorService() { 25 | if (EXECUTOR == null) { 26 | return new ThreadPoolExecutor(2, 2, 60, 27 | TimeUnit.SECONDS, new LinkedBlockingDeque<>(), r -> new Thread(r, "RunnableUtils")); 28 | } 29 | return EXECUTOR; 30 | } 31 | 32 | private static UiHandler sUiHandler = new UiHandler(Looper.getMainLooper()); 33 | 34 | /** 35 | * 在UI线程中执行一个Runnable 36 | * 37 | * @param task 要执行的任务 38 | */ 39 | public static void postUi(Runnable task) { 40 | postUiWork(task); 41 | } 42 | 43 | /** 44 | * 在UI线程中执行一个Runnable 45 | * 46 | * @param task 要执行的任务 47 | * @param delay 延迟(MILLISECONDS) 1000ms = 1s 48 | */ 49 | public static void postUi(Runnable task, int delay) { 50 | postUiWork(task, delay); 51 | } 52 | 53 | /** 54 | * 在后台 线程中执行一个 Callable 55 | * 56 | * @param callableTask 要执行的任务 57 | * @return Future 对象 58 | */ 59 | public static Future executeOnWorkThread(Callable callableTask) { 60 | return EXECUTOR.submit(callableTask); 61 | } 62 | 63 | /** 64 | * 在IO 线程中执行一个Runnable 65 | * 66 | * @param task 要执行的任务 67 | */ 68 | public static void executeOnWorkThread(Runnable task) { 69 | EXECUTOR.execute(task); 70 | } 71 | 72 | 73 | private static void postUiWork(Runnable runnable, int delay) { 74 | Message msg = sUiHandler.obtainMessage(); 75 | msg.obj = runnable; 76 | msg.arg1 = delay; 77 | if (sUiHandler == null) { 78 | sUiHandler = new UiHandler(Looper.getMainLooper()); 79 | } 80 | sUiHandler.sendMessage(msg); 81 | } 82 | 83 | private static void postUiWork(Runnable runnable) { 84 | if (sUiHandler == null) { 85 | sUiHandler = new UiHandler(Looper.getMainLooper()); 86 | } 87 | sUiHandler.post(runnable); 88 | } 89 | 90 | private static class UiHandler extends Handler { 91 | 92 | UiHandler(Looper looper) { 93 | super(looper); 94 | } 95 | 96 | @Override 97 | public void handleMessage(Message msg) { 98 | super.handleMessage(msg); 99 | if (msg.obj != null) { 100 | if (msg.arg1 > 0) { 101 | postDelayed((Runnable) msg.obj, msg.arg1); 102 | } else { 103 | post((Runnable) msg.obj); 104 | } 105 | } 106 | } 107 | } 108 | 109 | } 110 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 12 | 13 | 18 | 19 |