【问题标题】:Unable to create bitmap from Path无法从路径创建位图
【发布时间】:2020-06-14 15:31:31
【问题描述】:

我的代码没有从我搜索过的路径创建位图,但没有找到解决方案,这是我的代码

val bitmap=BitmapFactory.decodeFile(path)

它总是返回 null 我也尝试过 glide 来执行此操作,但没有任何效果

  Glide.with(requireActivity()).asBitmap().load(pictureFilePath)
            .apply(RequestOptions().override(50, 50))
            .listener(object  : RequestListener<Bitmap> {
                override fun onLoadFailed(e: GlideException?, model: Any?, target: com.bumptech.glide.request.target.Target<Bitmap>?, isFirstResource: Boolean): Boolean {
                    return false
                }

                override fun onResourceReady(bitmap: Bitmap, model: Any?, target: com.bumptech.glide.request.target.Target<Bitmap>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                    val mybitmap= bitmap // this bitmap return null

                    return true
                }

            }).submit()

谁能在这件小事上帮助我?我已经厌倦了这样做

【问题讨论】:

  • path 是从哪里来的?
  • @CommonsWare 从相机我得到了 uri 然后将其转换为真实路径
  • 一个人没有从相机中得到尿。一个为相机提供一个 uri,以便相机应用可以向其写入图片。
  • @CommonsWare 我检查路径是否正确但是当我创建位图时它返回位图 null
  • “从相机我得到了 uri 然后将其转换为真实路径”——正如 blackapps 所指出的,你没有从相机中得到 Uri。而且,一般来说,Uri 没有“真实路径”。您可能想要编辑这个问题,或者提出一个新问题,在其中提供一个 minimal reproducible example,其中显示了您采取的所有步骤,从而产生了这个 path 值。

标签: android bitmap uri android-glide android-bitmap


【解决方案1】:

使用这个,path是你的本地路径请确保你的路径是正确的

BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(path.getAbsolutePath(),bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);

【讨论】:

    猜你喜欢
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 2013-03-17
    • 2018-12-10
    相关资源
    最近更新 更多