【发布时间】:2020-03-16 18:04:49
【问题描述】:
我正在使用 Picasso 2.7 从网络和本地文件系统加载图像。我遇到了毕加索根本没有从本地文件系统加载图像的问题。这是代码sn-p:
String filePath = "file:///sdcard/ic_download_normal.png";
Picasso.get().setLoggingEnabled(true);
Picasso.get().load(filePath.trim())
.error(R.drawable.error)
.into(imageView, new Callback() {
@Override
public void onSuccess() {
Log.error(TAG, "Successfully loaded image into view");
}
@Override
public void onError(Exception e) {
Log.error(TAG, "Error loading file: " + e);
}
});
发生的情况是 imageView 保持为空,并且永远不会填充错误图像。 Callback 中的任何回调方法都不会被调用,因此请求似乎永远不会完成或出错。
我查看了生成的毕加索日志,发现毕加索检索图像文件的请求已启动:
D/Picasso: Main created [R1] Request{file:///sdcard/ic_download_normal.png}
D/Picasso: Dispatcher enqueued [R1]+27ms
D/Picasso: Hunter executing [R1]+28ms
但是,与我正在运行的其他 Picasso 任务不同,我从未看到上述 Picasso 请求完成(似乎只是卡在执行中),这可能解释了为什么永远不会调用 onSuccess 和 onError。
我仔细检查了文件路径是否存在,如果我自己将文件拉入,将其转换为位图并手动(而不是通过 picasso)将其加载到 imageView 中,则 imageView 被正确填充。此外,我能够从有效的 URL 中提取图像,以便该部分正常工作,它似乎无法处理本地文件系统上的图像(是的,我确实拥有 WRITE_EXTERNAL_STORAGE 和 READ_EXTERNAL_STORAGE 权限)。
我也尝试过使用 File 而不仅仅是本地文件路径,同样的问题。在这一点上,我不确定有什么问题,任何见解或建议将不胜感激。
【问题讨论】:
-
从您的路径中删除“file://”。
-
根据毕加索自己的文档,本地文件路径应该附加“file://”(square.github.io/picasso/#features),例如Picasso.get().load("file:///android_asset/DvpvklR.png").into(imageView2);此外,如果没有附加的“file://”,我会收到错误:Unrecognized type of request: Request{/sdcard/ic_download_normal.png}