【问题标题】:Picasso image loading毕加索图片加载
【发布时间】:2015-11-16 06:08:02
【问题描述】:

我正在尝试在 Picasso 2.5.2 的帮助下从 gridview 中的相机路径加载图像

存储/模拟/0/DCIM/相机/IMG_20150822_133220.jpg 这是路径。

我还在毕加索的 Github 存储库中尝试了给定的解决方案。但这并没有解决我的问题。

我尝试了毕加索的转换,但图像没有从相机路径加载。

我试过了

File imageFile = new File(data.path);
Picasso.with(mContext)
  .load(imageFile)
  .placeholder(R.drawable.default_error)
  .error(R.drawable.default_error)
  .resize(mItemSize, mItemSize)
  .centerCrop()
  .into(image);

【问题讨论】:

  • 能否请您显示代码以及logcat中是否有错误?
  • @Miriana Itani :问题已更新。还尝试使用 uri、stringpath 以及两者都使用“file:///”作为前缀,但都不起作用。我坚持下去了。
  • 你用什么手机用什么软件?
  • 其实手机在这里无所谓。我试过三星 SM-G355H (4.4.2)、HTC Desire 816G(4.4.2)、三星 S3(4.2)。
  • 我只是在检查它是否是棒棒糖。我知道这很愚蠢,但您拥有所有权限,对吗?我还希望您尝试加载一个网址。然后我们可以开始缩小问题的范围。

标签: android picasso


【解决方案1】:

您必须将 url 传递给 load() 方法。

File imageFile = new File(data.path);

 try{
        String url = imageFile.toURI().toURL().toString(); 
        Picasso.with(mContext)
               .load(url)
               .placeholder(R.drawable.default_error)
               .error(R.drawable.default_error)
               .resize(mItemSize, mItemSize)
               .centerCrop()
               .into(image);   
    }catch (MalformedURLException exeption){
        exeption.printStackTrace();
    }

【讨论】:

    【解决方案2】:

    不要使用硬编码的路径来访问 sd 卡内容——它们不是跨设备通用的。

    改用.getExternalStoragePublicDirectory() 和适当的两参数File 构造函数:

    File f = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
            "Camera/IMG_20150822_133220.jpg");
    Picasso.with(mContext).load(f)./* ..;
    

    UPD哦,实际上,除此之外,他们还有看起来相关的an open bug,那里有人说他们使用绝对路径解决了这个问题,所以试试

    Picasso.with(context).load(new File(data.path).getAbsolutePath())...
    

    【讨论】:

    • 它不是硬编码的。我给了你调试时得到的路径。
    • 发现了一些东西,看看我的回答中的更新
    • 巴斯托夫。抱歉,使用 Absolutepath 仍然无法正常工作。
    • 嗯。好的,那我没有建议了。至少去你的评论或简单的 +1 到我链接的 github 上的错误 - 这将使它更快地引起开发人员的注意。
    【解决方案3】:

    我现在遇到了类似的问题,这就是我解决它的方法。我正在尝试截取手机屏幕的屏幕截图,然后将其保存为此文件路径中的 jpg 文件:

    filepath: /data/data/com.example.simon.myapp/app_report/report.jpg
    

    但是,毕加索根本没有加载这个 - 它只是给了我一个空白屏幕。

    所以当我截取屏幕截图时,我决定从文件名中删除 .jpg,以便文件路径类似于:

    filepath: /data/data/com.example.simon.myapp/app_report/report
    

    然后我使用以下代码访问并显示我的屏幕截图,没有其他问题。

        ContextWrapper cw = new ContextWrapper(context);
        File directory = cw.getDir("report", Context.MODE_PRIVATE);
        file = new File(directory, "/report");
        Log.e("filepath", file.getPath());
        Picasso.with(context).load(file).memoryPolicy(MemoryPolicy.NO_CACHE).into(screenshot);
    

    我知道您将需要在将来某个阶段获取 jpg 文件扩展名,因此如果您需要获取“report.jpg”,只需使用以下方法重命名文件:

    android, How to rename a file?

    private boolean rename(File from, File to) {
        return from.getParentFile().exists() && from.exists() && from.renameTo(to);
    } 
    

    【讨论】:

      猜你喜欢
      • 2020-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-07
      • 2015-11-14
      • 1970-01-01
      相关资源
      最近更新 更多