【问题标题】:Picasso Load image from filesystemPicasso 从文件系统加载图像
【发布时间】:2014-07-04 01:46:51
【问题描述】:

我可以使用 Picasso 库从文件系统加载图像吗?

我正在使用startActivityForResult 让用户从他的图库中选择一张照片,然后想要显示选定的图像。

我已经有工作代码来获取图像文件系统Uri,但无法让Picasso.load() 方法工作。

【问题讨论】:

    标签: android picasso


    【解决方案1】:

    当然可以。它实际上非常简单:

    File f = new File("path-to-file/file.png");
    

    File f = new File(uri);
    
    Picasso.get().load(f).into(imageView);
    

    还有

    Picasso.get().load(uri).into(imageView);
    

    作品

    【讨论】:

    • 我不知道它是否是毕加索从文件系统加载图像所需的“特定”URI 格式(字符串格式)。但是我使用了从 ActivityResult 返回的那个,直到我传递了一个 File 对象而不是直接传递 String 才起作用。
    • 我正在尝试这样做,但这不起作用,我从另一个活动中获得了一个文件到我的应用程序缓存但毕加索没有加载它...
    • patrickf 的答案可以解决问题,但是自从发布答案以来,毕加索发生了一些变化,您需要使用以下语法:Picasso.get().load(f).into(imageView);Picasso.get().load(uri).into(imageView);Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(imageView);跨度>
    【解决方案2】:

    是的,你可以。

    试试:

    Picasso.with(context).load(new File(YOUR_FILE_PATH)).into(imageView);
    

    编辑

    您也可以致电.load(YOUR_URI)

    【讨论】:

      【解决方案3】:

      查看源代码我还发现您可以从文件系统加载图像,将file: 字符串前缀添加到图像路径。例如:

      file:path/to/your/image
      

      另外,当使用 startActivityForResult 时,你会得到这样的结果:

      Uri imageContent = data.getData();
      

      然后,您可以直接调用Picasso.with(getContext()).load(imageContent.toString).into(imageView);,而无需创建Cursor并查询图片路径。

      【讨论】:

      • 谢谢你,直到我看到你的回答是“文件:”前缀是必需的。
      • 我不知道为什么它不起作用。有我的路径 - "file:/storage/emulated/0/Android/data/com.fittingroom.newtimezone/files/default/AvatarPackage/DEFAULT_MY_AVATAR/pose1.jpeg" ,但任何结果(
      • @AlekseyTimoshchenko 它应该以file:// 开头。您的 Uri 缺少第二个 /
      【解决方案4】:

      试试这个:

      Picasso.with(context)
      .load("file://"+path) // Add this
      .config(Bitmap.Config.RGB_565)
      .fit().centerCrop()
      .into(imageView);
      

      它非常适合我。

      【讨论】:

      • mh,我会使用 RGB_8888,除非由于图像太大而导致内存限制存在问题
      【解决方案5】:
      > 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);
      

      【讨论】:

      • 这是使用新版本毕加索的方法。 Picasso.with() 不再可用。
      【解决方案6】:

      基本上我们需要三个东西,Contextimage´s pathImageView Container

      旧版毕加索

       Picasso.with(context).load("/files/my_image.jpg").into(myImageView);
      

      毕加索的新版本

       Picasso.get().load("/files/my_image.jpg").into(myImageView);
      

      但我们可以使用更多选项:

        .resize(20, 20)
        .centerCrop()
        .placeholder(R.drawable.user_placeholder)
        .error(R.drawable.user_placeholder_error)
      

      等等……

      更多信息:http://square.github.io/picasso/

      【讨论】:

        【解决方案7】:

        如果有人试图用 Kotlin 来做这件事,那就是……

        //变量

        private lateinit var addImage: ImageView  // set the id of your ImageView
        private lateinit var imageUri: Uri
        

        //打开图库以选择图像

        val gallery = Intent()
                gallery.type = "image/*"
                gallery.action = Intent.ACTION_GET_CONTENT
        
                startActivityForResult(Intent.createChooser(gallery, "Select picture"), PICK_IMAGE)
        

        //下一个

        override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
                super.onActivityResult(requestCode, resultCode, data)
                if (requestCode == PICK_IMAGE && resultCode == RESULT_OK) {
                    imageUri = data?.data!!
                    try {
        
                        Picasso.get()
                            .load(imageUri)
                            .into(addImage)
        
                    } catch (e: Throwable) {
                        e.printStackTrace()
                    }
                }
            }
        

        这就是你所需要的。

        【讨论】:

          猜你喜欢
          • 2020-03-16
          • 2016-06-30
          • 2016-07-29
          • 2020-08-03
          • 2019-07-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多