未知变量或字段 imageURL
Unknown variable or field imageURL
我正在使用上下文菜单在 Web 视图中查找图像...
这是我通过 webviews 上的上下文菜单保存图像的代码 -
@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",如何解决这个问题?
如果这正是您收到的错误消息,那么您在某处有一个 'misspelling'。在您的声明中,变量是 'camelCase'
String imageUrl = ...
没有
String imageURL = ...
还没有看到错误,但是错误发生在哪一行?
编辑:
在你的class开头声明变量:(如)
String imageUrl = "";
然后从您当前的声明中删除 "string"(在 onCreateContextMenu 中),使其看起来像:
imageUrl = hr.getExtra();
我正在使用上下文菜单在 Web 视图中查找图像... 这是我通过 webviews 上的上下文菜单保存图像的代码 -
@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",如何解决这个问题?
如果这正是您收到的错误消息,那么您在某处有一个 'misspelling'。在您的声明中,变量是 'camelCase'
String imageUrl = ...
没有
String imageURL = ...
还没有看到错误,但是错误发生在哪一行?
编辑:
在你的class开头声明变量:(如)
String imageUrl = "";
然后从您当前的声明中删除 "string"(在 onCreateContextMenu 中),使其看起来像:
imageUrl = hr.getExtra();