activity_main.xml:
Java代碼 收藏代碼
<RelativeLayout 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"
>
<Button
android:id="@+id/btn_open_word"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="打開word" />
<Button
android:id="@+id/btn_open_pdf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_below="@+id/btn_open_word"
android:text="打開pdf" />
</RelativeLayout>
MainActivity.java:
Java代碼 收藏代碼
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private Button btnOpenWord;
private Button btnOpenPdf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnOpenWord = (Button)findViewById(R.id.btn_open_word);
btnOpenPdf = (Button)findViewById(R.id.btn_open_pdf);
btnOpenWord.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
File file = new File(getSDPath()+"/***.doc");//這裏更改爲你的名稱
Log.e(TAG, "file exsit:"+file.getPath());
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/msword");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(MainActivity.this,
"No Application Available to View WORD",
Toast.LENGTH_SHORT).show();
}
}
}
});
btnOpenPdf.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
File file = new File(getSDPath()+"/***.pdf");//這裏更改爲你的名稱
Log.e(TAG, "file exsit:"+file.exists());
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(MainActivity.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
}
});
}
/**
* 獲取sd卡路徑
*
* */
private String getSDPath(){
File sdDir = null;
boolean sdCardExist = android.os.Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());//判斷sd卡是否存在
if(sdCardExist){
sdDir = Environment.getExternalStorageDirectory();//獲取目錄
}
Log.e(TAG, "sdCard:"+sdDir.toString());
return sdDir.toString();
}
}
當然不要忘了添加相關讀取sd卡的權限否則打不開哦!
下面寫個例子吧在網絡上下載word文檔並放到sd卡里面在通過三方打開
下面是通過代碼android-async-http這個三方下載的蠻好用的等會我會上傳代碼不用積分裏面就有這個
private Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt=(Button) findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
btn();
}
});
}
private void btn() {
// 下載
FinalHttp finalHttp = new FinalHttp();
finalHttp
.download(
"http://119.37.192.234:9079/JDZJ/Resource/988202f2-5161-4bb5-a055-b89b328d620b.doc",
Environment.getExternalStorageDirectory().getPath()
+ "/TJCX/"+"asdf.docx", new AjaxCallBack<File>() {
@Override
public void onStart() {
super.onStart();
}
@Override
public void onLoading(long count, long current) {
System.out.println(current + "~~~");
super.onLoading(count, current);
}
@Override
public void onSuccess(File t) {
super.onSuccess(t);
}
@Override
public void onFailure(Throwable t, int errorNo,
String strMsg) {
super.onFailure(t, errorNo, strMsg);
}
});
}
通過這個方式下載下來的文件 finalHttp.download(String url,String path);這個就是用來下載的第一個參數是下載地址,第二個參數是下載存放在手機的位置
Environment.getExternalStorageDirectory().getPath()+ "/TJCX/"+"asdf.docx",這就是我存放的位置了
Environment.getExternalStorageDirectory().getPath()--------------------->手機根目錄
/TJCX/------------------------------------------------------------------------------------>根目錄下的TJCX文件夾下
asdf.docx--------------------------------------------------------------------------------->這個是給下載下來的文件命名以及後綴了
同樣按照上面的進行打開就可以了
打開word文檔
下載word文檔