Android WebView上传文件名未设置正确

时间:2016-03-23 00:24:22

标签: android webview

我使用简单的WebView浏览具有上传文件格式的网站,其中包含来自stackovrflow的以下代码的表单字段:

private ValueCallback<Uri> mUploadMessage;
public ValueCallback<Uri[]> uploadMessage;
public static final int REQUEST_SELECT_FILE = 100;
private final static int FILECHOOSER_RESULTCODE = 1;

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    Log.i("ME", String.valueOf( requestCode));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        if (requestCode == REQUEST_SELECT_FILE) {
            if (uploadMessage == null)
                return;
            uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams
                    .parseResult(resultCode, intent));
            uploadMessage = null;
        }
    } else if (requestCode == FILECHOOSER_RESULTCODE) {
        if (null == mUploadMessage)
            return;
        // Use MainActivity.RESULT_OK if you're implementing WebView inside
        // Fragment
        // Use RESULT_OK only if you're implementing WebView inside an
        // Activity
        Uri result = intent == null || resultCode != RESULT_OK ? null
                : intent.getData();
        mUploadMessage.onReceiveValue(result);
        mUploadMessage = null;
        Log.i("ME", result.toString());
    } else
        Toast.makeText(getActivity().getApplicationContext(),
                "Failed to Upload Image", Toast.LENGTH_LONG).show();
}



private class MyWebChromeClient extends WebChromeClient {
    @Override
    public void onProgressChanged(WebView view, int progress) {
        // progressDialog.show();
        // progressDialog.setProgress(0);
        // activity.setProgress(progress * 1000);

        // progressDialog.incrementProgressBy(progress * 1000);

        progressBar.setProgress(progress);

        // if (progress == 100 && progressDialog.isShowing()) {
        // progressDialog.dismiss();
        // }

        // activity.setTitle("Loading...");
        // activity.setProgress(progress * 100); // Make the bar disappear
        // after URL is loaded
        // Log.i("ME", ""+progress);

        // Return the app name after finish loading
        if (progress == 100) {
            // activity.setTitle(R.string.app_name);
            loadingUrl = false;
            progressBar.setProgress(0);
            progressBar.setVisibility(View.GONE);
        }

    }

    // The undocumented magic method override
    // Eclipse will swear at you if you try to put @Override here
    // For Android 3.0+
    public void openFileChooser(ValueCallback<Uri> uploadMsg) {

        mUploadMessage = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");
        startActivityForResult(
                Intent.createChooser(i, "File Chooser"),
                FILECHOOSER_RESULTCODE);

    }

    // For Android 3.0+
    public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
        mUploadMessage = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("*/*");
        startActivityForResult(
                Intent.createChooser(i, "File Browser"),
                FILECHOOSER_RESULTCODE);
    }

    // For Android 4.1
    public void openFileChooser(ValueCallback<Uri> uploadMsg,
            String acceptType, String capture) {
        mUploadMessage = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");
        startActivityForResult(
                Intent.createChooser(i, "File Chooser"),
                FILECHOOSER_RESULTCODE);

    }

    // For Lollipop 5.0+ Devices
    public boolean onShowFileChooser(WebView mWebView,
            ValueCallback<Uri[]> filePathCallback,
            WebChromeClient.FileChooserParams fileChooserParams) {
        if (uploadMessage != null) {
            uploadMessage.onReceiveValue(null);
            uploadMessage = null;
        }

        uploadMessage = filePathCallback;

        Intent intent = fileChooserParams.createIntent();
        try {
            startActivityForResult(intent, REQUEST_SELECT_FILE);
        } catch (ActivityNotFoundException e) {
            uploadMessage = null;
            Toast.makeText(getActivity().getApplicationContext(),
                    "Cannot Open File Chooser", Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }

}

在Android 5.1上运行正常。在Android 4.x.x及更低版本上,它将文件上传到服务器正常,但文件名是这样的:

image%3A9121
20098
24964

虽然实际的文件名是这样的:

image.jpg
image.png
image.jpeg

为什么将图像文件名更改为数字等。

onActivityResult显示结果图像url [使用Log.i(“ME”,result.toString());]:

内容://com.android.providers.media.documents/documents/image%3A175458

该文件为.jpg,这是否意味着%3A175458在某些字符编码中引用“.jpg”。

1 个答案:

答案 0 :(得分:0)

使用库aFileChooser解决。

我刚刚使用该库中的文件FileUtils.java和LocalStorageProvider.java,并使用以下代码修改了onActivityResult:

if($contacttype -eq "1")