在app內(nèi)部實現(xiàn)文檔的預覽效果
通過騰訊的TbsReaderView控件實現(xiàn)文檔預覽功能威创,參考來源:http://www.reibang.com/p/84784cf428c9
一、下載騰訊X5內(nèi)核SDK
二晚缩、項目中添加jar包和.so文
1墅垮、添加jar包
2、添加.so文件
三、清單文件中添加權(quán)限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION"/>
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
四捂寿、app內(nèi)顯示文檔
該插件只支持展示本地文檔,不支持在線預覽功能孵运,所以秦陋,要先下載文檔保存的本地,然后打開文檔
public class OpenReadOffice extends Activity implements TbsReaderView.ReaderCallback {
private TbsReaderView mTbsReaderView;
private Button mDownloadBtn;
private DownloadManager mDownloadManager;
private long mRequestId;
private DownloadObserver mDownloadObserver;
private String mFileUrl="http://www.hrssgz.gov.cn/bgxz/sydwrybgxz/201101/P020110110748901718161.doc";
private String mFileName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.read_office);
mTbsReaderView = new TbsReaderView(OfficeReader.this, this);
mDownloadBtn = (Button) findViewById(R.id.btn_download);
LinearLayout rootRl = (LinearLayout) findViewById(R.id.read_office_tbs);
rootRl.addView(mTbsReaderView, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
mFileName = parseName(mFileUrl);
if (isLocalExist()) {
mDownloadBtn.setVisibility(View.GONE);
displayFile();
} else {
startDownload();
}
}
public void onClickDownload(View v) {
if (isLocalExist()) {
mDownloadBtn.setVisibility(View.GONE);
displayFile();
} else {
startDownload();
}
}
private void displayFile() {
Bundle bundle = new Bundle();
bundle.putString("filePath", getLocalFile().getPath());
bundle.putString("tempPath", Environment.getExternalStorageDirectory().getPath());
boolean result = mTbsReaderView.preOpen(parseFormat(mFileName), false);
if (result) {
mTbsReaderView.openFile(bundle);
}
}
private String parseFormat(String fileName) {
return fileName.substring(fileName.lastIndexOf(".") + 1);
}
private String parseName(String url) {
String fileName = null;
try {
fileName = url.substring(url.lastIndexOf("/") + 1);
} finally {
if (TextUtils.isEmpty(fileName)) {
fileName = String.valueOf(System.currentTimeMillis());
}
}
return fileName;
}
private boolean isLocalExist() {
return getLocalFile().exists();
}
private File getLocalFile() {
return new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), mFileName);
}
private void startDownload() {
Toast.makeText(this, "開始下載文件", Toast.LENGTH_SHORT).show();
RxPermissions.getInstance(this)
.request(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
.subscribe(new Action1<Boolean>() {
@Override
public void call(Boolean aBoolean) {
if (aBoolean) {
mDownloadObserver = new DownloadObserver(new Handler());
getContentResolver().registerContentObserver(Uri.parse("content://downloads/my_downloads"), true, mDownloadObserver);
mDownloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
//mFileUrl = Uri.encode(mFileUrl, "-![.:/,%?&=]");
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(mFileUrl));
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, mFileName);
request.allowScanningByMediaScanner();
// request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
mRequestId = mDownloadManager.enqueue(request);
} else {
//不同意
Toast.makeText(OfficeReader.this, "未授予讀寫權(quán)限治笨,無法查看", Toast.LENGTH_SHORT).show();
}
}
});
}
private void queryDownloadStatus() {
DownloadManager.Query query = new DownloadManager.Query().setFilterById(mRequestId);
Cursor cursor = null;
try {
cursor = mDownloadManager.query(query);
if (cursor != null && cursor.moveToFirst()) {
//已經(jīng)下載的字節(jié)數(shù)
int currentBytes = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
//總需下載的字節(jié)數(shù)
int totalBytes = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
//狀態(tài)所在的列索引
int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
Log.i("downloadUpdate: ", currentBytes + " " + totalBytes + " " + status);
mDownloadBtn.setText("正在下載:" + currentBytes + "/" + totalBytes);
if (DownloadManager.STATUS_SUCCESSFUL == status && mDownloadBtn.getVisibility() == View.VISIBLE) {
mDownloadBtn.setVisibility(View.GONE);
mDownloadBtn.performClick();
}
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
@Override
public void onCallBackAction(Integer integer, Object o, Object o1) {
}
@Override
protected void onDestroy() {
super.onDestroy();
mTbsReaderView.onStop();
if (mDownloadObserver != null) {
getContentResolver().unregisterContentObserver(mDownloadObserver);
}
}
private class DownloadObserver extends ContentObserver {
private DownloadObserver(Handler handler) {
super(handler);
}
@Override
public void onChange(boolean selfChange, Uri uri) {
Log.i("downloadUpdate: ", "onChange(boolean selfChange, Uri uri)");
queryDownloadStatus();
}
}
·
}
五驳概、遇到的問題
1赤嚼、請求地址問題
通過DownloadManager進行網(wǎng)絡請求時,如果URL中含有中文顺又,需要進行URL轉(zhuǎn)碼
mFileUrl = Uri.encode(mFileUrl, "-![.:/,%?&=]");
2更卒、讀寫權(quán)限問題