我正在使用上下文菜单来获取Web视图中的图像...这是我通过网络视图上的上下文菜单保存图像的代码 -
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
WebView webView = (WebView) v;
HitTestResult hr = webView.getHitTestResult();
//Setting Hit-Tests to go for Images Only!
int type = hr.getType();
String imageUrl = hr.getExtra();
if (type == HitTestResult.IMAGE_TYPE || type == HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Save");
}
}
@Override
public boolean onContextItemSelected(MenuItem item)
{
if(item.getTitle()== "Save")
{
//File & Folder Structure on your SdCard to Save Images
File file = new File
(Environment.getExternalStorageDirectory() + "/XYZ/");
if (!file.isDirectory())
{
file.mkdirs(); //If folder doesn't exist, this will Create One! :)
}
//Final Output will be "Your App Folder/Image name.jpg"
File savePic = new File (file, getFilenameFromURL(imageUrl));
//Setting up Download Manager to Show Image Download on Notif Panel/Status Bar :)
DownloadManager downloadManager = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(imageUrl));
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
request.setDestinationUri(Uri.fromFile(savePic));
request.setTitle("XYZ"); //Title Visible during Image Download
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
downloadManager.enqueue(request);
Toast.makeText(this,"Action 1 invoked", Toast.LENGTH_SHORT).show();
}
return true;
}
protected String getFilenameFromURL(URL url) {
return getFilenameFromURL(url.getFile());
}
protected String getFilenameFromURL(String url) {
String[] p = url.split("/");
String s = p[p.length - 1];
if (s.indexOf("?") > -1) {
return s.substring(0, s.indexOf("?"));
}
return s;
}
但是我收到错误未知变量或字段"imageURL",如何解决此问题?
如果这是您得到的确切错误消息,那么您在某处有"拼写错误"。在您的声明中,变量是"驼峰案例"
String imageUrl = ...
不
String imageURL = ...
还没有看到错误,但是错误发生在哪一行?
编辑:
在类的开头声明变量:(例如)
String imageUrl = "";
然后从当前声明中删除"字符串"(在 onCreateContextMenu 中),使其如下所示:
imageUrl = hr.getExtra();