如何通过webview上传多个文件?

时间:2019-04-04 14:41:29

标签: java android webview

我已经用Webview创建了一个项目来在移动应用程序中调用我的网站。 (在Android App / Java中完成)。 在网站上工作时,我可以上传多个文件,但不能选择多个文件,只能选择一个文件。 我已经尝试了stackoverflow中提供的许多编码,但是无法解决我的问题。 请帮助我激活此功能。我是应用程序创建的新手,请指导我在哪里添加和添加什么。 下面我给出了编码

我尝试了optnfiechooser选项, intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true)和其他代码。

MainActivity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
    super.onActivityResult(requestCode, resultCode, intent);
    if (Build.VERSION.SDK_INT >= 21) {
        Uri[] results = null;
        //Check if response is positive
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == FCR) {
                if (null == mUMA) {
                    return;
                }
                if (intent == null) {
                    //Capture Photo if no image available
                    if (mCM != null) {
                        results = new Uri[]{Uri.parse(mCM)};
                    }
                } else {
                    String dataString = intent.getDataString();
                    if (dataString != null) {
                        results = new Uri[]{Uri.parse(dataString)};
                    }

                }
            }
        }
        mUMA.onReceiveValue(results);
        mUMA = null;
    } else {
        if (requestCode == FCR) {
            if (null == mUM) return;
            Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
            mUM.onReceiveValue(result);
            mUM = null;
        }
    }
}


private File createImageFile() throws IOException
{
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "img_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    return File.createTempFile(imageFileName, ".jpg", storageDir);
}


public void onClick_GoBack(View v)
{
    if (wv.canGoBack())
        wv.goBack();
}


public void onClick_Home(View v)
{
    wv.loadUrl(MY_SITE_HOME);
}


public void onClick_Info(View v)
{
    wv.loadUrl(MY_SITE_INFO);
}

内部类

class MyWebChromeClient extends WebChromeClient
{
    @Override
    public void onProgressChanged(WebView view, int newProgress)
    {
        super.onProgressChanged(view, newProgress);

        loadingProgressBar.setProgress(newProgress);

        txtState.setText(Integer.toString(newProgress) + "%");
        if (newProgress >= 100)
        {
            loadingProgressBar.setVisibility(View.INVISIBLE);
            txtState.setVisibility(View.INVISIBLE);
        }

        if (newProgress <= 10)
        {
            loadingProgressBar.setVisibility(View.VISIBLE);
            txtState.setVisibility(View.VISIBLE);
        }
    }


    @Override


    public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams)

    {

        if (mUMA != null)
        {
            mUMA.onReceiveValue(null);
        }
        mUMA = filePathCallback;
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null)
        {
            File photoFile = null;
            try {
                photoFile = createImageFile();
                takePictureIntent.putExtra("PhotoPath", mCM);
            } catch (IOException ex) {
                Log.e(TAG, "Image file creation failed", ex);
            }
            if (photoFile != null) {
                mCM = "file:" + 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("*/*");
        Intent[] intentArray;
        if (takePictureIntent != null) {
            intentArray = new Intent[]{takePictureIntent};
        } else {
            intentArray = new Intent[0];
        }


    }


}





class MyWebViewClient extends WebViewClient
{

    @Override
    public void onPageStarted(WebView view,String url,Bitmap favicon)
    {
        super.onPageStarted(view,url,favicon);
    }


    @Override
    public boolean shouldOverrideUrlLoading(WebView view,String Url)
    {
        view.loadUrl(Url);
        return true;
    }


    @Override
    public void onPageFinished(WebView view,String url)
    {
        super.onPageFinished(view,url);
    }
}
}

0 个答案:

没有答案