【问题标题】:how to load bitmap directly with picasso library like following如何使用毕加索库直接加载位图,如下所示
【发布时间】:2016-04-10 07:47:50
【问题描述】:
Picasso.with(context).load("url").into(imageView);

在这里,我想要位图而不是 url,我该如何实现这一点。 如下-

Picasso.with(context).load(bitmap).into(imageView);

【问题讨论】:

    标签: java picasso


    【解决方案1】:

    这应该适合你。 将返回的 URI 与 Picasso 一起使用。

    (取自:is there away to get uri of bitmap with out save it to sdcard?

    public Uri getImageUri(Context inContext, Bitmap inImage) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
        return Uri.parse(path);
    }
    

    【讨论】:

    • 虽然它有效,但它不是最好的解决方案,因为它需要“写入存储”权限。
    【解决方案2】:

    我的 Kotlin 解决方案

    从数据创建位图

        val inputStream = getContentResolver().openInputStream(data.data)
        val bitmap = BitmapFactory.decodeStream(inputStream)
        val stream = ByteArrayOutputStream()
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream)
    

    重要提示: 如果您不需要存储图片,您可以避开 Picasso 并立即加载图片

        imageView.setImageBitmap(bitmap)
    

    否则存储文件并用毕加索加载它

        val jpegData = stream.toByteArray()
    
        val file = File(cacheDir, "filename.jpg")
        file.createNewFile()
    
        val fileOS = FileOutputStream(file)
        fileOS.write(jpegData)
        fileOS.flush()
        fileOS.close()
    
        Picasso.get().load(Uri.parse(file.path)).into(imageView)
    

    【讨论】:

      【解决方案3】:
      private void loadBitmapByPicasso(Context pContext, Bitmap pBitmap, ImageView pImageView) {
          try {
              Uri uri = Uri.fromFile(File.createTempFile("temp_file_name", ".jpg", pContext.getCacheDir()));
              OutputStream outputStream = pContext.getContentResolver().openOutputStream(uri);
              pBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
              outputStream.close();
              Picasso.get().load(uri).into(pImageView);
          } catch (Exception e) {
              Log.e("LoadBitmapByPicasso", e.getMessage());
          }
      }
      

      【讨论】:

      • 实现'com.squareup.picasso:picasso:2.71828'
      【解决方案4】:

      您使用的是旧版本的毕加索。

      更新 Gradle 文件中的版本:

      implementation 'com.squareup.picasso:picasso:2.71828'
      

      Java:

      Picasso.get().load(R.drawable.landing_screen).into(imageView1);
      Picasso.get().load("file:///android_asset/DvpvklR.png").into(imageView2);
      Picasso.get().load(new File(...)).into(imageView3);
      

      See more details on the Picasso website

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-06
        • 2016-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-25
        相关资源
        最近更新 更多