在 Android 中显示来自驱动器的图像
displaying an image from drive in Android
我正在使用 Fresco 库在我的 Android 应用程序中显示图像。我想显示一些我用 public 赠款设置的图像(jpg 或 png)。
当我在做快速测试时,我只是从网上截取任何图像来设置一个 URL,但是当使用我需要使用的真实图像时,我有以下 url https://drive.google.com/uc?export=view&id=<>,但由于它是重定向,并且一旦重定向,新的 url 并不是图像本身,Fresco 无法显示它。
我尝试将 Picasso 作为替代库,但没有成功。
我还尝试为两个库 (https://drive.google.com/uc?export=download&id=<>) 下载 url。但是没有结果。
有人知道怎么可能得到这些图像吗?或者唯一的解决方案是下载它(使用第二个 url)处理接收到的对象存储它的位图并显示它?
要下载它,我应该使用什么以及如何使用?改造?
提前致谢。
Fresco 支持不同的网络堆栈。例如,您可以将 OkHttp 与 Fresco 一起使用,它应该遵循重定向或修改默认重定向以允许重定向 - 或者基于它们编写您自己的。
OkHttp 指南:http://frescolib.org/docs/using-other-network-layers.html
相关 GitHub 问题:https://github.com/facebook/fresco/issues/61
我找到了这个问题的解决方案(但仅当您使用 Google 云或 Google 脚本时才适用)。
它包括在内部使用以下代码创建一个 doGet() 服务:
var file = DriveApp.getFileById(fileId)
return Utilities.base64Encode(file.getBlob().getBytes());
并在您的应用中使用该 base64 值。使用这种格式,Fresco 可以发挥魔力
这不是立竿见影的解决方案,需要在不是您的 Android 应用程序的其他平台上做一些工作,但它运行良好。
您确定您的网址没有问题吗?
Picasso 使用直接 URL,例如:https://kudago.com/media/images/place/06/66/06662fda6309ce1ee9116d13bd1c66d5.jpg
然后您可以像这样下载图片:
Picasso.with(this)
.load(url)
.noFade()
.placeholder(R.drawable.placeholder_grey) //if you want to use a stub
.into(imageView, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
//here you can operate with image after it is downloaded
}
@Override
public void onError() {
}
});
希望对您有所帮助。
我正在使用 Fresco 库在我的 Android 应用程序中显示图像。我想显示一些我用 public 赠款设置的图像(jpg 或 png)。
当我在做快速测试时,我只是从网上截取任何图像来设置一个 URL,但是当使用我需要使用的真实图像时,我有以下 url https://drive.google.com/uc?export=view&id=<>,但由于它是重定向,并且一旦重定向,新的 url 并不是图像本身,Fresco 无法显示它。
我尝试将 Picasso 作为替代库,但没有成功。
我还尝试为两个库 (https://drive.google.com/uc?export=download&id=<>) 下载 url。但是没有结果。
有人知道怎么可能得到这些图像吗?或者唯一的解决方案是下载它(使用第二个 url)处理接收到的对象存储它的位图并显示它?
要下载它,我应该使用什么以及如何使用?改造?
提前致谢。
Fresco 支持不同的网络堆栈。例如,您可以将 OkHttp 与 Fresco 一起使用,它应该遵循重定向或修改默认重定向以允许重定向 - 或者基于它们编写您自己的。
OkHttp 指南:http://frescolib.org/docs/using-other-network-layers.html
相关 GitHub 问题:https://github.com/facebook/fresco/issues/61
我找到了这个问题的解决方案(但仅当您使用 Google 云或 Google 脚本时才适用)。
它包括在内部使用以下代码创建一个 doGet() 服务:
var file = DriveApp.getFileById(fileId)
return Utilities.base64Encode(file.getBlob().getBytes());
并在您的应用中使用该 base64 值。使用这种格式,Fresco 可以发挥魔力
这不是立竿见影的解决方案,需要在不是您的 Android 应用程序的其他平台上做一些工作,但它运行良好。
您确定您的网址没有问题吗? Picasso 使用直接 URL,例如:https://kudago.com/media/images/place/06/66/06662fda6309ce1ee9116d13bd1c66d5.jpg
然后您可以像这样下载图片:
Picasso.with(this)
.load(url)
.noFade()
.placeholder(R.drawable.placeholder_grey) //if you want to use a stub
.into(imageView, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
//here you can operate with image after it is downloaded
}
@Override
public void onError() {
}
});
希望对您有所帮助。