欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android编程开发之打开文件的Intent及使用方法

程序员文章站 2023-12-06 08:47:34
本文实例讲述了android编程开发之打开文件的intent及使用方法。分享给大家供大家参考,具体如下: 在写文件管理系统时会用到各种打开不同格式的文件的需求,由于and...

本文实例讲述了android编程开发之打开文件的intent及使用方法。分享给大家供大家参考,具体如下:

在写文件管理系统时会用到各种打开不同格式的文件的需求,由于android系统默认内置了一些可以打开的系统应用,但还是不能满足需求,比如打开视频文件、word等,需要安装相应的播放软件才可以使用,这时程序会通过intent查找可以使用的软件实现通过代码打开一个文件需要2部分,一部分是要获取到不同文件的后缀,以便根据需求匹配相应的intent,另一个就是不同格式的文件打开的intent不同

1、在values目录下定义后缀数组文件fileendings

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<array name="fileendingimage"> 
<item>.png</item> 
<item>.gif</item> 
<item>.jpg</item> 
<item>.jpeg</item> 
<item>.bmp</item> 
</array> 
<array name="fileendingaudio"> 
<item>.mp3</item> 
<item>.wav</item> 
<item>.ogg</item> 
<item>.midi</item> 
</array> 
<array name="fileendingvideo"> 
<item>.mp4</item> 
<item>.rmvb</item> 
<item>.avi</item> 
<item>.flv</item> 
</array> 
<array name="fileendingpackage"> 
<item>.jar</item> 
<item>.zip</item> 
<item>.rar</item> 
<item>.gz</item> 
<item>.apk</item> 
<item>.img</item> 
</array> 
<array name="fileendingwebtext"> 
<item>.htm</item> 
<item>.html</item> 
<item>.php</item> 
<item>.jsp</item> 
</array> 
<array name="fileendingtext"> 
<item>.txt</item> 
<item>.java</item> 
<item>.c</item> 
<item>.cpp</item> 
<item>.py</item> 
<item>.xml</item> 
<item>.json</item> 
<item>.log</item> 
</array> 
<array name="fileendingword"> 
<item>.doc</item> 
<item>.docx</item> 
</array> 
<array name="fileendingexcel"> 
<item>.xls</item> 
<item>.xlsx</item> 
</array> 
<array name="fileendingppt"> 
<item>.ppt</item> 
<item>.pptx</item> 
</array> 
<array name="fileendingpdf"> 
<item>.p<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<array name="fileendingimage"> 
<item>.png</item> 
<item>.gif</item> 
<item>.jpg</item> 
<item>.jpeg</item> 
<item>.bmp</item> 
</array> 
<array name="fileendingaudio"> 
<item>.mp3</item> 
<item>.wav</item> 
<item>.ogg</item> 
<item>.midi</item> 
</array> 
<array name="fileendingvideo"> 
<item>.mp4</item> 
<item>.rmvb</item> 
<item>.avi</item> 
<item>.flv</item> 
</array> 
<array name="fileendingpackage"> 
<item>.jar</item> 
<item>.zip</item> 
<item>.rar</item> 
<item>.gz</item> 
<item>.apk</item> 
<item>.img</item> 
</array> 
<array name="fileendingwebtext"> 
<item>.htm</item> 
<item>.html</item> 
<item>.php</item> 
<item>.jsp</item> 
</array> 
<array name="fileendingtext"> 
<item>.txt</item> 
<item>.java</item> 
<item>.c</item> 
<item>.cpp</item> 
<item>.py</item> 
<item>.xml</item> 
<item>.json</item> 
<item>.log</item> 
</array> 
<array name="fileendingword"> 
<item>.doc</item> 
<item>.docx</item> 
</array> 
<array name="fileendingexcel"> 
<item>.xls</item> 
<item>.xlsx</item> 
</array> 
<array name="fileendingppt"> 
<item>.ppt</item> 
<item>.pptx</item> 
</array> 
<array name="fileendingpdf"> 
<item>.pdf</item> 
</array> 
</resources>df</item> 
</array> 
</resources>

2、定义openfiles工具类,只需传输file参数即可,然后通过返回的intent打开文件

public class openfiles { 
//android获取一个用于打开html文件的intent 
public static intent gethtmlfileintent(file file) 
{ 
uri uri = uri.parse(file.tostring()).buildupon().encodedauthority("com.android.htmlfileprovider").scheme("content").encodedpath(file.tostring()).build(); 
intent intent = new intent("android.intent.action.view"); 
intent.setdataandtype(uri, "text/html"); 
return intent; 
} 
//android获取一个用于打开图片文件的intent 
public static intent getimagefileintent(file file) 
{ 
intent intent = new intent("android.intent.action.view"); 
intent.addcategory("android.intent.category.default"); 
intent.addflags(intent.flag_activity_new_task); 
uri uri = uri.fromfile(file); 
intent.setdataandtype(uri, "image/*"); 
return intent; 
} 
//android获取一个用于打开pdf文件的intent 
public static intent getpdffileintent(file file) 
{ 
intent intent = new intent("android.intent.action.view"); 
intent.addcategory("android.intent.category.default"); 
intent.addflags(intent.flag_activity_new_task); 
uri uri = uri.fromfile(file); 
intent.setdataandtype(uri, "application/pdf"); 
return intent; 
} 
//android获取一个用于打开文本文件的intent 
public static intent gettextfileintent(file file) 
{ 
intent intent = new intent("android.intent.action.view"); 
intent.addcategory("android.intent.category.default"); 
intent.addflags(intent.flag_activity_new_task); 
uri uri = uri.fromfile(file); 
intent.setdataandtype(uri, "text/plain"); 
return intent; 
} 
//android获取一个用于打开音频文件的intent 
public static intent getaudiofileintent(file file) 
{ 
intent intent = new intent("android.intent.action.view"); 
intent.addflags(intent.flag_activity_clear_top); 
intent.putextra("oneshot", 0); 
intent.putextra("configchange", 0); 
uri uri = uri.fromfile(file); 
intent.setdataandtype(uri, "audio/*"); 
return intent; 
} 
//android获取一个用于打开视频文件的intent 
public static intent getvideofileintent(file file) 
{ 
intent intent = new intent("android.intent.action.view"); 
intent.addflags(intent.flag_activity_clear_top); 
intent.putextra("oneshot", 0); 
intent.putextra("configchange", 0); 
uri uri = uri.fromfile(file); 
intent.setdataandtype(uri, "video/*"); 
return intent; 
} 
//android获取一个用于打开chm文件的intent 
public static intent getchmfileintent(file file) 
{ 
intent intent = new intent("android.intent.action.view"); 
intent.addcategory("android.intent.category.default"); 
intent.addflags(intent.flag_activity_new_task); 
uri uri = uri.fromfile(file); 
intent.setdataandtype(uri, "application/x-chm"); 
return intent; 
} 
//android获取一个用于打开word文件的intent 
public static intent getwordfileintent(file file) 
{ 
intent intent = new intent("android.intent.action.view"); 
intent.addcategory("android.intent.category.default"); 
intent.addflags(intent.flag_activity_new_task); 
uri uri = uri.fromfile(file); 
intent.setdataandtype(uri, "application/msword"); 
return intent; 
} 
//android获取一个用于打开excel文件的intent 
public static intent getexcelfileintent(file file) 
{ 
intent intent = new intent("android.intent.action.view"); 
intent.addcategory("android.intent.category.default"); 
intent.addflags(intent.flag_activity_new_task); 
uri uri = uri.fromfile(file); 
intent.setdataandtype(uri, "application/vnd.ms-excel"); 
return intent; 
} 
//android获取一个用于打开ppt文件的intent 
public static intent getpptfileintent(file file) 
{ 
intent intent = new intent("android.intent.action.view"); 
intent.addcategory("android.intent.category.default"); 
intent.addflags(intent.flag_activity_new_task); 
uri uri = uri.fromfile(file); 
intent.setdataandtype(uri, "application/vnd.ms-powerpoint"); 
return intent; 
} 
//android获取一个用于打开apk文件的intent 
public static intent getapkfileintent(file file) 
{ 
intent intent = new intent(); 
intent.addflags(intent.flag_activity_new_task); 
intent.setaction(android.content.intent.action_view); 
intent.setdataandtype(uri.fromfile(file), "application/vnd.android.package-archive"); 
return intent; 
} 
}

3、定义用于检查要打开的文件的后缀是否在遍历后缀数组中

private boolean checkendswithinstringarray(string checkitsend, 
string[] fileendings){ 
for(string aend : fileendings){ 
if(checkitsend.endswith(aend)) 
return true; 
} 
return false; 
}

4、通过调用openfiles类返回的intent,打开相应的文件

if(currentpath!=null&¤tpath.isfile()) 
{ 
string filename = currentpath.tostring(); 
intent intent; 
if(checkendswithinstringarray(filename, getresources(). 
getstringarray(r.array.fileendingimage))){ 
intent = openfiles.getimagefileintent(currentpath); 
startactivity(intent); 
}else if(checkendswithinstringarray(filename, getresources(). 
getstringarray(r.array.fileendingwebtext))){ 
intent = openfiles.gethtmlfileintent(currentpath); 
startactivity(intent); 
}else if(checkendswithinstringarray(filename, getresources(). 
getstringarray(r.array.fileendingpackage))){ 
intent = openfiles.getapkfileintent(currentpath); 
startactivity(intent); 
}else if(checkendswithinstringarray(filename, getresources(). 
getstringarray(r.array.fileendingaudio))){ 
intent = openfiles.getaudiofileintent(currentpath); 
startactivity(intent); 
}else if(checkendswithinstringarray(filename, getresources(). 
getstringarray(r.array.fileendingvideo))){ 
intent = openfiles.getvideofileintent(currentpath); 
startactivity(intent); 
}else if(checkendswithinstringarray(filename, getresources(). 
getstringarray(r.array.fileendingtext))){ 
intent = openfiles.gettextfileintent(currentpath); 
startactivity(intent); 
}else if(checkendswithinstringarray(filename, getresources(). 
getstringarray(r.array.fileendingpdf))){ 
intent = openfiles.getpdffileintent(currentpath); 
startactivity(intent); 
}else if(checkendswithinstringarray(filename, getresources(). 
getstringarray(r.array.fileendingword))){ 
intent = openfiles.getwordfileintent(currentpath); 
startactivity(intent); 
}else if(checkendswithinstringarray(filename, getresources(). 
getstringarray(r.array.fileendingexcel))){ 
intent = openfiles.getexcelfileintent(currentpath); 
startactivity(intent); 
}else if(checkendswithinstringarray(filename, getresources(). 
getstringarray(r.array.fileendingppt))){ 
intent = openfiles.getpptfileintent(currentpath); 
startactivity(intent); 
}else 
{ 
showmessage("无法打开,请安装相应的软件!"); 
} 
}else 
{ 
showmessage("对不起,这不是文件!"); 
} 

希望本文所述对大家android程序设计有所帮助。