简体   繁体   English

使用 Intent 在浏览器中打开 Base64

[英]Open Base64 in Browser with Intent

i want to know if there is a way to open a Base64 with predefined MIME type with browser Intent.我想知道是否有办法使用浏览器意图打开具有预定义 MIME 类型的 Base64。 I have try something like this based on Android Studio Official Documentation :我已经尝试过基于 Android Studio 官方文档的类似方法:

    String url = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,[myBase64]"
    Uri webpage = Uri.parse(url);
    Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }

If i try to paste url into chrome, it automatically prompt a download window, but when i try above code, i got error like this如果我尝试将url粘贴到 chrome 中,它会自动提示一个下载窗口,但是当我尝试上面的代码时,我得到了这样的错误

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,[myBase64]

It seems that my url didn't accepted as valid url, how can i get my file from [mybase64] with browser intent ?似乎我的url没有被接受为有效的 url,我如何使用浏览器意图从[mybase64]获取我的文件? any help or hint will be greatly appreciated.任何帮助或提示将不胜感激。

thank you谢谢你

As you say your url is not valid.正如您所说,您的网址无效。 I don't know if my answer is accurate, but reading it is entirely like a single string, so '[myBase64]' doesn't say anything.我不知道我的回答是否准确,但阅读它完全就像一个字符串,所以 '[myBase64]' 什么也没说。

Try concatenating your base64 file in it .尝试在其中连接您的 base64 文件

You need to remove the "data:image/png;base64,":您需要删除“data:image/png;base64,”:

String imageB64 = url.substring(22);
byte[] decodedStr = Base64.decode(imageB64, Base64.NO_WRAP);

Documentation of Intent.ACTION_VIEW . Intent.ACTION_VIEW文档

Source: Opening a URL in Android's web browser from within application来源: 从应用程序内在 Android 的网络浏览器中打开 URL

The solution depends on what is the required behavior.解决方案取决于所需的行为。 Do you want to download the file?您要下载文件吗? If yes, you need to use Download Manager API for that.如果是,您需要为此使用Download Manager API。

If you need to open the file and let the Android OS select the appropriate application to open your intent, then you can set the mime type of your intent to spreadsheet first.如果您需要打开文件并让 Android 操作系统选择合适的应用程序来打开您的 Intent,那么您可以先将您的 Intent 的 mime 类型设置为电子表格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM