尝试仅获取位图时,Glide返回空位图

时间:2019-09-20 16:00:46

标签: java android kotlin bitmap glide

我正在尝试使用glide加载位图网址,然后我想使用glide返回位图,但是当我尝试获取位图时,它总是返回空位图,这是我的代码:

fun getimage(context: Context, filePath:String ):Bitmap {
            val audioUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
            val selection = MediaStore.Audio.Media.DATA + "=? "
            lateinit var bitmap: Bitmap
        GlobalScope.launch(Dispatchers.IO) {
            val projection = arrayOf<String>(MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ALBUM_ID)
            val cursor = context.getContentResolver().query(
                    audioUri,
                    projection,
                    selection,
                    arrayOf<String>(filePath), null)
            if (cursor != null && cursor.moveToFirst()) {
                val albumId = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID))
                val sArtworkUri = Uri.parse("content://media/external/audio/albumart")
                val imgUri = ContentUris.withAppendedId(sArtworkUri,
                        albumId)
                Glide.with(context)
                        .asBitmap()
                        .load(imgUri)
                        .error(R.mipmap.icon)
                        .listener(object : RequestListener<Bitmap> {
                            override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Bitmap>?, isFirstResource: Boolean): Boolean {
                                bitmap = BitmapFactory.decodeResource(context.resources, R.mipmap.icon)
                                return true
                            }

                            override fun onResourceReady(resource: Bitmap?, model: Any?, target: Target<Bitmap>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                                bitmap = resource!!
                                return false
                            }
                        })
                        .submit()
                        .get()
                cursor.close()
            }
        }
        return bitmap!!
    }

但是,相同的代码在我的recylerview适配器类中运行正常,没有任何问题:

 fun loadart(context:Context,filePath:String,holder: ProjectViewHolder) {
        val audioUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
        val selection = MediaStore.Audio.Media.DATA + "=? "
        val projection = arrayOf<String>(MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ALBUM_ID)
        val cursor = context.getContentResolver().query(
                audioUri,
                projection,
                selection,
                arrayOf<String>(filePath), null)
        if (cursor != null && cursor.moveToFirst())
        {
            val albumId = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID))
            val sArtworkUri = Uri.parse("content://media/external/audio/albumart")
            val imgUri = ContentUris.withAppendedId(sArtworkUri,
                    albumId)
            Glide.with(context)
                    .asBitmap()
                    .load(imgUri)
                    .error(R.mipmap.icon)
                    .listener(object : RequestListener<Bitmap>{
                        override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Bitmap>?, isFirstResource: Boolean): Boolean {
                            holder.covimg.setImageResource( R.mipmap.icon)
                            return true
                        }

                        override fun onResourceReady(resource: Bitmap?, model: Any?, target: Target<Bitmap>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                            return false
                        }
                    })
                    .into(holder.covimg)
            cursor.close()
        }
    }

我无法理解我在做什么错,对此的任何帮助将不胜感激

0 个答案:

没有答案