2023年12月14日发(作者:)

笔记-Android中打开各种格式的文件(apk、word、excel、

ppt、pdf、音。。。

打开后缀.apk的文件,即启动安装程序;

[java]

01.

02.

03.

04.

05.

06.

07.

08.

09.

10.

//apkFilePath 文件路径

ublic void installAPK(String apkFilePath) {

// 创建URI

Uri uri = le(new File(apkFilePath));

Intent intent = new Intent(_VIEW);

// 设置Uri和类型

aAndType(uri, "application/vnd..package-archive");

// 执行安装

ctivity(intent);

}

[java]

01.

02.

03.

04.

05.

06.

07.

08.

09.

10.

11.

12.

13.

14.

15.

16.

17.

18.

19.

20.

21.

22.

23.

24.

25.

26.

27.

28.

29.

30.

31.

32.

/**

* 打开多种类型文件

* @param path 文件路径

* @param type 文件类型

*/

public void openText(String path , int type){

Intent intent = new Intent(_VIEW);

egory(RY_DEFAULT);

gs(_ACTIVITY_NEW_TASK);

Uri uri = le(new File(path ));

//判断文件类型

if (FILE_TYPE_PPT == type) {

aAndType(uri, "application/-powerpoint");

} else if (FILE_TYPE_WORD == type) {

aAndType(uri, "application/msword");

} else if(FILE_TYPE_EXCEL == type){

aAndType(uri, "application/-excel");

} else if(FILE_TYPE_TXT == type){

aAndType(uri, "text/plain");

} else if(FILE_TYPE_PDF == type){

aAndType(uri, "application/pdf");

} else if(FILE_TYPE_HTML == type){

Uri htmluri = (path).buildUpon().encodedAuthority("leprovider"

.scheme("content").encodedPath(path).build();

aAndType(htmluri, "text/html");

}

try {

ctivity(intent);

} catch (Exception e) {

xt(mContext, "设备中没有安装支持该格式的程

序", _SHORT).show();

}

}

[java]

01.

02.

03.

04.

05.

06.

    打开多媒体类型  

aAndType(uri, "audio/*"); //音频

aAndType(uri, "video/*"); //视频

aAndType(uri, "image/*"); //图片

aAndType(uri, "application/x-chm"); //打开chm文件 [java]

01.

02.

03.

04.

05.

06.

07.

08.

09.

10.

11.

12.

13.

14.

15.

16.

17.

18.

19.

20.

21.

22.

23.

24.

25.

判断文件名是否是某种类型的后缀

private boolean check(final String name, final String[] extensions) {

for (String end : extensions) {

if (rCase().endsWith(end)) {

return true;

}

}

return false;

}

/设置类型

if (check(name, ".apk")){

e(FILE_TYPE_APK);

} else if(check(name, ".pdf")){

e(FILE_TYPE_PDF);

} else if(check(name,

getStringArray(_filter))){

e(FILE_TYPE_PPT);

}

...................

_filter:

.ppt

.pptx

//根据包名卸载apk

private void uninstallPkg(String pkg) {

Uri packageURI = ("package:"+pkg);

Intent uninstallIntent = new Intent(_DELETE, packageURI);

startActivity(uninstallIntent);

//也可以用这种方法卸载

// getPackageManager().deletePackage(pkg, null, 0);

}

//获取设备存储路径(sd卡,usb)

ernalStorageDirectory();

ernalStorageDirectory().getParent();