【问题标题】:Android: how can i access files on my phone from a webview file optionAndroid:如何通过 webview 文件选项访问手机上的文件
【发布时间】:2018-08-17 20:52:58
【问题描述】:

我正在开发一个允许人们从手机上传图像的 webview 应用程序,当我阅读 this post 时我更加困惑。

我注意到当我点击选择的文件按钮时...它不会打开我的画廊或访问我的文件。

这是我的代码:

    package com.benaija.benaija.Fragments;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.benaija.benaija.MainActivity;
import com.benaija.benaija.R;


/**
 * Created by Oto-obong on 10/01/2018.
 */

public class HomeFragement extends Fragment {

    private WebView webView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        webView = rootView.findViewById(R.id.webview);


        webView.setWebViewClient(new WebViewClient());

        webView.loadUrl("https://www.benaija.com");

        WebSettings webSettings = webView.getSettings();

        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(false);
        webSettings.setAllowFileAccess(true);
        webSettings.setAllowContentAccess(true);
        webSettings.setLoadsImagesAutomatically(true);


        return rootView;
    }




}

请问我该如何解决?

【问题讨论】:

  • @ViaTech ,链接没有解决问题,问题依旧
  • 我提供的更新后的示例是否满足了您的需求,或者您需要不同的组合?
  • 我还没有尝试过,很快就会尝试,但是我在更新之前得到的另一个示例允许我打开系统文件,但是当我选择一个文件时,我无法上传它
  • 好的,告诉我。它适用于我的示例,如果它不适用于您的网站:benaija.com,我可能需要您的更多代码来模仿您正在尝试做的事情......另外,根据您的示例上传应该发生在服务器上,而不是应用程序的 WebView 上。这是一个基于 Web 的上传,我的代码允许从 Web 应用程序选择本地文件进行上传,再次从服务器上传
  • @ViaTech 我可以把源代码发给你吗?

标签: android


【解决方案1】:

总体而言,您需要更改 WebView 的代码以允许设置 WebChromeClient,然后实现 onFileChooser() 方法以使 WebView 能够传达您的设备应该打开的信息按下 type="file" 输入按钮时系统的文件浏览器

我下面的代码会打开一个文件选择器,并允许从 WebView

中选择一个文件以通过网络表单上传

MainActivity.java

package com.viatechsystems.test;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;


public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
    }
}

HomeFragment.java

package com.viatechsystems.test;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import static android.app.Activity.RESULT_OK;

public class HomeFragment extends Fragment {

    private static final int INPUT_FILE_REQUEST_CODE = 1;
    private static final int FILECHOOSER_RESULTCODE = 1;
    private static final String TAG = MainActivity.class.getSimpleName();
    private WebView webView;
    private WebSettings webSettings;
    private ValueCallback<Uri> mUploadMessage;
    private Uri mCapturedImageURI = null;
    private ValueCallback<Uri[]> mFilePathCallback;
    private String filePath;

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

        if (requestCode != INPUT_FILE_REQUEST_CODE || mFilePathCallback == null) {
            super.onActivityResult(requestCode, resultCode, data);
            return;
        }else{
            Toast.makeText(getActivity().getApplicationContext(),
                    "Upload may take as long as you see this message.",
                    Toast.LENGTH_LONG).show();
            }

        Uri[] results = null;

        //check good response
        if (resultCode == Activity.RESULT_OK) {
            if (data == null) {
                if (filePath != null) {
                    results = new Uri[]{Uri.parse(filePath)};
                }
            } else {
                String dataString = data.getDataString();
                if (dataString != null) {
                    results = new Uri[]{Uri.parse(dataString)};
                }
            }
        }

        mFilePathCallback.onReceiveValue(results);
        mFilePathCallback = null;


        if (requestCode != FILECHOOSER_RESULTCODE || mUploadMessage == null) {
            super.onActivityResult(requestCode, resultCode, data);
            return;
        }

        Uri result = null;

        try {
            if (resultCode != RESULT_OK) {
                result = null;
            } else {
                // retrieve from the private variable if the intent is null
                result = data == null ? mCapturedImageURI : data.getData();
            }
        } catch (Exception e) {
            Toast.makeText(getActivity().getApplicationContext(),
                    "activity :" + e, Toast.LENGTH_LONG).show();
        }
        mUploadMessage.onReceiveValue(result);
        mUploadMessage = null;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        webView = rootView.findViewById(R.id.webview);

        webView.setWebViewClient(new WebViewClient());
        webView.setWebChromeClient(new ChromeClient());     //set with custom class below

        webView.loadUrl("https://files.fm/"); //changed so I could test a button

        WebSettings webSettings = webView.getSettings();

        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(false);
        webSettings.setAllowFileAccess(true);
        webSettings.setAllowContentAccess(true);
        webSettings.setLoadsImagesAutomatically(true);

        return rootView;
    }


    private class ChromeClient extends WebChromeClient {

        /*NOTE: method openFileChooser() is different for different Android Versions*/

        // 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("*/*");
            HomeFragment.this.startActivityForResult(
                    Intent.createChooser(i, "File Selection"),
                    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("*/*");
            HomeFragment.this.startActivityForResult(Intent.createChooser(i, "File Selection"),
                    HomeFragment.FILECHOOSER_RESULTCODE);

        }

        // For Android 5.0+ --- this is the method that will be called most of the time
        public boolean onShowFileChooser(WebView view,
                                         ValueCallback<Uri[]> filePath,
                                         WebChromeClient.FileChooserParams fileChooserParams) {

            // Double check that we don't have any existing callbacks
            if (mFilePathCallback != null) {
                mFilePathCallback.onReceiveValue(null);
            }
            mFilePathCallback = filePath;

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

            Intent[] intentArray = new Intent[0];

            Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
            chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
            chooserIntent.putExtra(Intent.EXTRA_TITLE, "Select Option:");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);

            startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE);

            return true;
        }
    }
}

fragment_home.xml - 布局资源

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</LinearLayout>

ma​​in_activity.xml - 布局资源

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <fragment android:name="com.viatechsystems.test.HomeFragment"
        android:id="@+id/home_fragment"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent" />

</LinearLayout>

【讨论】:

  • 效果很好,但是这段代码只支持棒棒糖版本的android吗?
  • 没有我的代码支持 Android 5.0 (lollipop) 及更高版本,较低版本的 Android 对 onActivityResult() 使用的代码略有不同,如果绝对需要,我可以显示,但是这些天他们很少使用
  • 不用担心,对不起,我的意思是 onShowFileChooser() 方法对于不同的 Android 版本是不同的,我已按照您的要求更新了我的代码。我不需要 onActivityResult() 中的 Lollipop 检查
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-23
  • 1970-01-01
  • 2015-09-11
  • 2014-01-14
  • 2015-08-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多