Is there a way to load image as bitmap to Glide

后端 未结 11 1447
温柔的废话
温柔的废话 2020-12-04 19:35

Im looking for a way to use bitmap as input to Glide. I am even not sure if its possible. It\'s for resizing purposes. Glide has a good image enhancement with scale. The pro

相关标签:
11条回答
  • 2020-12-04 20:08

    A really strange case, but lets try to solve it. I'm using the old and not cool Picasso, but one day I'll give Glide a try. Here are some links that could help you :

    • Bitmap POC
    • Supporting bitmaps topic
    • Someone also facing your problem

    And actually a cruel but I think efficient way to solve this :

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
      yourBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
      Glide.with(this)
          .load(stream.toByteArray())
          .asBitmap()
          .error(R.drawable.ic_thumb_placeholder)
          .transform(new CircleTransform(this))
          .into(imageview);
    

    I'm not sure if this will help you, but I hope it can make you a step closer to the solution.

    0 讨论(0)
  • 2020-12-04 20:08

    Please use Implementation for that is:

    implementation 'com.github.bumptech.glide:glide:4.9.0'

         Glide.with(this)
         .asBitmap()
          .load("http://url")
        .into(new CustomTarget <Bitmap>() {   
    @Override  
    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition <? super Bitmap> transition) { 
                    // you can do something with loaded bitmap here
    
     }
    @Override 
    public void onLoadCleared(@Nullable Drawable placeholder) { 
     } 
    });
    
    0 讨论(0)
  • 2020-12-04 20:12

    The accepted answer works for previous versions, but in new versions of Glide use:

    RequestOptions requestOptions = new RequestOptions();
    requestOptions.placeholder(android.R.drawable.waiting);
    requestOptions.error(R.drawable.waiting);
    Glide.with(getActivity()).apply(requestOptions).load(imageUrl).into(imageView);
    

    Courtesy

    0 讨论(0)
  • 2020-12-04 20:12

    Most of the API's and methods of Glide are now deprecated. Below is working for Glide 4.9 and upto Android 10.

    For image URI

      Bitmap bitmap = Glide
        .with(context)
        .asBitmap()
        .load(image_uri_or_drawable_resource_or_file_path)
        .submit()
        .get();
    

    Use Glide as below in build.gradle

    implementation 'com.github.bumptech.glide:glide:4.9.0'
    
    0 讨论(0)
  • 2020-12-04 20:15

    In Kotlin,

    Glide.with(this)
                .asBitmap()
                .load("https://...")
                .addListener(object : RequestListener<Bitmap> {
                    override fun onLoadFailed(
                        e: GlideException?,
                        model: Any?,
                        target: Target<Bitmap>?,
                        isFirstResource: Boolean
                    ): Boolean {
                        Toast.makeText(this@MainActivity, "failed: " + e?.printStackTrace(), Toast.LENGTH_SHORT).show()
                        return false
                    }
    
                    override fun onResourceReady(
                        resource: Bitmap?,
                        model: Any?,
                        target: Target<Bitmap>?,
                        dataSource: DataSource?,
                        isFirstResource: Boolean
                    ): Boolean {
                        //image is ready, you can get bitmap here
                        return false
                    }
    
                })
                .into(imageView)
    
    0 讨论(0)
提交回复
热议问题