筆記-Android中開啟各種格式的檔案(apk、word、excel、ppt、pdf、音視頻、圖片等)

來源:互聯網
上載者:User

標籤:android   apk   開啟檔案   intent   

開啟尾碼.apk的檔案,即啟動安裝程式;

 //apkFilePath 檔案路徑public void installAPK(String apkFilePath) {           // 建立URI        Uri uri = Uri.fromFile(new File(apkFilePath));        Intent intent = new Intent(Intent.ACTION_VIEW);        // 設定Uri和類型        intent.setDataAndType(uri, "application/vnd.android.package-archive");        // 執行安裝        mContext.startActivity(intent);    }   
/** * 開啟多種類型檔案 * @param path  檔案路徑 * @param type  檔案類型 */public void openText(String path , int type){    Intent intent = new Intent(Intent.ACTION_VIEW);             intent.addCategory(Intent.CATEGORY_DEFAULT);             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);             Uri uri = Uri.fromFile(new File(path ));         //判斷檔案類型        if (FILE_TYPE_PPT == type) {                 intent.setDataAndType(uri, "application/vnd.ms-powerpoint");          } else if (FILE_TYPE_WORD == type) {            intent.setDataAndType(uri, "application/msword");             } else if(FILE_TYPE_EXCEL == type){            intent.setDataAndType(uri, "application/vnd.ms-excel");             } else if(FILE_TYPE_TXT == type){            intent.setDataAndType(uri, "text/plain");             } else if(FILE_TYPE_PDF == type){            intent.setDataAndType(uri, "application/pdf");             } else if(FILE_TYPE_HTML == type){            Uri htmluri = Uri.parse(path).buildUpon().encodedAuthority("com.android.htmlfileprovider")                    .scheme("content").encodedPath(path).build();              intent.setDataAndType(htmluri, "text/html");         }          try {        activity.startActivity(intent);        } catch (Exception e) {            Toast.makeText(mContext, "裝置中沒有安裝支援該格式的程式", Toast.LENGTH_SHORT).show();        }}
<pre name="code" class="java">////////開啟多媒體類型intent.setDataAndType(uri, "audio/*");  //音頻intent.setDataAndType(uri, "video/*");  //視頻intent.setDataAndType(uri, "image/*");  //圖片intent.setDataAndType(uri, "application/x-chm");     //開啟chm檔案


////判斷檔案名稱是否是某種類型的尾碼private boolean check(final String name, final String[] extensions) {for (String end : extensions) {if (name.toLowerCase().endsWith(end)) {return true;}}return false;}/////////////設定類型   if (check(name, ".apk")){     file.setType(FILE_TYPE_APK);    } else  if(check(name, ".pdf")){         file.setType(FILE_TYPE_PDF);    } else if(check(name,             getStringArray(R.array.ppt_filter))){         file.setType(FILE_TYPE_PPT);    } ...................array.ppt_filter:<array name="ppt_filter">        <item>.ppt</item>        <item>.pptx</item>    </array>


//擷取裝置儲存路徑(sd卡,usb)
Environment.getExternalStorageDirectory();

Environment.getExternalStorageDirectory().getParent();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.