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;
}
});