版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】怎么在Android中实现文档浏览功能
怎么在Android中实现文档浏览功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。一、下载腾讯X5内核1.前往/下载Android的内核,新版本的腾讯X5可以直接在bulid.gradle集成api'com.tencent.tbs.tbssdk:sdk:43697',如果是在App里集成可以把api换成implementation2.AndroidStudio导入腾讯X5a.把下载好的jar包导入libs,然后runas,再把jnilibs导入main包下b.module的build.gradle添加cpu适配3.Application中腾讯X5初始化,在onCreate()方法调用init方法QbSdk.initX5Environment(this,
new
QbSdk.PreInitCallback()
{
@Override
public
void
onCoreInitFinished()
{
}
@Override
public
void
onViewInitFinished(boolean
b)
{
Log.e("xxx","hasLoad"+b);
//此处将内核加载是否成功的状态保存到本地,SharePreference工具类可换为自己的
SharedPreferenceUtils.saveBoolean(getApplicationContext(),"hasLoad",b);
}
});4.应用内调用,无论是否加载内核,都是把在线的文档下载到本地然后打开,不同的是,未加载内核会借助QQ浏览器或其他App的文件浏览功能类似微信(这个调用,腾讯X5已自动处理,我们无需关心),而加载内核后,使用X5的TbsReaderView来打开文件,接下来就是具体代码。a.bulid.gradle中集成Retrofit,RxJava和进度管理器ProgressManager
implementation
'io.reactivex:rxandroid:1.2.1'
implementation
'com.squareup.retrofit2:converter-gson:2.3.0'
implementation
'com.squareup.retrofit2:adapter-rxjava:2.1.0'
implementation
'com.squareup.retrofit2:retrofit:2.3.0'
implementation
'io.reactivex:rxjava:1.2.6'
implementation
'me.jessyan:progressmanager:1.5.0'写网络下载工具public
interface
LoadFileApi
{
@GET
Call<ResponseBody>
loadPdfFile(@Url
String
fileUrl);
}
public
class
LoadFileModel
{
//使用该方法来下载文件
public
static
void
loadPdfFile(String
url,
Callback<ResponseBody>
callback)
{
Retrofit
retrofit
=
new
Retrofit.Builder()
.baseUrl("/")
.client(ProgressManager.getInstance().with(new
OkHttpClient.Builder())
.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
LoadFileApi
mLoadFileApi
=
retrofit.create(LoadFileApi.class);
if
(!TextUtils.isEmpty(url))
{
Call<ResponseBody>
call
=
mLoadFileApi.loadPdfFile(url);
call.enqueue(callback);
}
}
}b.具体调用首先注册文件进度下载监听private
ProgressDialog
commonProgressDialog;//切换为自己的进度控件
private
ProgressInfo
mLastDownloadingInfo;
ProgressManager.getInstance().addResponseListener(url,
new
ProgressListener()
{
@Override
public
void
onProgress(ProgressInfo
progressInfo)
{
if
(mLastDownloadingInfo
==
null)
{
mLastDownloadingInfo
=
progressInfo;
}
//因为是以请求开始时的时间作为
Id
,所以值越大,说明该请求越新
if
(progressInfo.getId()
<
mLastDownloadingInfo.getId())
{
return;
}
else
if
(progressInfo.getId()
>
mLastDownloadingInfo.getId())
{
mLastDownloadingInfo
=
progressInfo;
}
int
progress
=
mLastDownloadingInfo.getPercent();
commonProgressDialog.setProgress(progress);
Log.d("xxx",
"--Download--
"
+
progress
+
"
%
"
+
mLastDownloadingInfo.getSpeed()
+
"
byte/s
"
+
mLastDownloadingInfo.toString());
if
(mLastDownloadingInfo.isFinish())
{
//说明已经下载完成
commonProgressDialog.dismiss();
Log.d("xxx",
"--Download--
finish");
}
}
@Override
public
void
onError(long
id,
Exception
e)
{
e.printStackTrace();
new
Handler().post(new
Runnable()
{
@Override
public
void
run()
{
commonProgressDialog.dismiss();
}
});
}
});
}然后打开文件private
static
final
String
DOWN_DIR
=
Environment.getExternalStorageDirectory()
+
File.separator+"Download";
//文件下载的路径,可以自定义
//打开文件
private
void
openFile(String
filePath,String
fileName)
{
boolean
hasLoad
=
SharedPreferenceUtils.getBoolean(mContext,
"hasLoad");//是否记载内核
if
(hasLoad){
//展示文件的Activity
Intent
intentDoc
=
new
Intent(this,FileDisplayActivity.class);
intentDoc.putExtra("path",filePath);
intentDoc.putExtra("fileName",fileName);
startActivity(intentDoc);
}else
{
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(this,"当前SD不可用",Toast.LENGTH_LONG).show();
return;
}
try
{
File
file
=
new
File(DOWN_DIR,
fileName);
if
(file.exists()){
QbSdk.openFileReader(this,
file.getAbsolutePath(),
null,
new
ValueCallback<String>()
{
@Override
public
void
onReceiveValue(String
s)
{
Log.e("xxx",s);
}
});
}
else
{
downLoadFile(filePath,fileName);
}
}
catch
(Exception
e)
{
e.printStackTrace();
ToastUtils.showShortToast(mContext,
"已下载过了");
}
}
}
private
void
downLoadFile(final
String
url,
final
String
fileName)
{
commonProgressDialog.show();
LoadFileModel.loadPdfFile(url,
new
Callback<ResponseBody>()
{
@Override
public
void
onResponse(@NonNull
Call<ResponseBody>
call,
@NonNull
Response<ResponseBody>
response)
{
InputStream
is
=
null;
byte[]
buf
=
new
byte[2048];
int
len;
FileOutputStream
fos
=
null;
try
{
ResponseBody
responseBody
=
response.body();
if
(responseBody
!=
null)
{
is
=
responseBody.byteStream();
final
File
file
=
new
File(DOWN_DIR,
fileName);
fos
=
new
FileOutputStream(file);
while
((len
=
is.read(buf))
!=
-1)
{
fos.write(buf,
0,
len);
}
fos.flush();
//未加载内核调用方法
QbSdk.openFileReader(mContext,
file.getAbsolutePath(),
null,
new
ValueCallback<String>()
{
@Override
public
void
onReceiveValue(String
s)
{
Log.e("xxx",s);
}
});
ToastUtils.showLongToast(MeetingDetailActivity.this,
"下载成功,保存在Download文件夹下");
}
}
catch
(Exception
e)
{
commonProgressDialog.dismiss();
ProgressManager.getInstance().notifyOnErorr(url,e);
ToastUtils.showLongToast(MeetingDetailActivity.this,
"下载失败");
}
finally
{
try
{
if
(is
!=
null)
is.close();
}
catch
(IOException
e)
{
e.printStackTrace();
}
try
{
if
(fos
!=
null)
fos.close();
}
catch
(IOException
e)
{
e.printStackTrace();
}
}
}
@Override
public
void
onFailure(@NonNull
Call<ResponseBody>
call,
@NonNull
Throwable
t)
{
t.printStackTrace();
ToastUtils.showShortToast(MeetingDetailActivity.this,
"下载失败");
commonProgressDialog.dismiss();
}
});
}FileDisplayActivity代码,对于一些包名可以切换为自己的包名,还有一些第三方库像进度展示,也可以切换为自己的进度控件import
android.content.Intent;
import
android.os.Bundle;
import
android.os.Environment;
import
android.os.Handler;
import
android.os.Message;
import
android.support.annotation.NonNull;
import
android.support.annotation.Nullable;
import
android.support.v7.app.AppCompatActivity;
import
android.text.TextUtils;
import
android.util.Log;
import
android.view.View;
import
android.view.WindowManager;
import
android.widget.TextView;
import
com.anshi.oatencentschool.R;
import
com.anshi.oatencentschool.utils.DialogBuild;
import
com.anshi.oatencentschool.utils.StatusBarUtils;
import
com.anshi.oatencentschool.utils.ToastUtils;
import
com.anshi.oatencentschool.utils.WeakHandler;
import
com.anshi.oatencentschool.utils.filetool.LoadFileModel;
import
com.anshi.oatencentschool.utils.filetool.SuperFileView2;
import
com.kaopiz.kprogresshud.KProgressHUD;
import
java.io.File;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
gressmanager.ProgressListener;
import
gressmanager.ProgressManager;
import
gressmanager.body.ProgressInfo;
import
okhttp3.ResponseBody;
import
retrofit2.Call;
import
retrofit2.Callback;
import
retrofit2.Response;
public
class
FileDisplayActivity
extends
AppCompatActivity
{
private
String
TAG
=
"FileDisplayActivity";
//Tencent
提供的TBS阅读浏览功能,不借助第三方软件打开office和pdf文件
private
SuperFileView2
mSuperFileView;
private
String
filePath;
private
TextView
mTextView;
private
static
final
String
DOWN_DIR
=
Environment.getExternalStorageDirectory()
+
File.separator+"Download";
private
String
fileName;
private
KProgressHUD
commonProgressDialog;
private
WeakHandler
weakHandler
=
new
WeakHandler(new
Handler.Callback()
{
@Override
public
boolean
handleMessage(Message
msg)
{
int
progress
=
msg.arg1;
if
(progress==100){
commonProgressDialog.setProgress(progress);
commonProgressDialog.dismiss();
}else
{
commonProgressDialog.setProgress(progress);
}
return
true;
}
});
@Override
protected
void
onCreate(@Nullable
Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_display);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|WindowManager.LayoutParams.FLAG_FULLSCREEN);
commonProgressDialog
=
DialogBuild.getBuild().createCommonProgressDialog(this,
"下载中");
init();
}
/**
*
初始化
*/
public
void
init()
{
mTextView
=
(TextView)
findViewById(R.id.file_album_name);
fileName
=
getIntent().getStringExtra("fileName");
int
lastIndexOf
=
fileName.lastIndexOf(".");
String
substring
=
fileName.substring(0,
lastIndexOf);
mTextView.setText(substring);
mSuperFileView
=
(SuperFileView2)
findViewById(R.id.mSuperFileView);
mSuperFileView.setOnGetFilePathListener(new
SuperFileView2.OnGetFilePathListener()
{
@Override
public
void
onGetFilePath(SuperFileView2
mSuperFileView2)
{
getFilePathAndShowFile(mSuperFileView);
}
});
Intent
intent
=
this.getIntent();
String
path
=
(String)
intent.getSerializableExtra("path");
if
(!TextUtils.isEmpty(path))
{
Log.d(TAG,
"文件path:"
+
path);
setFilePath(path);
}
mSuperFileView.show();
ProgressManager.getInstance().addResponseListener(path,
new
ProgressListener()
{
@Override
public
void
onProgress(ProgressInfo
progressInfo)
{
int
percent
=
progressInfo.getPercent();
Message
obtain
=
Message.obtain();
obtain.arg1
=
percent;
weakHandler.sendMessage(obtain);
}
@Override
public
void
onError(long
id,
Exception
e)
{
}
});
}
@Override
protected
void
onResume()
{
super.onResume();
StatusBarUtils.setWindowStatusBarColor(this,R.color.color_main);
}
/**
*
显示文件
*
@param
mSuperFileView2
控件
*/
private
void
getFilePathAndShowFile(SuperFileView2
mSuperFileView2)
{
if
(getFilePath().contains("http")&&!new
File(DOWN_DIR,
fileName).exists())
{//网络地址要先下载
downLoadFile(getFilePath(),fileName,mSuperFileView2);
}
else
{
try
{
mSuperFileView2.displayFile(new
File(DOWN_DIR,
fileName));
}catch
(Exception
e){
e.printStackTrace();
}
}
}
public
void
setFilePath(String
fileUrl)
{
this.filePath
=
fileUrl;
}
private
String
getFilePath()
{
return
filePath;
}
/**
*
回退上一级菜单
*
@param
view
控件
*/
public
void
onClick(View
view)
{
finish();
}
private
void
downLoadFile(final
String
url,
final
String
fileName,final
SuperFileView2
mSuperFileView2)
{
commonProgressDialog.show();
LoadFileModel.loadPdfFile(url,
new
Callback<ResponseBody>()
{
@Override
public
void
onResponse(@NonNull
Call<ResponseBody>
call,
@NonNull
Response<ResponseBody>
response)
{
InputStream
is
=
null;
byte[]
buf
=
new
byte[2048];
int
len;
FileOutputStream
fos
=
null;
try
{
ResponseBody
responseBody
=
response.body();
is
=
responseBody.byteStream();
final
File
file
=
new
File(DOWN_DIR,
fileName);
fos
=
new
FileOutputStream(file);
while
((len
=
is.read(buf))
!=
-1)
{
fos.write(buf,
0,
len);
}
fos.flush();
mSuperFileView2.displayFile(file);
ToastUtils.showLongToast(FileDisplayActivity.this,
"下载成功,保存在Download文件夹下");
}
catch
(Exception
e)
{
commonProgressDialog.dismiss();
ToastUtils.showLongToast(FileDisplayActivity.this,
"下载失败");
}
finally
{
try
{
if
(is
!=
null)
is.close();
}
catch
(IOException
e)
{
e.printStackTrace();
}
try
{
if
(fos
!=
null)
fos.close();
}
catch
(IOException
e)
{
e.printStackTrace();
}
}
}
@Override
public
void
onFailure(@NonNull
Call<ResponseBody>
call,
@NonNull
Throwable
t)
{
t.printStackTrace();
ToastUtils.showShortToast(FileDisplayActivity.this,
"下载失败");
commonProgressDialog.dismiss();
}
});
}
}布局Layout:<?xml
version="1.0"
encoding="utf-8"?>
<RelativeLayout
xmlns:android="/apk/res/android"
android:id="@+id/activity_file_display"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="46dp"
android:background="@color/color_main"
android:id="@+id/file_toolbar"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/file_album_name"
android:layout_gravity="center"
android:gravity="center"
android:maxLines="2"
android:ellipsize="end"
android:textSize="14sp"
android:textColor="#FFF"
/>
</android.support.v7.widget.Toolbar>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/file_toolbar"
>
<com.anshi.oatencentschool.utils.filetool.SuperFileView2
android:id="@+id/mSuperFileView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_gravity="center|start"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:onClick="onClick"
android:id="@+id/back_icon"
android:drawableStart="@drawable/svg_left_back_blur"
android:layout_marginStart="10dp"
/>
</RelativeLayout>
<!--<RadioButton-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:button="@null"-->
<!--android:visibility="gone"-->
<!--android:layout_gravity="end|bottom"-->
<!--android:onClick="saveLocal"-->
<!--android:layout_marginBottom="10dp"-->
<!--android:layout_marginEnd="10dp"-->
<!--android:drawableStart="@drawable/vector_drawable_down_load"-->
<!--android:layout_marginStart="10dp"-->
<!--/>-->
</FrameLayout>
</RelativeLayout>SuperFileView2代码,这个自定义的类是从/ZhongXiaoHong/superFileView导入import
android.content.Context;
import
android.os.Bundle;
import
android.os.Environment;
import
android.text.TextUtils;
import
android.util.AttributeSet;
import
android.util.Log;
import
android.widget.FrameLayout;
import
android.widget.LinearLayout;
import
com.tencent.smtt.sdk.TbsReaderView;
import
java.io.File;
/**
*
*
Created
by
12457
on
2017/8/29.
*/
public
class
SuperFileView2
extends
FrameLayout
{
private
static
String
TAG
=
"SuperFileView";
private
TbsReaderView
mTbsReaderView;
private
Context
context;
public
SuperFileView2(Context
context)
{
this(context,
null,
0);
}
public
SuperFileView2(Context
context,
AttributeSet
attrs)
{
this(context,
attrs,
0);
}
public
SuperFileView2(Context
context,
AttributeSet
attrs,
int
defStyleAttr)
{
super(context,
attrs,
defStyleAttr);
mTbsReaderView
=
new
TbsReaderView(context,
new
TbsReaderView.ReaderCallback()
{
@Override
public
void
onCallBackAction(Integer
integer,
Object
o,
Object
o1)
{
}
});
this.addView(mTbsReaderView,
new
LinearLayout.LayoutParams(-1,
-1));
this.context
=
context;
}
private
OnGetFilePathListener
mOnGetFilePathListener;
public
void
setOnGetFilePathListener(OnGetFilePathListener
mOnGetFilePathListener)
{
this.mOnGetFilePathListener
=
mOnGetFilePathListener;
}
private
TbsReaderView
getTbsReaderView(Context
context)
{
return
new
TbsReaderView(context,
new
TbsReaderView.ReaderCallback()
{
@Override
public
void
onCallBackAction(Integer
integer,
Object
o,
Object
o1)
{
}
});
}
public
void
displayFile(File
mFile)
{
if
(mFile
!=
null
&&
!TextUtils.isEmpty(mFile.toString()))
{
//增加下面一句解决没有TbsReaderTemp文件夹存在导致加载文件失败
String
bsReaderTemp
=
Environment.getExternalStorageDirectory()+
File.separator+"TbsReaderTemp";
File
bsReaderTempFile
=new
File(bsReaderTemp);
if
(!bsReaderTempFile.exists())
{
Log.d("xxx","准备创建/storage/emulated/0/TbsReaderTemp!!");
boolean
mkdir
=
bsReaderTempFi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 卧式螺旋离心脱水机相关项目投资计划书范本
- 区块链技术在资产管理中的作用
- 《地球的内部作业设计方案-2023-2024学年小学科学湘科版》
- 《电池导学案-2023-2024学年科学湘科版2001》
- OLED寿命检测系统相关行业投资规划报告
- 《我的语文生活作业设计方案-2023-2024学年初中语文统编版》
- 2024届四川省长宁县中考三模化学试题含解析
- 2024届山东省泰安市、新泰市中考一模化学试题含解析
- 辽宁省辽阳市辽阳县重点中学2024届中考试题猜想化学试卷含解析
- 福建省龙岩新罗区重点中学2024届中考化学猜题卷含解析
- 加油站项目监理要点
- 旅游景区服务与管理智慧树知到课后章节答案2023年下青岛酒店管理职业技术学院
- 搅拌站承包合同
- 中国汽车物流与供应链管理研究
- 人才测评技术-无领导小组讨论-马欣川
- 大跨度钢结构厂房施工组织设计
- 第一单元 清洁与卫生 (教学设计)-三年级上册劳动苏教版
- 生活垃圾管理办法解读
- 课程思政示范课程申报书(成功申报)
- 凿井井架施工方案
- 2023宏泰集团所属湖北省征信限公司招聘9人上岸笔试历年难、易错点考题附带参考答案与详解
评论
0/150
提交评论