Android中很少有设备无法读取.crt(证书文件),而很少有人可以。如何处理这个设备相关的问题?是否有任何其他Intent打开除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();
}
答案 0 :(得分:1)
我不确定您现在遇到的确切问题(没有堆栈跟踪很难说),但可能是因为您的某些设备没有安装SD卡。事实上,我一直在研究加载证书的最佳解决方案,如果你只需要管理一些证书,将它们放在/ res / raw /文件夹中可能比将它们存储在SD卡中更好。以下是如何从/ res / raw /:
加载一个InputStream is = context.getResources().openRawResource(R.raw.cert_id);