Search code examples
javaandroidandroid-webviewandroid-contextmenu

Unknown variable or field imageURL


I am using a Context Menu to go for Images in Webviews... This is my Code for saving Images via Context Menu on 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;
    }

But I am getting error Unknown Variable or Field "imageURL", how to fix this issue?


Solution

  • If that is the exact error msg you are getting, then you have a 'misspelling' somewhere. In your declaration the variable is 'camelCase'

    String  imageUrl = ...
    

    not

    String  imageURL = ...
    

    Didn't see the mistake yet, but what line does the error happen on?

    edit:

    Declare the variable in the beginning of your class: (such as)

    String imageUrl = "";
    

    Then remove the "string" from your current declaration (within onCreateContextMenu) so it looks like:

    imageUrl = hr.getExtra();