我需要打开一个从JSON的URL获取的PDF文件。
该URL的末尾不包含文件扩展名。我尝试过这样的事情:
final String pdfUrl = dataUrl.replace("data", "file") + "/x?filefield=" + tf.getFieldName() + "&context=\"" + dataItemIdModOne + "\"&skipcache=1";
SharedPreferences sharedPrefs = getActivity().getSharedPreferences(Constants.userDetails, MODE_PRIVATE);
String username = sharedPrefs.getString(Constants.user, "");
String password = sharedPrefs.getString(Constants.pass, "");
String userPassword = username + ":" + password;
final String basicAuth = "Basic " + new String(new Base64().encode(userPassword.getBytes()));
final Map<String, String> extraHeaders = new HashMap<>();
extraHeaders.put("Authorization", basicAuth);
pdfIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uri uri = Uri.parse(pdfUrl);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(uri,"*/*");
Bundle bundle = new Bundle();
bundle.putString("Authorization", basicAuth);
target.putExtra(Browser.EXTRA_HEADERS, bundle);
Intent intent = Intent.createChooser(target, "Open File");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(getActivity(), "Please install PDF app", Toast.LENGTH_SHORT).show();
}
}
});
我只需要向用户提供一个应用选择器,以便他/她将使用巫婆应用来选择打开文件,或者充其量只是直接在他/她的设备上打开可用的PDF阅读器。
通过此实现,我得到了应用选择器,并在尝试打开文件时加载了该文件,但最后它说无法打开。
因此,使用此URL进行的钻探是,当调用URL时,它将重定向到另一个包含PDF文件本身的URL。但是我看不到第二个URL。另外,当我将URL粘贴到浏览器中时,它会要求输入用户名和密码(这就是为什么我需要基本身份验证)。
提前致谢。
PDF查看器很少(如果有)支持http
/https
支持他们的ACTION_VIEW
活动。实际上,它们中没有一个会支持随机Intent
附加项,例如EXTRA_HEADERS
。
使用您喜欢的HTTP客户端API(例如HttpUrlConnection
,OkHttp)自己下载PDF 。然后,使用ACTION_VIEW
来查看下载的PDF。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句