本篇文章為大家展示了PDF、Doc與Dwg格式的文件怎么在Android 應用中打開,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

成都創(chuàng)新互聯(lián)專注于牟平企業(yè)網(wǎng)站建設,響應式網(wǎng)站設計,商城系統(tǒng)網(wǎng)站開發(fā)。牟平網(wǎng)站建設公司,為牟平等地區(qū)提供建站服務。全流程按需定制開發(fā),專業(yè)設計,全程項目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務
代碼:
這是一個單獨的類 首先接收intent傳過來的url我是用url的后14位作為存儲本地的文件名(這里根據(jù)自己服務器的文件命名規(guī)則而定) 拿到文件路徑之后 判斷本地是否有此文件 有則打開沒有則從服務器上下載并打開 ;
Intent intent = act.getIntent();
final String Strname = intent.getStringExtra("docurl");
//截取最后14位 作為文件名
String s = Strname.substring(Strname.length()-14);
//文件存儲
file1 = new File(Environment.getExternalStorageDirectory(), getFileName(s));
new Thread() {
public void run() {
File file = new File( file1.getAbsolutePath());
//判斷是否有此文件
if (file.exists()) {
//有緩存文件,拿到路徑 直接打開
Message msg = Message.obtain();
msg.obj = haha;
msg.what = DOWNLOAD_SUCCESS;
handler.sendMessage(msg);
mProgressDialog.dismiss();
return;
}
// 本地沒有此文件 則從網(wǎng)上下載打開
File downloadfile = downLoad(Strname, file1.getAbsolutePath(), mProgressDialog);
// Log.i("Log",file1.getAbsolutePath());
Message msg = Message.obtain();
if (downloadfile != null) {
// 下載成功,安裝....
msg.obj = downloadfile;
msg.what = DOWNLOAD_SUCCESS;
} else {
// 提示用戶下載失敗.
msg.what = DOWNLOAD_ERROR;
}
handler.sendMessage(msg);
mProgressDialog.dismiss();
};
}.start();下載文檔代碼;
傳入需要下載的文檔的url 和存入內(nèi)存的路徑和dialog
public static File downLoad(String serverpath, String savedfilepath, ProgressDialog pd) {
try {
URL url = new URL(serverpath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
if (conn.getResponseCode() == 200) {
int max = conn.getContentLength();
pd.setMax(max);
InputStream is = conn.getInputStream();
File file = new File(savedfilepath);
FileOutputStream fos = new FileOutputStream(file);
int len = 0;
byte[] buffer = new byte[1024];
int total = 0;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
total += len;
pd.setProgress(total);
}
fos.flush();
fos.close();
is.close();
return file;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
}打開文件選擇器
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case DOWNLOAD_SUCCESS:
File file = (File) msg.obj;
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType (Uri.fromFile(file), "application/pdf");
// startActivity(intent);
startActivity(Intent.createChooser(intent, "標題"));
/**
* 彈出選擇框之后 把本activity銷毀
*/
finish();
break;
case DOWNLOAD_ERROR:
Util.showToast(act,"文件加載失敗");
break;
}
}
};整體代碼
public class list_item_doc extends BaseActivity {
private ProgressDialog mProgressDialog;
// 下載失敗
public static final int DOWNLOAD_ERROR = 2;
// 下載成功
public static final int DOWNLOAD_SUCCESS = 1;
private File file1;
@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
initView();
}
private void initView() {
// TODO Auto-generated method stub
Intent intent = act.getIntent();
final String Strname = intent.getStringExtra("url");
mProgressDialog = new ProgressDialog(act);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
//截取最后14位 作為文件名
String s = Strname.substring(Strname.length()-14);
//文件存儲
file1 = new File(Environment.getExternalStorageDirectory(), getFileName(s));
new Thread() {
public void run() {
File haha = new File( file1.getAbsolutePath());
//判斷是否有此文件
if (haha.exists()) {
//有緩存文件,拿到路徑 直接打開
Message msg = Message.obtain();
msg.obj = haha;
msg.what = DOWNLOAD_SUCCESS;
handler.sendMessage(msg);
mProgressDialog.dismiss();
return;
}
// 本地沒有此文件 則從網(wǎng)上下載打開
File downloadfile = downLoad(Strname, file1.getAbsolutePath(), mProgressDialog);
// Log.i("Log",file1.getAbsolutePath());
Message msg = Message.obtain();
if (downloadfile != null) {
// 下載成功,安裝....
msg.obj = downloadfile;
msg.what = DOWNLOAD_SUCCESS;
} else {
// 提示用戶下載失敗.
msg.what = DOWNLOAD_ERROR;
}
handler.sendMessage(msg);
mProgressDialog.dismiss();
};
}.start();
}
/**
* 下載完成后 直接打開文件
*/
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case DOWNLOAD_SUCCESS:
File file = (File) msg.obj;
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType (Uri.fromFile(file), "application/pdf");
// startActivity(intent);
startActivity(Intent.createChooser(intent, "標題"));
/**
* 彈出選擇框 把本activity銷毀
*/
finish();
break;
case DOWNLOAD_ERROR:
Util.showToast(act,"文件加載失敗");
break;
}
}
};
/**
*
*/
/**
* 傳入文件 url 文件路徑 和 彈出的dialog 進行 下載文檔
*/
public static File downLoad(String serverpath, String savedfilepath, ProgressDialog pd) {
try {
URL url = new URL(serverpath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
if (conn.getResponseCode() == 200) {
int max = conn.getContentLength();
pd.setMax(max);
InputStream is = conn.getInputStream();
File file = new File(savedfilepath);
FileOutputStream fos = new FileOutputStream(file);
int len = 0;
byte[] buffer = new byte[1024];
int total = 0;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
total += len;
pd.setProgress(total);
}
fos.flush();
fos.close();
is.close();
return file;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String getFileName(String serverurl) {
return serverurl.substring(serverurl.lastIndexOf("/") + 1);
}
}上述內(nèi)容就是PDF、Doc與Dwg格式的文件怎么在Android 應用中打開,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文題目:PDF、Doc與Dwg格式的文件怎么在Android應用中打開
轉(zhuǎn)載源于:http://chinadenli.net/article44/ipcjhe.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、網(wǎng)站制作、靜態(tài)網(wǎng)站、云服務器、虛擬主機、動態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)