关于应用打开word、pdf

来源:互联网 发布:男生发型软件app 编辑:程序博客网 时间:2024/04/29 08:18
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文档




0 0
原创粉丝点击