gpt4 book ai didi

android - 长按从 WebView 保存图像

转载 作者:行者123 更新时间:2023-12-05 00:17:33 24 4
gpt4 key购买 nike

需要帮助

我正在尝试通过长按照片从 webview 下载图片,
当我用 toast 消息测试长按 Action 时,它起作用了

但它没有下载。

 private View.OnLongClickListener listener=new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
mWebView.setDownloadListener(new DownloadListener() {

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));

request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Name of your downloadble file goes here, example: Keerthi");
DownloadManager dm = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
dm.enqueue(request);
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); //This is important!
intent.addCategory(Intent.CATEGORY_OPENABLE); //CATEGORY.OPENABLE
intent.setType("*/*");//any application,any extension
Toast.makeText(getContext(), "Downloading File", //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();

}
});
return true;
}
};

最佳答案

Alexander 和 Aashish 的回答都很出色。
但是,这是来自 tutorial I recently found 的完整 Activity 。 .它将所有内容置于清晰的上下文中。

package com.android_examples.saveimagefromwebview_android_examplescom;
import android.app.DownloadManager;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.URLUtil;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

WebView webView;
String HTTP_URL = "https://www.google.com" ;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

webView = (WebView)findViewById(R.id.WebView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());

//Register the WebView to be able to take display a menu...
//You'll need this menu to choose an action on long press
registerForContextMenu(webView);
webView.loadUrl(HTTP_URL);
}

@Override
public void onCreateContextMenu(ContextMenu contextMenu, View view,
ContextMenu.ContextMenuInfo contextMenuInfo){
super.onCreateContextMenu(contextMenu, view, contextMenuInfo);

final WebView.HitTestResult webViewHitTestResult = webView.getHitTestResult();

if (webViewHitTestResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||
webViewHitTestResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {

contextMenu.setHeaderTitle("Download Image From Below");

contextMenu.add(0, 1, 0, "Save - Download Image")
.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {

String DownloadImageURL = webViewHitTestResult.getExtra();

if(URLUtil.isValidUrl(DownloadImageURL)){

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DownloadImageURL));
request.allowScanningByMediaScanner();


request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
downloadManager.enqueue(request);

Toast.makeText(MainActivity.this,"Image Downloaded Successfully.",Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this,"Sorry.. Something Went Wrong.",Toast.LENGTH_LONG).show();
}
return false;
}
});
}
}
}

关于android - 长按从 WebView 保存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40092893/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com