1

I've created a webview android app and added " long press to save image" option.
the code runs without a problem and it also downloads the image, I can open the downloaded image from notification panel but i dont know where it is saved. i searched each and every folder of my storage but i couldn't find the file. here is my code for my "Context Menu"

//To save image from web view
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    // Get the web view hit test result
    final WebView.HitTestResult result = myWebView.getHitTestResult();

    /*
        WebView.HitTestResult

            IMAGE_TYPE
                HitTestResult for hitting an HTML::img tag.

            SRC_IMAGE_ANCHOR_TYPE
                HitTestResult for hitting a HTML::a tag with src=http + HTML::img.
    */

    // If user long press on an image
    if (result.getType() == WebView.HitTestResult.IMAGE_TYPE ||
            result.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {

        // Set the title for context menu
        menu.setHeaderTitle("CONTEXT MENU");

        // Add an item to the menu
        menu.add(0, 1, 0, "Save Image")
                .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        // Get the image url
                        String imgUrl = result.getExtra();

                        // If this is an image url then download it
                        if (URLUtil.isValidUrl(imgUrl)) {
                            // Initialize a new download request
                            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(imgUrl));
                            request.allowScanningByMediaScanner();
                            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                            DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);


                            downloadManager.enqueue(request);

                            Toast.makeText(myContext, "image saved.", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(myContext, "Invalid image url.", Toast.LENGTH_SHORT).show();
                        }
                        return false;
                    }
                });

1 Answers1

1

This should save image in downloads folder. If it is not saving there, You can make some change in code

request.setDestinationInExternalPublicDir(
                Environment.DIRECTORY_DOWNLOADS,    //Download folder
                URLUtil.guessFileName(DownloadImageURL, null, null));  //Name of file

DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

You need to provide Storage permission in manifest file

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Also grant permission programatically and now you can save this image anywhere location you want to specify.

Raman Sharma
  • 159
  • 1
  • 5