Android WebView文件上传在三星设备上无法正常运行

时间:2016-03-08 08:13:06

标签: android file-upload webview

我们已经创建了一个将在Android WebView中加载的Web应用程序。它为最终用户提供图像上传功能。我已经实现了提供文件上传功能的所有必要方法。我们正在将文件上传到AWS S3服务器。它适用于大多数手机。从Samsung GT-P1300 7英寸标签(Jelly Bean - 4.1.2)上传图像时,我遇到了一个奇怪的问题。

如果您通过Media Store内容提供商上传存储在外部存储上的图像。服务器接收大小为0字节的空对象。 例如, content:// media / external / images / media / 3064

其中,如果您从其他内容提供商上传图像。它没有任何问题。例如, content://com.sec.android.gallery3d.provider/picasa/item/6228047309682110642

以下是我所面临问题的更多细节。

以下路径有效!文件通过WebView上传。

图片URI :content://com.sec.android.gallery3d.provider/picasa/item/6228047309682110642

权限:com.sec.android.gallery3d.provider

以下路径不起作用!服务器获取空文件对象。

图片URI :content:// media / external / images / media / 3064

权限:媒体

这是我到目前为止实施的代码。

  

WebViewActivity.Java

package com.mine.activities;

import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.mine.R;
import com.mine.utility.AppConstants;

import java.io.File;
import java.net.CookieHandler;
import java.net.HttpCookie;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

public class WebViewActivity extends AppCompatActivity {

private WebView webView = null;
private ProgressBar progressBar;
private ValueCallback<Uri> mFileUploadCallbackFirst;
private ValueCallback<Uri[]> mFileUploadCallbackSecond;
private File uploadedFile = null;

private final static int FILECHOOSER_RESULTCODE=51426;
public static final int INPUT_FILE_REQUEST_CODE = 1;

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

    setContentView(R.layout.activity_web_view);

    progressBar = (ProgressBar) findViewById(R.id.pb_load_page);
    webView = (WebView) findViewById(R.id.webview);

    webView.setListener(this, this);
    setUpWebViewDefaults(webView);

    webView.loadUrl("http://some.server.com/page");
}

/**
 * Convenience method to set some generic defaults for a
 * given WebView
 *
 * @param webView
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setUpWebViewDefaults(WebView webView) {
    WebSettings settings = webView.getSettings();

    // Enable Javascript
    settings.setJavaScriptEnabled(true);

    // Use WideViewport and Zoom out if there is no viewport defined
    settings.setUseWideViewPort(true);
    settings.setLoadWithOverviewMode(true);

    settings.setAllowFileAccess(true);

    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        settings.setAllowFileAccessFromFileURLs(true);
        settings.setAllowUniversalAccessFromFileURLs(true);
    }

    // Enable pinch to zoom without the zoom buttons
    settings.setBuiltInZoomControls(true);

    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
        // Hide the zoom controls for HONEYCOMB+
        settings.setDisplayZoomControls(false);
    }

    // Enable Caching
    enableHTML5AppCache(webView);

    webView.setWebChromeClient(new WebChromeClient() {

        // file upload callback (Android 2.2 (API level 8) -- Android 2.3 (API level 10)) (hidden method)
        @SuppressWarnings("unused")
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {
            openFileChooser(uploadMsg, null);
        }

        // file upload callback (Android 3.0 (API level 11) -- Android 4.0 (API level 15)) (hidden method)
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
            openFileChooser(uploadMsg, acceptType, null);
        }

        // file upload callback (Android 4.1 (API level 16) -- Android 4.3 (API level 18)) (hidden method)
        @SuppressWarnings("unused")
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
            openFileInput(uploadMsg, null);
        }

        // file upload callback (Android 5.0 (API level 21) -- current) (public method)
        @SuppressWarnings("all")
        public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
            openFileInput(null, filePathCallback);
            return true;
        }

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            progressBar.setProgress(newProgress);
        }
    });
}

@SuppressLint("NewApi")
protected void openFileInput(final ValueCallback<Uri> fileUploadCallbackFirst, final ValueCallback<Uri[]> fileUploadCallbackSecond) {
    if (mFileUploadCallbackFirst != null) {
        mFileUploadCallbackFirst.onReceiveValue(null);
    }
    mFileUploadCallbackFirst = fileUploadCallbackFirst;

    if (mFileUploadCallbackSecond != null) {
        mFileUploadCallbackSecond.onReceiveValue(null);
    }
    mFileUploadCallbackSecond = fileUploadCallbackSecond;

    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.addCategory(Intent.CATEGORY_OPENABLE);
    i.setType(mUploadableImageFileTypes);

    startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
}

private void enableHTML5AppCache(WebView webView) {

    webView.getSettings().setDomStorageEnabled(true);

    // Set cache size to 8 mb by default. should be more than enough
    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
        webView.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
    }

    webView.getSettings().setAppCachePath(getCacheDir().getAbsolutePath());
    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setAppCacheEnabled(true);

    webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
}

public void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
    if (requestCode == FILECHOOSER_RESULTCODE) {
        if (resultCode == Activity.RESULT_OK) {
            if (intent != null) {
                if (mFileUploadCallbackFirst != null) {
mFileUploadCallbackFirst.onReceiveValue(intent.getData());

                    mFileUploadCallbackFirst = null;
                }
                else if (mFileUploadCallbackSecond != null) {
                    Uri[] dataUris;
                    try {
                        dataUris = new Uri[] { Uri.parse(intent.getDataString()) };
                    }
                    catch (Exception e) {
                        dataUris = null;
                    }

                    mFileUploadCallbackSecond.onReceiveValue(dataUris);
                    mFileUploadCallbackSecond = null;
                }
            }
        }
        else {
            if (mFileUploadCallbackFirst != null) {
                mFileUploadCallbackFirst.onReceiveValue(null);
                mFileUploadCallbackFirst = null;
            }
            else if (mFileUploadCallbackSecond != null) {
                mFileUploadCallbackSecond.onReceiveValue(null);
                mFileUploadCallbackSecond = null;
            }
        }
    }
}
}
  

activity_web_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.djinie.vendormobileapp.VendorMobileActivity">

<ProgressBar
    android:id="@+id/pb_load_page"
    style="@style/CustomProgressBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:max="100"
    android:progress="10"/>

<WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:layout_below="@+id/pb_load_page" />
</RelativeLayout>

任何帮助将不胜感激。如果您需要更多信息,请与我们联系。

0 个答案:

没有答案