一身转战三千里,一剑曾百万师。这篇文章主要讲述如果您的CA不受系统信任,如何将Android应用程序连接到SSL服务器?相关的知识,希望能为你提供帮助。
我正在尝试从使用SSL保护的网站下载图像。当我在API 23(android 6.0)或更高版本上运行App时,下载图像是成功的,但是当App在较低的API上运行时,我得到SSLHandshakeException,其中包含“java.security.cert.CertPathValidator异常:找不到证书路径的信任锚。 “ 信息。
答案解决方案就在指尖之下。试试这个链接Security with HTTPS and SSL。这里提供完整的解决方案
另一答案
您还可以使用DownloadManager从Url下载图像并保存到SD卡【如果您的CA不受系统信任,如何将Android应用程序连接到SSL服务器()】DownloadImages(PictureURL,ImageSavePath);
public void DownloadImages(String theUrl, String thePath) {
Uri Download_Uri = Uri.parse(theUrl);
DownloadManager downloadManager = (DownloadManager) myContext.getSystemService(myContext.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
//Restrict the types of networks over which this download may proceed.
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
//Set whether this download may proceed over a roaming connection.
request.setAllowedOverRoaming(true);
//Set the local destination for the downloaded file to a path within the application's external files directory
String[] split = theUrl.split("/");
//request.setDestinationInExternalFilesDir(myContext, mySdCardImagePath, split[split.length - 1]);
//request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, split[split.length-1]);
File destinationFile = new File(thePath, split[split.length - 1]);
Uri uri = null;
// naugat does not need provider here
uri = Uri.fromFile(destinationFile);
request.setDestinationUri(uri);
//Set the title of this download, to be displayed in notifications (if enabled).
request.setTitle(split[split.length - 1]);
//Set a description of this download, to be displayed in notifications (if enabled)
request.setDescription(thePath);
request.setVisibleInDownloadsUi(true);
//Enqueue a new download and get the reference Id
long downloadReference = downloadManager.enqueue(request);
//Check_Image_Status(downloadReference);
}
另一答案
// Glide Image loader
compile 'com.github.bumptech.glide:glide:3.7.0'
Use following code inside onBindViewHolder
int defaultImagePath = R.drawable.default_thumb;
int errorImagePath = R.drawable.damaged_image;
holder.mImageViewContactItem.setImageResource(defaultImagePath);
String uri = photoPath;
loadImageWithGlide(context, holder.mImageViewContactItem, uri, defaultImagePath,
errorImagePath);
publicvoid loadImageWithGlide(final Context context, ImageView theImageViewToLoadImage,
String theLoadImagePath, int theDefaultImagePath, int tehErrorImagePath) {
if (context == null) return;
Glide.with(context) //passing context
.load(theLoadImagePath) //passing your url to load image.
.placeholder(theDefaultImagePath) //this would be your default image (like default profile or logo etc). it would be loaded at initial time and it will replace with your loaded image once glide successfully load image using url.
.error(tehErrorImagePath)//in case of any glide exception or not able to download then this image will be appear . if you won't mention this error() then nothing to worry placeHolder image would be remain as it is.
.diskCacheStrategy(DiskCacheStrategy.ALL) //using to load into cache then second time it will load fast.
//.animate(R.anim.fade_in) // when image (url) will be loaded by glide then this face in animation help to replace url image in the place of placeHolder (default) image.
//.fitCenter()//this method help to fit image into center of your ImageView
.into(theImageViewToLoadImage);
//pass imageView reference to appear the image./*Normal way to Load Image with Glide.
Glide.with(theContext)
.load(theImagePath)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(theImageView);
*/
}
推荐阅读
- 如何在Android设备上安装可信CA证书()
- 让我们加密证书与android api <20一起使用
- 当前关于高级内存管理的Apple文档是否存在轻微的不准确之处()
- 在Swift Cocoa App中实现“打开文件”
- 在tomcat中部署战争后,在catalina中重复WebapplicationException
- Tomcat war run application - 无法启动组件
- 从tomcat context.xml而不是application.properties获取数据源
- 使用Laravel处理密集任务
- Gulp揭秘(构建基于流的任务自动化工具)