Android应用:用其他应用打开功能实现界面跳转和URI文件资源传递
程序员文章站
2022-04-30 22:13:56
...
1、找到AndroidManifest文件对应Activity进行配置
用其他应用打开指定文件时,实际上应用间会传递文件对应的URI资源连接,文件类型和URL等信息通过Bundle以Intent进行传递,因此要使开发的应用能接收打开文件的请求,必须为指定的文件打开Activity设置隐式意图的action(android.intent.action.VIEW)、category(android.intent.category.DEFAULT)、data(根据实际需要设置指定文件格式)
<application>
...
<activity android:name=".castbox.doc.activity.DocDisplayActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/vnd.ms-powerpoint" />
<data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation" />
<data android:mimeType="application/mspowerpoint" />
<data android:mimeType="application/ms-powerpoint" />
<data android:mimeType="application/powerpoint" />
<data android:mimeType="application/x-powerpoint" />
<data android:mimeType="application/mspowerpnt" />
<data android:mimeType="application/vnd-mspowerpoint" />
<data android:mimeType="application/x-mspowerpoint" />
<data android:mimeType="application/x-m" />
<data android:mimeType="application/pdf" />
<data android:mimeType="application/msword" />
<data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" />
<data android:mimeType="application/vnd.ms-excel" />
<data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
<data android:mimeType="text/plain" />
<data android:scheme="content" />
<data android:scheme="file" />
</intent-filter>
</activity>
</application>
2、获取到Intent中uri信息,则可进行资源的操作(这里主要进行文件复制操作)
if (null != getIntent().getData())
{
String uriStr = getIntent().getDataString();
Uri uri = Uri.parse(uriStr);
FileOutputStream out = null;
InputStream in = null;
String filePostFix;
try
{
String type = getIntent().getType(); // 获取文件类型
if (null != type)
{
filePostFix = getFileType(type);
}
in = this.getContentResolver().openInputStream(uri);
File file = new File("/storage/emulated/0/Android/data/" + getPackageName() + "/tempFile." + filePostFix);
if (file.exists())
{
file.delete();
}
file.createNewFile(file);
out = new FileOutputStream(file);
String filepath = "/storage/emulated/0/Android/data/" + getPackageName() + "/tempFile." + filePostFix;
byte[] b = new byte[1024 * 5];
int len = 0;
while ((len = in.read(b)) != -1)
{
out.write(b, 0, len);
}
out.flush();
in.close();
out.close();
}
catch (IOException e)
{
/* 对谷歌浏览器的适配 */
if (getIntent().getData().getHost().equals("com.android.chrome.FileProvider"))
{
String path = getIntent().getData().getPath().replace("/downloads/", "/download/");
Log.d(TAG, "onCreate: " + path);
filepath = "/storage/emulated/0/" + path;
}
}
finally
{
if (out != null)
{
try
{
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if (in != null)
{
try
{
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
private String getFileType(String type)
{
switch (type)
{
case "application/vnd.ms-powerpoint":
case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
case "application/mspowerpoint":
case "application/ms-powerpoint":
case "application/powerpoint":
case "application/x-powerpoint":
case "application/mspowerpnt":
case "application/vnd-mspowerpoint":
case "application/x-mspowerpoint":
case "application/x-m":
return "ppt";
case "application/pdf":
return "pdf";
case "application/msword":
return "doc";
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
return "docx";
case "application/vnd.ms-excel":
return "xls";
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
return "xlsx";
case "text/plain":
return "txt";
default:
return "";
}
}
上一篇: 递归删除菜单
下一篇: 一次Nexus因异常停机导致无法启动问题