android中很少有设备无法读取。crt(证书文件),而很少有设备可以。如何处理这个设备相关的问题?除了ACTION_VIEW之外,是否有其他打开文件的意图可以作为打开文件的替代方法。
File vpnCerti = new File("/sdcard/VPNCertificate/Install.crt");
Uri path = Uri.fromFile(vpnCerti); //Mime Type Info : http://webdesign.about.com/od/multimedia/a/mime-types-by-content-type.htm
MimeTypeMap type_map = MimeTypeMap.getSingleton();
//Get the extension from the path
String extension = MimeTypeMap.getFileExtensionFromUrl(path.toString());
extension = extension.toLowerCase();
if (extension.contains(".")) {
extension = extension.substring(extension.lastIndexOf("."));
}
String mime_type = type_map.getMimeTypeFromExtension(extension);
Log.d("DownloadManager", "MIME Type : " + mime_type);
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(vpnCerti), mime_type);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try
{
Log.d("DownloadManager", "Trying to open file");
startActivity(i);
Log.d("DownloadManager", "Opened file");
}
catch(ActivityNotFoundException e)
{
Log.d("DownloadManager", "Couldn't open file");
Toast.makeText(SettingsActivity.this, "Couldn't find specific activity to open it", Toast.LENGTH_LONG).show();
}
我不确定您现在遇到的确切问题(没有堆栈跟踪很难判断),但这可能是因为您的一些设备没有安装SD卡。事实上,我一直在研究加载证书的最佳解决方案,如果你只有几个证书要管理,把它们放在/res/raw/文件夹中可能比存储在sd卡中更好。下面是如何从/res/raw/加载一个:
InputStream is = context.getResources().openRawResource(R.raw.cert_id);