输入类型文件在android webview中无法工作浏览选项?

时间:2017-09-26 06:21:01

标签: java android

MainActivity.java代码:

package com.example.webrivers.controlmarketing;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends ActionBarActivity {
private WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myWebView = (WebView)findViewById(R.id.webView);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.loadUrl("http://domainname");
    myWebView.setWebViewClient(new WebViewClient());
 }

   @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    return false;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    return super.onOptionsItemSelected(item);
}
}

我在android 2.3.3版本中使用网站进行android转换,在android web视图中无法看到输入文件浏览选项。请给出任何想法。

1 个答案:

答案 0 :(得分:0)

您无法直接访问移动文件,您需要在清单中使用运行时权限

<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />

//这是您的代码Approprite更改将允许您从移动设备访问文件。

  package com.example.webrivers.controlmarketing;

  import android.support.v7.app.ActionBarActivity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.webkit.WebSettings;
  import android.webkit.WebView;
  import android.webkit.WebViewClient;

 public class MainActivity extends ActionBarActivity {
 private WebView myWebView;
 String FileString = "";

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myWebView = (WebView) findViewById(R.id.webView);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.loadUrl("http://domainname");
    myWebView.setWebViewClient(new WebViewClient());
    fixWebViewJSInterface(webView, new clsJScriptInterface(MainActivity.this), "app", "_gbjsfix");
}

@SuppressLint("JavascriptInterface")
public void fixWebViewJSInterface(WebView webview, Object jsInterface, String jsInterfaceName, String jsSignature) {
    webview.addJavascriptInterface(jsInterface, jsInterfaceName);

}

public class clsJScriptInterface {
    private final Context context;

    public clsJScriptInterface(Context paramContext) {
        this.context = paramContext;
    }

    @JavascriptInterface
    public String Uploadfile() {
    //This method will create in your js in website which will accepts string;
        try {

        } catch (Exception ex) {

            ex.printStackTrace();
        }

        return FileString;

    }


}


@Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);


    if (resultCode == RESULT_OK) {
        try {

    //here you can write code for fatch files form gallary and convert in appropriate format this will be your FileString to send to website.
        } catch (FileNotFoundException e) {
            e.printStackTrace();

        }

    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    return false;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    return super.onOptionsItemSelected(item);
  }
}