在webview上的Facebook上的文件选择器

时间:2018-02-28 15:33:44

标签: java android webview

我有一个webview,可以加载包含Facebook评论插件的网页。 "附加图片" 选项在评论中启用,但在网页视图中不起作用。是否可以通过选择图库中的图像并在webview中发布来执行此操作?

我在Manifest中添加了此权限:

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

这是我的代码 Site.class

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;


public class Site extends Activity {
    private static final String TAG = Site.class.getSimpleName();


    protected WebView mainWebView;
    private Context mContext;
    private WebView mWebviewPop;
    private FrameLayout mContainer;

    private String url= "http://example.com";


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



        mContainer = (FrameLayout) findViewById(R.id.container);
        mContext = this.getApplicationContext();


        // if you want to make sure that we need to log in facebook again ...
//        clearCookies(this);

        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);


        mainWebView = getWebView(mContext);

        // handle the opening of facebook
        mainWebView.setWebChromeClient(new MyCustomChromeClient());

        mainWebView.loadUrl(url);

        mContainer.addView(mainWebView);


    }


    /**
     * get webview with all necessary properties to support facebook login
     */
    WebView getWebView(Context context) {
        WebView wv = new WebView(context);
        wv.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));

        wv.clearCache(true);

        // Settings for the webview
        if (isOnline()) {
        WebSettings webSettings = wv.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setSupportMultipleWindows(true);

        if (Build.VERSION.SDK_INT >= 21) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
            CookieManager.getInstance().setAcceptThirdPartyCookies(wv, true);
        }
        } else {
            /* deve cair aqui caso não haja internet */
            Intent i = new Intent(Blog.this, Error.class);
            startActivity(i);
        }


        return wv;
    }
    public boolean isOnline() {
        ConnectivityManager cm =
                (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        return netInfo != null && netInfo.isConnectedOrConnecting();
    }


    private class MyCustomChromeClient extends WebChromeClient {

        @Override
        public boolean onCreateWindow(WebView view, boolean isDialog,
                                      boolean isUserGesture, Message resultMsg) {
            mWebviewPop = getWebView(mContext);
            mWebviewPop.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    String host = Uri.parse(url).getHost();
                    Log.d(TAG, "host=" + host);
                    if (host.contains("facebook.com")) {
                        view.loadUrl(url);
                        return true;
                    }

                    // Otherwise, the link is not for a page on my site, so launch
                    // another Activity that handles URLs
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(intent);
                    return false;
                }
            });
            mWebviewPop.setWebChromeClient(new MyCustomChromeClient());

            mContainer.addView(mWebviewPop);

            WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
            transport.setWebView(mWebviewPop);
            resultMsg.sendToTarget();

            return true;
        }

        @Override
        public void onCloseWindow(WebView window) {
            // facebook login finishes, close the facebook window and return to the main one
            Log.d("onCloseWindow", "called");

            mWebviewPop.setVisibility(View.GONE);
            mContainer.removeView(mWebviewPop);
            mWebviewPop = null;

        }
    }

    @SuppressWarnings("deprecation")
    public static void clearCookies(Context context) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            Log.d(TAG, "Using clearCookies code for API >=" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1));
            CookieManager.getInstance().removeAllCookies(null);
            CookieManager.getInstance().flush();
        } else {
            Log.d(TAG, "Using clearCookies code for API <" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1));
            CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(context);
            cookieSyncMngr.startSync();
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.removeAllCookie();
            cookieManager.removeSessionCookie();
            cookieSyncMngr.stopSync();
            cookieSyncMngr.sync();
        }
    }
}

0 个答案:

没有答案