我有一个混合Cordova Android应用程序,它使用一个自定义(即我自己编写)插件。除此之外,我使用插件从外部源获取文件资源。插件中的相关Java代码如下所示
public static String grabFile(String url,String fName)
{
File file = new File(filePath,fName);
if ((null != file) && file.exists()) return file.toURI().toString();
URL aurl;
try{aurl = new URL(url);}catch(Exception e){return "";}
try(InputStream fIn = aurl.openStream();FileOutputStream fOut = new
FileOutputStream(file))
{
try
{
byte[] buffer = new byte[2048];
int length;
while ((length = fIn.read(buffer)) != -1) {fOut.write(buffer,0,length);}
return file.toURI().toString();
}
finally
{
fIn.close();
fOut.close();
}
}
catch(Exception e)
{
Feedback.postBackInfo(e.getMessage() + "grab file");
return "";
}
}
其中filePath
,Feedback.postBackInfo
等来自插件代码中的其他位置。这通常会返回类似这样的内容
file:/data/user/0/com.example.myapp/files/filename.png'
然后我在前端使用(Webview)Javascript来分配图像源和HTML元素背景
img.src = file:/data/user/0/com.example.myapp/files/filename.png
OR
element.style.backgroundImage = 'url(' +
file:/data/user/0/com.example.myapp/files/filename.png' + ')';
然而,这不起作用。显然,我需要在这里提供的URL结构不同。它应该如何构建
答案 0 :(得分:1)
我曾考虑过删除这个问题,但我决定将此处留下代码,以造福他人。简单的答案是file:/data/user...
需要更改为file:///data/user...
。为什么返回的URI有不同的格式我无法分辨。