2016-11-10 6 views
1

私は、Androidに新しいですが、私は、私はファイル選択ボタンを持っているのWebViewのアプリを開発しているし、それがlollipop.iで働いていないインストールMI5マシュマロ何も応答のAPKがAndroid LollipopでWebViewを使用してファイルをアップロードするにはどうすればよいですか?

これでコード来て、私を助けて

<?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:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="0dp" 
    android:paddingLeft="0dp" 
    android:paddingRight="0dp" 
    android:paddingTop="0dp" 
    tools:context="com.example.tamil.stagingsite.MainActivity"> 

    <ProgressBar 
     android:id="@+id/progressBar" 
     style="@android:style/Widget.Material.Light.ProgressBar.Large" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" /> 

    <WebView 
     android:id="@+id/activity_main_webview" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" 
     android:animationCache="true" 
     android:background="@android:color/white"> 

    </WebView> 
</RelativeLayout> 

MainActivity.java

:私はWebViewの

activity_main.xmlのオプションを有効にファイルアップロードを含める必要がある場所

package com.example.tamil.stagingsite; import android.app.Activity; import android.graphics.Bitmap; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.Window; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ProgressBar; import com.google.android.gms.appindexing.Action; import com.google.android.gms.appindexing.AppIndex; import com.google.android.gms.appindexing.Thing; import com.google.android.gms.common.api.GoogleApiClient; public class MainActivity extends Activity { private WebView mWebView; /** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroidStudio for more information. */ private GoogleApiClient client; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); mWebView = (WebView) findViewById(R.id.activity_main_webview); WebSettings webSettings = mWebView.getSettings(); webSettings.setJavaScriptEnabled(true); mWebView.loadUrl("http://census-staging.herokuapp.com/"); mWebView.setWebViewClient(new WebViewClient()); mWebView.setWebViewClient(new MyAppWebViewClient()); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); } @Override public void onBackPressed() { if (mWebView.canGoBack()) { mWebView.goBack(); } else { super.onBackPressed(); } } /** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroidStudio for more information. */ public Action getIndexApiAction() { Thing object = new Thing.Builder() .setName("Main Page") // TODO: Define a title for the content shown. // TODO: Make sure this auto-generated URL is correct. .setUrl(Uri.parse("http:staging.herokuapp.com/")) .build(); return new Action.Builder(Action.TYPE_VIEW) .setObject(object) .setActionStatus(Action.STATUS_TYPE_COMPLETED) .build(); } @Override public void onStart() { super.onStart(); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. client.connect(); AppIndex.AppIndexApi.start(client, getIndexApiAction()); } @Override public void onStop() { super.onStop(); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. AppIndex.AppIndexApi.end(client, getIndexApiAction()); client.disconnect(); } } 

答えて

0

このコードをsetwebchormeclinetで使用します。このため、この

webView.setWebChromeClient(new WebChromeClient() { 

     // openFileChooser for Android 3.0+ 
     public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType){ 

      // Update message 
      mUploadMessage = uploadMsg; 

      try{  

       // Create AndroidExampleFolder at sdcard 

       File imageStorageDir = new File(
             Environment.getExternalStoragePublicDirectory(
             Environment.DIRECTORY_PICTURES) 
             , "AndroidExampleFolder"); 

       if (!imageStorageDir.exists()) { 
        // Create AndroidExampleFolder at sdcard 
        imageStorageDir.mkdirs(); 
       } 

       // Create camera captured image file path and name 
       File file = new File(
           imageStorageDir + File.separator + "IMG_" 
           + String.valueOf(System.currentTimeMillis()) 
           + ".jpg"); 

       mCapturedImageURI = Uri.fromFile(file); 

       // Camera capture image intent 
       final Intent captureIntent = new Intent(
               android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

       captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI); 

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

       // Create file chooser intent 
       Intent chooserIntent = Intent.createChooser(i, "Image Chooser"); 

       // Set camera intent to file chooser 
       chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS 
             , new Parcelable[] { captureIntent }); 

       // On select image call onActivityResult method of activity 
       startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE); 

       } 
      catch(Exception e){ 
       Toast.makeText(getBaseContext(), "Exception:"+e, 
          Toast.LENGTH_LONG).show(); 
      } 

     } 

     // openFileChooser for Android < 3.0 
     public void openFileChooser(ValueCallback<Uri> uploadMsg){ 
      openFileChooser(uploadMsg, ""); 
     } 

     //openFileChooser for other Android versions 
     public void openFileChooser(ValueCallback<Uri> uploadMsg, 
            String acceptType, 
            String capture) { 

      openFileChooser(uploadMsg, acceptType); 
     } 

参考リンクはhere

+0

申し訳ショー多くのエラーは、私はあなたがビデオでこの例を表示することができ、この「MainActivy.java」を貼り付けることができますしようとしているときであるように? –

関連する問題