我想在网站中搜索图像,例如“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;
}
};
}
关于java - 如何在android中以编程方式搜索和下载图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59546619/