java - 如何在android中以编程方式搜索和下载图像?

标签 java android

我想在网站中搜索图像,例如“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/

相关文章:

android - Gradle:错误:包 com.nostra13.universalimageloader.core 不存在

android - 在 SwipeRefreshLayout @ v23.2.0 上切换选项卡时出现 NPE

java - 使用 hibernate 和 mysql 自动生成时,表创建的 SQL 不正确

java - 使用 BeanShell 在方法内执行脚本

android - iOS6、android 和位置 :fixed and scaling

java - Android NDK 捕获关键事件

java - 连接两个压缩字节数组

java - 检索表时无法获取表中 0 位置的第一个元素?

java - Viewpager 但不响应 clicklistener/

android - 在 Genymotion 中安装 Facebook APK