使用webview浏览照片库

时间:2015-02-24 05:46:01

标签: android webview

Min SDK API 15

您好,

我使用的网页视图上有一个按钮,可以浏览应用相片库。

但是,在webview中单击按钮时,没有任何反应。

网址格式为:

https://www.xxxxxxxxxxx

我添加了以下权限:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" android:required="false" />

我有一个片段,可以在启用了javascript的onCreateView方法(仅限代码段)中加载webview。

if(!message.isEmpty()) {            
    WebView webView = (WebView)view.findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.setWebViewClient(new WebViewClient());
    webView.loadUrl(message);
}

我创建了一个简单的html页面来测试:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test Android Popup</title>
</head>
<body>
<label>Test Alert 1:</label>
<button type="button" onClick="alert('Test Alert Box1');">Click Me!</button>
<br>
<label>Test Browse file</label>
<input type="file" name="img">
</body>
</html>

因此url将被加载到webview中。 webview显示一个按钮,用户将单击该按钮浏览其库中的照片。

webview看起来像这样: enter image description here

单击它们时,没有任何按钮可用。

非常感谢任何建议,

此代码段适用于&lt; 4.3。但是,4.4和5.0失败了。

 webView.setWebChromeClient(new WebChromeClient() {
            /* Open File */
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
                mImageFilePath = uploadMsg;

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

                startActivityForResult(intent, FILECHOOSER_RESULTCODE);
            }


            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
                mImageFilePath = uploadMsg;

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

                startActivityForResult(intent, FILECHOOSER_RESULTCODE);
            }

        });

4 个答案:

答案 0 :(得分:6)

创建此文件并将其放在资源文件夹中: webdemo.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>title</title>
</head>
<body>
<h1>Web View Demo</h1>
<br>
    <input type="button" value="Say hello"
        onClick="showAndroidToast('Hello Android!')" />
    <br />
    File Uri: <label id="lbluri">no file uri</label>
    <br />
    File Path: <label id="lblpath">no file path</label>
    <br />
    <input type="button" value="Choose Photo" onClick="choosePhoto()" />
    <script type="text/javascript">
        function showAndroidToast(toast) {
            Android.showToast(toast);
        }
        function setFilePath(file) {
            document.getElementById('lblpath').innerHTML = file;
            Android.showToast(file);
        }
        function setFileUri(uri) {
            document.getElementById('lbluri').innerHTML = uri;
            Android.showToast(uri);
        }
        function choosePhoto() {
            var file = Android.choosePhoto();
            window.alert("file = " + file);
        }
    </script>
</body>
</html>

专注于这里写的javascript。

按以下方式编写您的活动: 的 WebViewDemo.java

public class WebViewDemo extends Activity
{

    private WebView webView;

    final int SELECT_PHOTO = 1;

    @SuppressLint("SetJavaScriptEnabled")
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_web_view);

        webView = (WebView) findViewById(R.id.webView1);

        webView.getSettings().setJavaScriptEnabled(true);

        webView.getSettings().setLoadWithOverviewMode(true);

        // Other webview settings
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.setScrollbarFadingEnabled(false);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setPluginState(PluginState.ON);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setSupportZoom(true);
        webView.addJavascriptInterface(new MyJavascriptInterface(this), "Android");

        webView.loadUrl("file:///android_asset/webdemo.html");

    }

    class MyJavascriptInterface
    {

        Context mContext;

        /** Instantiate the interface and set the context */
        MyJavascriptInterface(Context c)
        {
            mContext = c;
        }

        /** Show a toast from the web page */
        @JavascriptInterface
        public void showToast(String toast)
        {
            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        }

        @JavascriptInterface
        public String choosePhoto()
        {
            // TODO Auto-generated method stub
            String file = "test";
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, SELECT_PHOTO);
            return file;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent)
    {
        switch (requestCode)
        {
        case SELECT_PHOTO:
            if (resultCode == RESULT_OK)
            {
                Uri selectedImage = intent.getData();
                webView.loadUrl("javascript:setFileUri('" + selectedImage.toString() + "')");
                String path = getRealPathFromURI(this, selectedImage);
                webView.loadUrl("javascript:setFilePath('" + path + "')");
            }
        }

    }

    public String getRealPathFromURI(Context context, Uri contentUri)
    {
        Cursor cursor = null;
        try
        {
            String[] proj = { MediaStore.Images.Media.DATA };
            cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        finally
        {
            if (cursor != null)
            {
                cursor.close();
            }
        }
    }

}

使用此代码段并查看它是否有效。

  

以下屏幕截图取自Android 4.4 Kitkat设备。

see this screen shot

我不知道这个解决方案是否有效。对您而言,这可能只是一种解决方法或突破。 我希望它的某些部分会有所帮助。

答案 1 :(得分:5)

不幸的是,正如用户Slartibartfast在您的问题的一条评论中所说,输入类型&#34; file&#34;不适用于4.4设备。 Google代码上有issue表示这是预期的行为(状态:WorkingAsIntended)。

即便如此,您提供的代码也适用于4.4.4(在Nexus 7平板电脑上测试)和&lt; 4.4 os版本。

在5.0上,他们添加了一个记录在案的method来执行此操作。我使用以下代码:

mWebView.setWebChromeClient(new WebChromeClient() {
    // For Android < 3.0 - undocumented method
    @SuppressWarnings("unused")
    public void openFileChooser( ValueCallback<Uri> uploadMsg ) {
        openFileChooser( uploadMsg, "" );
    }

    // For Android 3.0+ - undocumented method
    public void openFileChooser( ValueCallback<Uri> uploadMsg, String acceptType ) {                
        mUploadCallback = uploadMsg;
        openFileChooserActivity();
    }

    // For Android > 4.1 - undocumented method
    @SuppressWarnings("unused")
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
        openFileChooser( uploadMsg, "" );
    }

    // For Android > 5.0
    public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
        mUploadCallbackLollipop = filePathCallback;
        openFileChooserActivity();
        return true;
    }
});

记下&gt;更改的回调参数5.0:从ValueCallback<Uri>ValueCallback<Uri[]>

因此,对于&gt; = 4.4和&lt; 4.4.4,您应该实现自己的文件上传机制。一些建议:

  • 用javascript检查android os版本并提供<input type="file">它的工作原理。在4.4版本中,使用您自己的url架构创建一个html元素(如Sushant所说,例如<form action="image://choose">)。然后,您可以在shouldOverrideUrlLoading中捕获它,并使用文件路径从java调用javascript函数,但您无法访问文件的内容。对于小文件,您可以将base64中的文件内容作为参数传递给javascript函数,但对于较大的文件,您肯定会获得OutOfMemory异常。
  • 您可以执行上述步骤,但在java中处理文件上传,并仅提供javascript函数的路径。提交的按钮可以使用Amrut Bidri所说的触发您在java上传的内容。

因此,作为结论,我看到了4.4和4.4.4之间的os版本的两个选项:使用javascript上传文件(内存问题)或者在java中上传它并在你的app和你之间实现你自己的通信机制网页流量。对于他们两个,您需要访问您在webview中加载的网页。

我使用了javascript上传方法,因为我们正在使用小文件,实施起来要快一些:

public boolean shouldOverrideUrlLoading(WebView view, String url) {         
    ... else if (url.startsWith(UPLOAD_FILE_URL_PREFIX)) {
        openFileChooserActivity();
        return true;
    }

    return false;
}

在活动结果上,我为4.4版本做了类似的事情(javascript函数insertFileForUpload处理文件上传):

private void invokeJavascriptWithFileContent(Uri fileUri) {
    String fileContentInBase64 = readAndConvertFileToBase64(fileUri);

    if (!fileContentInBase64.isEmpty()) {
        String fileMimeType = getMimeType(activity, fileUri);
        String fileName = getFileName(activity, fileUri);

        // invoke javascript
        String js = String.format("javascript:insertFileForUpload(\"%s\",\"%s\",\"%s\")", 
                fileName,
                fileMimeType,
                fileContentInBase64
                );
        mWebView.loadUrl(js);
    }
}

public static String getMimeType(Context context, Uri fileUri)    
{
    ContentResolver cR = context.getContentResolver();
    MimeTypeMap mime = MimeTypeMap.getSingleton();
    String type = mime.getExtensionFromMimeType(cR.getType(fileUri));

    return type;
}

此外,关于https://github.com/delight-im/Android-AdvancedWebView,文件上传似乎有效,但他们说:&#39;文件上传是自动处理的(使用AdvancedWebView.isFileUploadAvailable()检查可用性)&#39;和&#39; isFileUploadAvailable&#39;包含以下代码:

/**
 * Returns whether file uploads can be used on the current device (generally all platform versions except for 4.4)
 *
 * @return whether file uploads can be used
 */
public static boolean isFileUploadAvailable() {
    return isFileUploadAvailable(false);
}

/**
 * Returns whether file uploads can be used on the current device (generally all platform versions except for 4.4)
 *
 * On Android 4.4.3/4.4.4, file uploads may be possible but will come with a wrong MIME type
 *
 * @param needsCorrectMimeType whether a correct MIME type is required for file uploads or `application/octet-stream` is acceptable
 * @return whether file uploads can be used
 */
public static boolean isFileUploadAvailable(final boolean needsCorrectMimeType) {
    if (Build.VERSION.SDK_INT == 19) {
        final String platformVersion = (Build.VERSION.RELEASE == null) ? "" : Build.VERSION.RELEASE;

        return !needsCorrectMimeType && (platformVersion.startsWith("4.4.3") || platformVersion.startsWith("4.4.4"));
    }
    else {
        return true;
    }
}

因此,您可能会遇到与普通网页浏览相同的问题,但我还没有对图书馆进行测试,因此我无法说出来。

我可能创造了一个混乱的答案,但我希望你能找到有用的东西。

答案 2 :(得分:2)

嗯,你可以像这样实现它

    webView.setWebViewClient(new WebViewClient() {


        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.equals("alert://alert")) {
                Toast.makeText(this, "alert", Toast.LENGTH_LONG).show();
            } else if (url.equals("choose://image")) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("image/*");

                startActivityForResult(intent, FILECHOOSER_RESULTCODE);
            }
            return true;
        }
    });

你的html应该是这样的

<!doctype html>
<html>
    <head>
    <meta charset="utf-8">
    <title>Test Android Popup</title>
    </head>
    <body>
        <label>Test Alert 1:</label>
        <form action="alert://alert">
             <input type="submit" value="Click me!">
        </form>
        <br>
        <label>Test Browse file</label>
        <form action="image://choose">
            <input type="submit" value="Choose File">
        </form>
    </body>
</html>

答案 3 :(得分:0)

通过这段代码可以采取移动文件的路径,只是我无法上传php中的文件。下面是我正在使用的php代码:

$arquivo = $_POST['lblpath'];

$url = "file:///storage/external_SD/fotos/IMG10022.jpg"; //url da imagem que venha de um textField

//PEGA INFORMAÇÕES DA IMAGEM
$file_info = getimagesize($url);    


//VERIFICA EXTENSÃO DA IMAGEM
if ($file_info['mime'] == "image/jpeg")
$img = imagecreatefromjpeg($url);
else if ($file_info['mime'] == "image/gif")
$img = imagecreatefromgif($url);
else if ($file_info['mime'] == "image/png")
$img = imagecreatefrompng($url);

$altura = $file_info[1];

//PASSA AS MEDIDAS PARA A IMAGEM
$x   = imagesx($img);
$y   = imagesy($img);
$largura = ($altura * $x)/$y;


//CRIA A IMAGEM
$nova = imagecreatetruecolor($largura, $altura);
imagealphablending( $nova, false );
imagesavealpha( $nova, true );
imagecopyresampled($nova, $img, 0, 0, 0, 0, $largura, $altura, $x, $y);


//NOME DA IMAGEM
$imgName = end(explode("/", $url));

//LOCAL PARA SALVAR
$pasta = "images/"; // defina sua path
$local = $pasta . $imgName;


//SALVA NOVA IMAGEM
if ($file_info['mime'] == "image/jpeg")
imagejpeg($nova, $local, 100);
else if ($file_info['mime'] == "image/png")
imagepng($nova, $local, 9);

//DESTROI ELEMENTOS
imagedestroy($img);
imagedestroy($nova);