我在 azure 中托管我的应用程序(Web 服务 + 文件存储服务)。
我希望在我的应用程序中使用 Fresco (SimpleDraweeView),问题是我无法向用户提供直接的 url,因为 azure 中的存储是私有(private)的。
我唯一能做的就是将图像获取为字节数组(使用我的网络服务)并将该字节数组转发回 Android 客户端。
如何将 simpledraweeview 与字节数组一起使用而不是直接链接?
我尝试在我的 web 服务中设置一个端点,其中用户向我提供图像 id,端点返回字节数组,我尝试使用此端点作为 simpledraweeview.setImageUrl 方法的 url,但没有运气好。
最佳答案
根据您的描述,根据我的经验,听起来您需要创建一个 Web 应用程序作为代理服务来访问 Azure File Storgae 上托管的图像并将其响应回 andoird。
假设你是一名Java Web开发人员,我认为简单的方法是你可以create a Java web app in Azure App Service ,然后为Java Web应用程序创建一个Java servlet并引用文章How to use File Storage from Java要下载图像以将流传输到 http servlet 响应,请参阅下面的示例代码。
private static final String storageConnectionString =
"DefaultEndpointsProtocol=http;" +
"AccountName=your_storage_account_name;" +
"AccountKey=your_storage_account_key";
private CloudFileClient fileClient;
/**
* @see HttpServlet#HttpServlet()
*/
public PipeStream4FileStorage() {
super();
try {
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
fileClient = storageAccount.createCloudFileClient();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String dirName = request.getParameter("dir");
String fileName = request.getParameter("file");
OutputStream outStream = response.getOutputStream();
try {
// Get a reference to the file share
CloudFileShare share = fileClient.getShareReference("sampleshare");
//Get a reference to the root directory for the share.
CloudFileDirectory rootDir = share.getRootDirectoryReference();
//Get a reference to the directory that contains the file
CloudFileDirectory sampleDir = rootDir.getDirectoryReference(dirName);
//Get a reference to the file you want to download
CloudFile file = sampleDir.getFileReference(fileName);
//Write the stream of the file to the httpServletResponse.
file.download(outStream);
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (StorageException e) {
e.printStackTrace();
}
}
注意,关键函数请参见javadoc download(OutputStream outStream)
.
然后,您只需为 SimpleDraweeView 设置图像 url,如下所示。
Uri uri = Uri.parse("https://<your-webapp-name>.azurewebsites.net/<servlet-url-mapping-name>?dir=<dir-name>&file=<file-name>");
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
draweeView.setImageURI(uri);
关于安卓 | Fresco/FrescoLib + Azure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38150520/