如何从webview保存图像?

时间:2019-02-17 05:56:40

标签: java android webview

是的,我在这个网站上尝试了很多答案,但所有答案都不起作用  我有一个webview应用程序,我希望将图像保存在网站中。  我已执行长按以在应用程序中复制文本。 但长按则无法保存图像。 这些关于上下文的答案不起作用

这是我的主要活动代码,任何人都可以编辑并回答我 并请记住,我只用Java编写了一些基本代码,而我使用不同来源的代码制作了该应用的大部分内容

我的主要活动:

package com.example.ragulsundaram.typicaltamilan;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.DownloadManager;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.URLUtil;
import android.webkit.ValueCallback;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.CookieManager;
import java.net.HttpURLConnection;
import java.net.URL;


public class MainActivity extends AppCompatActivity {

    WebView mWebView;
    SwipeRefreshLayout swipe;
    private long backPressedTime;
    private static final String TAG = "MainActivity";
    public static int TIME_OUT=4000;




    @Override
    public void onBackPressed() {
        if(backPressedTime + 2000 > System.currentTimeMillis()){
            super.onBackPressed();
            return;
        }else{
            Toast.makeText(getBaseContext(),"Press back again to exit,",Toast.LENGTH_SHORT).show();
        }
        backPressedTime = System.currentTimeMillis();
    }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction()== KeyEvent.ACTION_DOWN){

            switch (keyCode){

                case KeyEvent.KEYCODE_BACK:
                    if (mWebView.canGoBack()){

                        mWebView.goBack();
                    }
                    else {

                        if(backPressedTime + 2000 > System.currentTimeMillis()){
                            super.onBackPressed();

                        }else{
                            Toast.makeText(getBaseContext(),"Press back again to exit",Toast.LENGTH_SHORT).show();
                        }
                        backPressedTime = System.currentTimeMillis();
                    }
                    return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onStop() {
        Toast.makeText(getApplicationContext(),"Pogatheeey",Toast.LENGTH_SHORT).show();
        super.onStop();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast.makeText(getApplicationContext(),"Vankakkam",Toast.LENGTH_LONG).show();



        swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
        {
            public void onRefresh(){
                LoadWeb();
            }
        });

        LoadWeb();
        if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.M){
            if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)==PackageManager.PERMISSION_DENIED){
                String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
                requestPermissions(permissions,1);
            }
        }
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mWebView.reload();

            }
        });

    }





    public void LoadWeb()
    {
        mWebView = (WebView) findViewById(R.id.webView);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setAppCacheEnabled(true);
        mWebView.loadUrl("https://typicaltamila.blogspot.com/   ");
        swipe.setRefreshing(true);
        mWebView.evaluateJavascript("(function(){return window.getSelection().toString()})()",
                new ValueCallback<String>()
                {
                    @Override
                    public void onReceiveValue(String value)
                    {
                        Log.v(TAG, "SELECTION:" + value);
                    }
                });
        mWebView.setWebViewClient(new WebViewClient() {



            public void onReveivedError(WebView view, int errorCode, String description, String failingUrl){
                mWebView.loadUrl("file://android_asset/error.html");
            }





            public void onPageFinished(WebView view, String url)
            {
                //hide the swipe refreshlayout
                swipe.setRefreshing(false);
            }
            @SuppressWarnings("deprecation")
            @Override

            public boolean shouldOverrideUrlLoading(WebView webView, String url) {

                if (url.startsWith("magnet")) {

                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(url));
                    try {
                        startActivity(i);
                    } catch (ActivityNotFoundException e) {
                        Toast.makeText(getBaseContext(), "Team localpeers:  No bittorent client installed in system.", Toast.LENGTH_SHORT).show();
                    }
                    return true;
                }

                return false;
            }
            //torrent
            @TargetApi(Build.VERSION_CODES.N)
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                String url=request.getUrl().toString();
                if (url.startsWith("magnet")) { Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(url));
                    try {
                        startActivity(i);
                    } catch (ActivityNotFoundException e) {
                        Toast.makeText(getBaseContext(), "Team localpeers:  No bittorent client installed in Android.", Toast.LENGTH_SHORT).show();
                    }
                    return true;
                }

                return false;
            }




        });

//downloading files

        mWebView.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
                DownloadManager.Request request= new DownloadManager.Request(Uri.parse(url));
                request.setMimeType(mimetype);
                String cookies = android.webkit.CookieManager.getInstance().getCookie(url);
                request.addRequestHeader("cookie",cookies);
                request.addRequestHeader("User-Agent",userAgent);
                request.setDescription(("Downloading"));
                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,URLUtil.guessFileName(url,contentDisposition,mimetype));
                DownloadManager dm=(DownloadManager)getSystemService(DOWNLOAD_SERVICE);
                dm.enqueue(request);
                Toast.makeText(MainActivity.this,"Your file is downloading",Toast.LENGTH_SHORT).show();




            }
        });

    }

}

0 个答案:

没有答案