我想在网站上搜索图像,例如-'pixabay,并下载搜索结果中的第一张图像。
我可以使用“自然”或“天空”之类的关键字轻松搜索图像。但是我找不到下载搜索结果中第一张图片的方法。
有什么办法吗?
您需要做的就是获取您要下载的图像的URL,然后可以像下面这样将其加载到android中:
/**
* Gets the async task for image loading
*
* @param url - the url to load the image from
* @return the executor that will pull the image from the URL
*/
private static AsyncTask<String, String, Bitmap> getImageLoader(final String url) {
return new AsyncTask<String, String, Bitmap>() {
@Override
protected Bitmap doInBackground(String... strings) {
try {
final URL imageLocation = new URL(url);
final BitmapFactory.Options options = new BitmapFactory.Options();
//uncomment if you want the image to be mutable
//options.inMutable = true;
return BitmapFactory.decodeStream(imageLocation.openConnection().getInputStream(), null, options);
} catch (final IOException e) {
e.printStackTrace();
}
return null;
}
};
}
您可以通过这种方式完成
Uri Download_Uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
request.setTitle("title");
request.setDescription(description);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "/newfolder/" +"title" + ".png");
refId = downloadManager.enqueue(request);