【问题标题】:How to get image size if I know its URL? Android (java)如果我知道它的 URL,如何获取图像大小?安卓 (java)
【发布时间】:2020-05-31 04:30:29
【问题描述】:

我使用 Jsoup 来阅读网站。但在 html 中,并非所有图像都有大小信息。所以,如果它不存在,我想以其他方式找出图像大小,使用图像源 URL。

【问题讨论】:

  • 下载图片,然后使用BitmapFactory读取尺寸。

标签: java android image jsoup image-size


【解决方案1】:

我建议使用滑行

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.11.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
}

然后使用 glide 加载你的图片 url

        Glide.with(this)
        .asBitmap()
        .load(path)
        .into(new CustomTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                int width = bitmap.getWidth();
                int height = bitmap.getHeight()
            }

            @Override
            public void onLoadCleared(@Nullable Drawable placeholder) {
            }
        });

【讨论】:

  • 所以,我尝试了这个,但我的应用程序崩溃了。这是一个日志 [pastebin.com/7GhiVXbk],这就是我使用它的方式 [pastebin.com/KzN7v59a]。 Android Studio 将表达式 .into(...) 强调为“SimpleTarget 已被贬低”。
  • 对不起,我刚刚更新了答案,现在通过滑动获取位图尺寸的方法是使用自定义目标
  • 您要加载的图片网址是什么?
【解决方案2】:
/*this library*/

implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

/* this code */

Glide.with(this)
            .asFile()     // get size image url
            .load("link image url")
            .into(new SimpleTarget<File>() {
                @Override
                public void onResourceReady(@NonNull File resource, @Nullable 
                    Transition<? super File> transition) {
                    fab_full.setLabelText(""+resource.length());     //  /1024 kb
                }
            });

【讨论】:

    猜你喜欢
    • 2020-09-05
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 2017-07-14
    • 2011-03-03
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    相关资源
    最近更新 更多