其实使用起来很方便,好像知乎也用了。也没有深入去研究写,主要看到两个项目优点多,几行代码就搞定!
在manifest中配置权限
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" tools:ignore="ProtectedPermissions" /> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>另外加一行配置Android9.0的
<application android:networkSecurityConfig="@xml/network_security_config">之后在相应的/res/xml/network_security_config.xml配置
<?xml version="1.0" encoding="utf-8"?> <network-security-config> <domain-config cleartextTrafficPermitted="true"> <domain includeSubdomains="true">127.0.0.1</domain> <domain includeSubdomains="true">localhost</domain> </domain-config> </network-security-config>布局文件就两个按钮,下载电子书文件和打开电子书文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/start" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Download book" /> <Button android:id="@+id/openbook" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Open book" /> </LinearLayout>在仓库中添加依赖包
implementation "com.folioreader:folioreader:0.5.1" implementation 'com.arialyy.aria:aria-core:3.5.1' annotationProcessor 'com.arialyy.aria:aria-compiler:3.5.1'主Activity代码
package com.bookreader.zangke.bookreader; import android.app.Activity; import android.os.Environment; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.arialyy.aria.core.Aria; import com.folioreader.FolioReader; public class MainActivity extends Activity { private Button start,openbook; private String DOWNLOAD_URL="http://bmob-cdn-2250.b0.upaiyun.com/2018/11/21/aefd267e400d1c8c8076ceed5d743ad2.epub"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Aria.download(this).register(); start = findViewById(R.id.start); final String filepath = Environment.getExternalStorageDirectory().getPath()+"/bookeader/1.epub"; start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Aria.download(this) .load(DOWNLOAD_URL) .setFilePath(filepath,true) .start(); Toast.makeText(MainActivity.this,"Downloading book",Toast.LENGTH_SHORT).show(); } }); final FolioReader folioReader = FolioReader.get(); openbook =findViewById(R.id.openbook); openbook.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { folioReader.openBook("sdcard/bookeader/1.epub"); Toast.makeText(MainActivity.this,filepath,Toast.LENGTH_SHORT).show(); } }); } }http://www.boreader.com
源码地址:https://github.com/zangketb/folioReader-Tibetan