Android应用:用其他应用打开功能实现界面跳转和URI文件资源传递

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 "";
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值