0

我有一个这样定义的 webview:

package pt.ini.mysite;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.provider.MediaStore;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.CookieManager;
import android.webkit.ValueCallback;
import android.net.Uri;
import android.view.WindowManager;

public class MainActivity extends AppCompatActivity {

    private WebView webView;
    private Context globalContext;

    /*private static final String TAG = MainActivity.class.getSimpleName();

    public static final int INPUT_FILE_REQUEST_CODE = 1;
    private ValueCallback mFilePathCallback;
    private String mCameraPhotoPath;*/

    private final static int FILECHOOSER_RESULTCODE = 1;
    private ValueCallback<Uri[]> mUploadMessage;


    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);

        pt.ini.mysite.CustomWebViewClient client = new pt.ini.mysite.CustomWebViewClient(this);

        webView = findViewById(R.id.webView);
        webView.setWebViewClient(client);
        
        webView.clearCache(true);
        webView.clearHistory();
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.getSettings().setUserAgentString(System.getProperty("http.agent") + "mysite");
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            WebView.setWebContentsDebuggingEnabled(true);
        }
        // Enable Cookies
        CookieManager.getInstance().setAcceptCookie(true);
        if(android.os.Build.VERSION.SDK_INT >= 21)
            CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);

        // Handle Popups
        //webView.setWebChromeClient(new CustomChromeClient());
        //webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        //webView.getSettings().setSupportMultipleWindows(true);
        //globalContext = this.getApplicationContext();
        
        webView.loadUrl("https://mysite.pt/?ref=android");


    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode == KeyEvent.KEYCODE_BACK && this.webView.canGoBack()){
            this.webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

class CustomWebViewClient extends WebViewClient{
    private Activity activity;

    public CustomWebViewClient(Activity activity){
         this.activity = activity;
    }
        //API level less than 24
    @Override
    public boolean shouldOverrideUrlLoading(WebView webView, String url){
        return false;
    }
            //API level upper 24
    @Override
    public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request){
        return false;
    }
}

我的目标是用户转到一个页面并使用 ResumableJS 提供的上传按钮,该按钮遵循使用我的 Android 应用程序的上传程序的标准实现。

至此,浏览正常,Cookies 和 XHR 请求也正常工作,但上传功能不起作用,代码如上。

通过浏览网页,有很多这样的例子:

webView.setWebChromeClient(new WebChromeClient() {
    public boolean onShowFileChooser(
        WebView webView, ValueCallback filePathCallback,
        WebChromeClient.FileChooserParams fileChooserParams) {
        if (mFilePathCallback != null) {
            mFilePathCallback.onReceiveValue(null);
        }
        mFilePathCallback = filePathCallback;

        /*Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
                takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
            } catch (IOException ex) {
                // Error occurred while creating the File
                Log.e("TAG", "Unable to create Image File", ex);
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {//"file:" +
                mCameraPhotoPath =  photoFile.getAbsolutePath();
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                                    Uri.fromFile(photoFile));
            } else {
                takePictureIntent = null;
            }
        }*/

        Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
        contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
        contentSelectionIntent.setType("image/*");

        Intent[] intentArray;
        //if (takePictureIntent != null) {
        //    intentArray = new Intent[]{takePictureIntent};
        //} else {
        intentArray = new Intent[0];
        //}

        Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
        chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
        chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);

        startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE);

        return true;
    }
});

上面的代码有点工作,因为应用程序打开相机拍照,但我不希望它这样做。我想从我的手机中选择一个文件,然后从中上传。

此外,带有“setWebChromeClient(new WebChromeClient() {”的应用程序的行为也很奇怪,因为它似乎正在打开一个新窗口(Chrome 浏览器)并访问我的网站,而我的另一个窗口(应用程序窗口)变为空白,因此强制我的用户再次登录。

如何更改我的代码以允许使用 Resumablejs 甚至标准输入文件上传文件?

API Level 26 及以上,但愿意使用其他级别。

到目前为止我研究过的内容:

  1. 上传按钮(带有 ResumableJS )不会触发文件浏览操作,甚至 html 文件输入也不会调用文件浏览操作。

  2. ResumableJS 上的以下问题表明通过 XHR 发布文件内容存在一些问题。

  3. 我发现这个示例几乎可以满足我的需要,但它会在使用 WebChromeClient 时打开一个 Chrome 窗口。

清单中的权限是什么:

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

有我可以尝试的样板代码吗?我只需要加载一个网站并允许从 sdcard 上传。

4

0 回答 0