Android获取Bitmap网络图片类型

常见的获取图片格式的方式

Android中常见的图片格式有png、jpeg(jpg)、gif、webp,不同格式的图片,那么如何获取图片类型呢?

常见的有两种方式,一种是在Bitmap加载过程中,通过BitmapFactory.Options#outMimeType来获取图片对应的格式,另一种是通过文件头信息来判断。

效果图:

jpeg

png

gif

webp

具体实现

因为我们这里针对的是网络图片,所以第一步是将图片下载到本地。

通过[BitmapFactory.Options#outMimeType]获取图片格式

接着我们可以通过BitmapFactory.decodeFile(String pathName, Options opts)方法,从opts中获取对应的outMimeType,然后根据outMimeType即可判断对应的类型了。

具体代码如下:

Observable.create<File> {
    val fileTask = Glide.with(this)
        .asFile()
        .load(jpgUrl)
        .submit()
    val file = fileTask.get()
    it.onNext(file)
    it.onComplete()
}
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .map {
        ivJpeg.setImageBitmap(BitmapFactory.decodeFile(it.absolutePath))
        it
    }
    .observeOn(Schedulers.io())
    .map({
        getBmpTypeByOptions(it.absolutePath)
    })
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(object : Observer<String> {
        override fun onComplete() {
            LogUtils.e(TAG, "onComplete")
        }

        override fun onSubscribe(d: Disposable?) {
            LogUtils.e(TAG, "onSubscribe")
        }

        override fun onNext(t: String?) {
            LogUtils.e(TAG, "onNext")
            LogUtils.e(TAG, "onNext mimeType:" + t)
            tvJpegInfoByBmpOptions.text = tvJpegInfoByBmpOptions.text.toString() + t
        }

        override fun onError(e: Throwable?) {
            LogUtils.e(TAG, "onError")
        }

    })

核心代码:

fun getBmpTypeByOptions(filePath: String): String {
    val options = BitmapFactory.Options()
    options.inJustDecodeBounds = true
    BitmapFactory.decodeFile(filePath, options)
    return options.outMimeType
}
通过文件头信息来判断图片格式
Observable.create<File> {
    var fileTask = Glide.with(this)
        .asFile()
        .load(gifUrl)
        .submit()
    val file = fileTask.get()
    it.onNext(file)
    it.onComplete()
}
    .subscribeOn(Schedulers.io())
    .observeOn(Schedulers.io())
    .map({
        FileTypeUtil.getMimeType(it.absolutePath)
    })
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(object : Observer<String> {
        override fun onComplete() {
            LogUtils.e(TAG, "onComplete")
        }

        override fun onSubscribe(d: Disposable?) {
            LogUtils.e(TAG, "onSubscribe")
        }

        override fun onNext(t: String?) {
            LogUtils.e(TAG, "onNext")
            LogUtils.e(TAG, "onNext mimeType:" + t)
            tvGifInfoByHead.text = tvGifInfoByHead.text.toString() + t
        }

        override fun onError(e: Throwable?) {
            LogUtils.e(TAG, "onError")
        }

    })

核心代码在FileTypeUtil中,具体请看FileTypeUtil.java

项目地址

tinyvampirepudge/AndroidStudy

具体页面地址: BitmapTypeActivity.kt

参考

https://my.oschina.net/ososchina/blog/1610685?nocache=1591319567444

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tinyvampirepudge

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值