Android 2.3的网页视图文件上传问题视图、文件上传、网页、问题

2023-09-06 11:07:22 作者:无人及你

我使用的WebView运行我的Web应用程序,在我的应用程序,我允许用户上传图片到我的服务器,一切工作在Android> 3细,在Android 2.3.3浏览器,它也工作正常,但在web视图文件没有在和我的服务器填充越来越空的对象。它只是发生的事情,当我从相机拍摄照片(即正规画廊是工作的罚款)。我注意到的是乌里不同的一个与文件:///和一个与内容:///

I'm using webview to run my web application, In my app I'm allowing the user to upload images to my server, everything is working fine on android > 3, on Android 2.3.3 browser it also working as expected but on the webview the file is not populated in the and my server is getting an empty object. It is happening only when I'm taking the picture from the camera (i.e. regular gallery is working fine). I noticed the the Uri are different one with file:/// and one with content:///

附件是我的code:HTML(我的简单测试页面):

Attached is my code: HTML (my simple test page):

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
<form id="fuForm" name="fuForm" action="/questions/upload" method="POST" enctype="multipart/form-data">

    <label for="fileToUpload">Select a File to Upload</label><br />
    <input type="file" name="fileToUpload" id="fileToUpload"/>
    <input type="submit"/>
</form>
</body>
</html>

MainActivity code:

MainActivity code:

    webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setSavePassword(false);
    webView.getSettings().setSaveFormData(false);
    webView.setWebViewClient(new MyWebViewClient());
    webView.setWebChromeClient(new MyWebChromeViewClient());  
    webView.loadUrl(url);


    private class MyWebViewClient extends WebViewClient {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        }

        private class MyWebChromeViewClient extends WebChromeClient {
            //@Override
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType )  {      
                 File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyApp");
                // Create the storage directory if it does not exist
                if (! imageStorageDir.exists()){
                    imageStorageDir.mkdirs();                  
                }
                File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");  
                imageUri = Uri.fromFile(file); 

                final List<Intent> cameraIntents = new ArrayList<Intent>();
                final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                final PackageManager packageManager = getPackageManager();
                final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
                for(ResolveInfo res : listCam) {
                    final String packageName = res.activityInfo.packageName;
                    final Intent intent = new Intent(captureIntent);
                    intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
                    intent.setPackage(packageName);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                    cameraIntents.add(intent);
                }


                uploadMessage = uploadMsg; 
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);  
                intent.addCategory(Intent.CATEGORY_OPENABLE);  
                intent.setType("image/*"); 
                Intent chooserIntent = Intent.createChooser(intent,"Image Chooser");
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
                activity.startActivityForResult(chooserIntent,  FILECHOOSER_RESULTCODE); 
            }

   // For Android < 3.0
        @SuppressWarnings("unused")
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {
            openFileChooser(uploadMsg, "");
        }

    }

   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        // super.onActivityResult(requestCode, resultCode, intent);
        if (resultCode == RESULT_OK && requestCode == FILECHOOSER_RESULTCODE) {
            if (null == uploadMessage)
                return;
            final boolean isCamera;
            if (intent == null) {
                isCamera = true;
            } else {
                final String action = intent.getAction();
                if (action == null) {
                    isCamera = false;
                } else {
                    isCamera = action
                            .equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                }
            }
            Uri result;
            if (isCamera) {
                result = imageUri;
            } else {
                result = intent == null ? null : intent.getData();
            }
            uploadMessage.onReceiveValue(result);
        } else {
            uploadMessage.onReceiveValue(null);
        }
        uploadMessage = null;
    }

任何帮助将AP preciated ...

Any help will be appreciated...

乔。

推荐答案

我遇到了同样的问题,但我发现很长一段时间的研究后,由我自己的解决方案。

I met the same problem, but I have found a solution by myself after a long time research.

在Android 2.3.3,web视图不与文件协议支持的URI(文件:///)。我们应该图像的URI转换为内容的协议的URI(内容:///)。调用uploadMessage.onReceiveValue.The varaiable imageFilePath下code是由摄像机拍摄到的图像文件的绝对路径之前

On the Android 2.3.3, the webview doesn’t support the Uri with file protocol (file:///). We should convert the image’s Uri to a content protocol Uri (content:///) before calling uploadMessage.onReceiveValue.The varaiable imageFilePath in below code is the absolute path of the image file captured by camera.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    // super.onActivityResult(requestCode, resultCode, intent);
    if (resultCode == RESULT_OK && requestCode == FILECHOOSER_RESULTCODE) {
        if (null == uploadMessage)
            return;
        final boolean isCamera;
        if (intent == null) {
            isCamera = true;
        } else {
            final String action = intent.getAction();
            if (action == null) {
                isCamera = false;
            } else {
                isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            }
        }
        Uri result;
        if (isCamera) {
            **String mediaUri =MediaStore.Images.Media.insertImage(getContentResolver(),imageFilePath,"","");
            result = Uri.parse(mediaUri);**
        } else {
            result = intent == null ? null : intent.getData();
        }
        uploadMessage.onReceiveValue(result);
    } else {
        uploadMessage.onReceiveValue(null);
    }
    uploadMessage = null;
}