【发布时间】:2018-07-13 21:51:51
【问题描述】:
我有一个混合 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
或
element.style.backgroundImage = 'url(' +
file:/data/user/0/com.example.myapp/files/filename.png' + ')';
但是,这不起作用。显然,我需要在此处提供的 URL 的结构不同。究竟应该如何构建
【问题讨论】:
标签: java android file cordova url