如何从WebView下载图像并将其保存在外部存储中?

时间:2018-10-07 08:47:10

标签: android webview bitmap android-webview android-download-manager

  1. 我想通过长按图像从Webview下载sdCard中的图像。
  2. 我想通过长按图像共享图像,而不像chrome那样将图像存储在sdCard中。 我可以下载图像,但是它们没有保存在sdCard中。 我几乎在整个网络上搜索,但没有任何解决方案。

    public class MainActivity extends AppCompatActivity {
    
    WebView webView;
    String HTTP_URL = "https://www.google.com" ;
    private long downloadID ;
    Bitmap bitmap;
    Tag TAG;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    webView = (WebView)findViewById(R.id.myWebView);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new WebViewClient());
    webView.getSettings().setDomStorageEnabled(true);
    
    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("Select option");
    
    
    contextMenu.add(0, 1, 0, "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);
    downloadID = downloadManager.enqueue(request);
    String publicDirPath = ExternalStorageUtil.getPublicExternalStorageBaseDir("/");
    File newFile = new File(publicDirPath, "download");
    newFile.mkdirs();
    
    File newFile1 = new File(newFile, "image.png");
    OutputStream os;
    
    try {
    os = new FileOutputStream(newFile1);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
    os.flush();
    os.close();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    
    } return false;
    }
    
    
    
    //set Download Listener
    
    webView.setDownloadListener(new DownloadListener() {
    
    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);
    
    request.setDestinationInExternalPublicDir("timestop", "Tillem");
    
    DownloadManager dm = (DownloadManager) getSystemService(
    DOWNLOAD_SERVICE);
    
    dm.enqueue(request);
    
    
    }});
    }}
    

0 个答案:

没有答案