实例介绍
【实例截图】


【核心代码】
package com.example.xch.tbsfilebrowsing;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DownloadManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.tencent.smtt.sdk.TbsReaderView;
import com.tencent.smtt.sdk.TbsReaderView.ReaderCallback;
import java.io.File;
import java.text.DecimalFormat;
public class FileDisplayActivity extends Activity implements ReaderCallback,
OnClickListener {
private TextView tv_title;
private TbsReaderView mTbsReaderView;
private TextView tv_download;
private RelativeLayout rl_tbsView; //rl_tbsView为装载TbsReaderView的视图
private ProgressBar progressBar_download;
private DownloadManager mDownloadManager;
private long mRequestId;
private DownloadObserver mDownloadObserver;
private String mFileUrl = "", mFileName, fileName;//文件url 由文件url截取的文件名 上个页面传过来用于显示的文件名
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_file_display);
findViewById();
getFileUrlByIntent();
mTbsReaderView = new TbsReaderView(this, this);
rl_tbsView.addView(mTbsReaderView, new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
if ((mFileUrl == null) || (mFileUrl.length() <= 0)) {
Toast.makeText(FileDisplayActivity.this, "获取文件url出错了",
Toast.LENGTH_SHORT).show();
return;
}
mFileName = parseName(mFileUrl);
if (isLocalExist()) {
tv_download.setText("打开文件");
tv_download.setVisibility(View.GONE);
displayFile();
} else {
if (!mFileUrl.contains("http")) {
new AlertDialog.Builder(FileDisplayActivity.this)
.setTitle("温馨提示:")
.setMessage("文件的url地址不合法哟,无法进行下载")
.setCancelable(false)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
return;
}
}).create().show();
}
startDownload();
}
}
/**
* 将url进行encode,解决部分手机无法下载含有中文url的文件的问题(如OPPO R9)
*
* @param url
* @return
* @author xch
*/
private String toUtf8String(String url) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < url.length(); i ) {
char c = url.charAt(i);
if (c >= 0 && c <= 255) {
sb.append(c);
} else {
byte[] b;
try {
b = String.valueOf(c).getBytes("utf-8");
} catch (Exception ex) {
System.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j ) {
int k = b[j];
if (k < 0)
k = 256;
sb.append("%" Integer.toHexString(k).toUpperCase());
}
}
}
return sb.toString();
}
private void findViewById() {
tv_download = (TextView) findViewById(R.id.tv_download);
ImageView back_icon = (ImageView) findViewById(R.id.back_icon);
tv_title = (TextView) findViewById(R.id.title);
progressBar_download = (ProgressBar) findViewById(R.id.progressBar_download);
rl_tbsView = (RelativeLayout) findViewById(R.id.rl_tbsView);
back_icon.setOnClickListener(this);
}
/**
* 获取传过来的文件url和文件名
*/
private void getFileUrlByIntent() {
Intent intent = getIntent();
mFileUrl = intent.getStringExtra("fileUrl");
fileName = intent.getStringExtra("fileName");
tv_title.setText(fileName);
}
/**
* 跳转页面
*
* @param context
* @param fileUrl 文件url
* @param fileName 文件名
*/
public static void actionStart(Context context, String fileUrl, String fileName) {
Intent intent = new Intent(context, FileDisplayActivity.class);
intent.putExtra("fileUrl", fileUrl);
intent.putExtra("fileName", fileName);
context.startActivity(intent);
}
/**
* 加载显示文件内容
*/
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);
} else {
File file = new File(getLocalFile().getPath());
if (file.exists()) {
Intent openintent = new Intent();
openintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String type = getMIMEType(file);
// 设置intent的data和Type属性。
openintent.setDataAndType(/* uri */Uri.fromFile(file), type);
// 跳转
startActivity(openintent);
finish();
}
}
}
private String getMIMEType(File file) {
String type = "*/*";
String fName = file.getName();
// 获取后缀名前的分隔符"."在fName中的位置。
int dotIndex = fName.lastIndexOf(".");
if (dotIndex < 0) {
return type;
}
/* 获取文件的后缀名 */
String end = fName.substring(dotIndex, fName.length()).toLowerCase();
if (end == "")
return type;
// 在MIME和文件类型的匹配表中找到对应的MIME类型。
for (int i = 0; i < MIME_MapTable.length; i ) {
if (end.equals(MIME_MapTable[i][0]))
type = MIME_MapTable[i][1];
}
return type;
}
private final String[][] MIME_MapTable = {
// {后缀名,MIME类型}
{".3gp", "video/3gpp"},
{".apk", "application/vnd.android.package-archive"},
{".asf", "video/x-ms-asf"},
{".avi", "video/x-msvideo"},
{".bin", "application/octet-stream"},
{".bmp", "image/bmp"},
{".c", "text/plain"},
{".class", "application/octet-stream"},
{".conf", "text/plain"},
{".cpp", "text/plain"},
{".doc", "application/msword"},
{".docx",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
{".xls", "application/vnd.ms-excel"},
{".xlsx",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
{".exe", "application/octet-stream"},
{".gif", "image/gif"},
{".gtar", "application/x-gtar"},
{".gz", "application/x-gzip"},
{".h", "text/plain"},
{".htm", "text/html"},
{".html", "text/html"},
{".jar", "application/java-archive"},
{".java", "text/plain"},
{".jpeg", "image/jpeg"},
{".jpg", "image/jpeg"},
{".js", "application/x-javascript"},
{".log", "text/plain"},
{".m3u", "audio/x-mpegurl"},
{".m4a", "audio/mp4a-latm"},
{".m4b", "audio/mp4a-latm"},
{".m4p", "audio/mp4a-latm"},
{".m4u", "video/vnd.mpegurl"},
{".m4v", "video/x-m4v"},
{".mov", "video/quicktime"},
{".mp2", "audio/x-mpeg"},
{".mp3", "audio/x-mpeg"},
{".mp4", "video/mp4"},
{".mpc", "application/vnd.mpohun.certificate"},
{".mpe", "video/mpeg"},
{".mpeg", "video/mpeg"},
{".mpg", "video/mpeg"},
{".mpg4", "video/mp4"},
{".mpga", "audio/mpeg"},
{".msg", "application/vnd.ms-outlook"},
{".ogg", "audio/ogg"},
{".pdf", "application/pdf"},
{".png", "image/png"},
{".pps", "application/vnd.ms-powerpoint"},
{".ppt", "application/vnd.ms-powerpoint"},
{".pptx",
"application/vnd.openxmlformats-officedocument.presentationml.presentation"},
{".prop", "text/plain"}, {".rc", "text/plain"},
{".rmvb", "audio/x-pn-realaudio"}, {".rtf", "application/rtf"},
{".sh", "text/plain"}, {".tar", "application/x-tar"},
{".tgz", "application/x-compressed"}, {".txt", "text/plain"},
{".wav", "audio/x-wav"}, {".wma", "audio/x-ms-wma"},
{".wmv", "audio/x-ms-wmv"},
{".wps", "application/vnd.ms-works"}, {".xml", "text/plain"},
{".z", "application/x-compress"},
{".zip", "application/x-zip-compressed"}, {"", "*/*"}};
private String parseFormat(String fileName) {
return fileName.substring(fileName.lastIndexOf(".") 1);
}
/**
* 利用文件url转换出文件名
*
* @param url
* @return
*/
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);
}
/**
* 下载文件
*/
@SuppressLint("NewApi")
private void startDownload() {
mDownloadObserver = new DownloadObserver(new Handler());
getContentResolver().registerContentObserver(
Uri.parse("content://downloads/my_downloads"), true,
mDownloadObserver);
mDownloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
//将含有中文的url进行encode
String fileUrl = toUtf8String(mFileUrl);
try {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(fileUrl));
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, mFileName);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
mRequestId = mDownloadManager.enqueue(request);
} catch (Exception e) {
e.printStackTrace();
}
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
private void queryDownloadStatus() {
DownloadManager.Query query = new DownloadManager.Query()
.setFilterById(mRequestId);
Cursor cursor = null;
try {
cursor = mDownloadManager.query(query);
if (cursor != null && cursor.moveToFirst()) {
// 已经下载的字节数
long currentBytes = cursor
.getLong(cursor
.getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
// 总需下载的字节数
long totalBytes = cursor
.getLong(cursor
.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
// 状态所在的列索引
int status = cursor.getInt(cursor
.getColumnIndex(DownloadManager.COLUMN_STATUS));
tv_download.setText("下载中...(" formatKMGByBytes(currentBytes)
"/" formatKMGByBytes(totalBytes) ")");
// 将当前下载的字节数转化为进度位置
int progress = (int) ((currentBytes * 1.0) / totalBytes * 100);
progressBar_download.setProgress(progress);
Log.i("downloadUpdate: ", currentBytes " " totalBytes " "
status " " progress);
if (DownloadManager.STATUS_SUCCESSFUL == status
&& tv_download.getVisibility() == View.VISIBLE) {
tv_download.setVisibility(View.GONE);
tv_download.performClick();
if (isLocalExist()) {
tv_download.setVisibility(View.GONE);
displayFile();
}
}
}
} 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);
}
}
@SuppressLint("Override")
public void onPointerCaptureChanged(boolean hasCapture) {
}
private class DownloadObserver extends ContentObserver {
private DownloadObserver(Handler handler) {
super(handler);
}
@Override
public void onChange(boolean selfChange, Uri uri) {
queryDownloadStatus();
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.back_icon:
finish();
break;
default:
break;
}
}
/**
* 将字节数转换为KB、MB、GB
*
* @param size 字节大小
* @return
*/
private String formatKMGByBytes(long size) {
StringBuffer bytes = new StringBuffer();
DecimalFormat format = new DecimalFormat("###.00");
if (size >= 1024 * 1024 * 1024) {
double i = (size / (1024.0 * 1024.0 * 1024.0));
bytes.append(format.format(i)).append("GB");
} else if (size >= 1024 * 1024) {
double i = (size / (1024.0 * 1024.0));
bytes.append(format.format(i)).append("MB");
} else if (size >= 1024) {
double i = (size / (1024.0));
bytes.append(format.format(i)).append("KB");
} else if (size < 1024) {
if (size <= 0) {
bytes.append("0B");
} else {
bytes.append((int) size).append("B");
}
}
return bytes.toString();
}
}
标签:
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论