2012-07-30 17 views
28

मैं एक एंड्रॉइड ऐप विकसित कर रहा हूं। असल में यह WebView और प्रगतिबार है। फेसबुक की मोबाइल साइट (m.facebook.com) WebView में लोड की गई है।एंड्रॉइड वेबव्यू फ़ाइल अपलोड करें

जब मैं कोई छवि अपलोड करने के लिए फ़ाइल चुनें बटन पर क्लिक करता हूं, तो कुछ भी नहीं होता है। मैंने सभी समाधानों का प्रयास किया है और उनमें से कोई भी काम नहीं करता है। मैं एक गैलेक्सी नोट (जीटी-एन 7000) पर 4.0.3 चल रहा हूं। मेरे न्यूनतम SDK संस्करण संस्करण 8.

My App

यहाँ अधिक जानकारी के लिए मेरी कोड है ...

public class IStyla extends Activity { 
    private ValueCallback<Uri> mUploadMessage; 
    private final static int FILECHOOSER_RESULTCODE = 1; 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 
     if (requestCode == FILECHOOSER_RESULTCODE) { 
      if (null == mUploadMessage) 
       return; 
      Uri result = intent == null || resultCode != RESULT_OK ? null 
        : intent.getData(); 
      mUploadMessage.onReceiveValue(result); 
      mUploadMessage = null; 

     } 
    } 
    private class MyWebChromeClient extends WebChromeClient { 
     public void openFileChooser(ValueCallback<Uri> uploadMsg) { 
      mUploadMessage = uploadMsg; 
      Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
      i.addCategory(Intent.CATEGORY_OPENABLE); 
      i.setType("image/*"); 
      IStyla.this.startActivityForResult(Intent.createChooser(i, "Image Browser"), FILECHOOSER_RESULTCODE); 
     } 

     @Override 
     public boolean onJsAlert(WebView view, String url, String message,final JsResult result) { 
      //handle Alert event, here we are showing AlertDialog 
      new AlertDialog.Builder(IStyla.this) 
       .setTitle("JavaScript Alert !") 
       .setMessage(message) 
       .setPositiveButton(android.R.string.ok, 
        new AlertDialog.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          // do your stuff 
          result.confirm(); 
         } 
        }).setCancelable(false).create().show(); 
      return true; 
     } 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_istyla); 
     WebView webView = (WebView) findViewById(R.id.webView1); 
     WebSettings webSettings = webView.getSettings(); 
     webSettings.setJavaScriptEnabled(true); 
     webView.setWebChromeClient(new MyWebChromeClient(){ 
      public void onProgressChanged(WebView view, int progress) { 
       // Activities and WebViews measure progress with different scales. 
       // The progress meter will automatically disappear when we reach 100% 
       ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar1); 
       if(progress < 100 && progressBar.getVisibility() == ProgressBar.GONE){ 
        progressBar.setVisibility(ProgressBar.VISIBLE); 
       } 
       progressBar.setProgress(progress); 
       if(progress == 100) { 
        progressBar.setVisibility(ProgressBar.GONE); 
       } 
      } 
      public void openFileChooser(ValueCallback<Uri> uploadMsg) { 
       mUploadMessage = uploadMsg; 
       Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
       i.addCategory(Intent.CATEGORY_OPENABLE); 
       i.setType("image/*"); 
       IStyla.this.startActivityForResult(Intent.createChooser(i, "Image Browser"), FILECHOOSER_RESULTCODE); 
      } 
     }); 
     webView.setWebViewClient(new WebViewClient() { 
      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 
       view.loadUrl(url); 
       ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar1); 
       progressBar.setVisibility(ProgressBar.VISIBLE); 
       return true; 
      } 

     }); 
     webView.loadUrl("https://m.facebook.com"); 

    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_BACK){ 
      if(((WebView)findViewById(R.id.webView1)).canGoBack()){ 
       ((WebView)findViewById(R.id.webView1)).goBack(); 
       return true; 
      } 
     } 
     return super.onKeyDown(keyCode, event); 
    } 

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

} 

धन्यवाद

+0

शायद तुम, permisions की जरूरत की तरह [READ_EXTERNAL_STORAGE] (http://developer.android.com/reference/android/Manifest.permission.html#READ_EXTERNAL_STORAGE) – neworld

+0

मैं जोड़ लिया है कुछ अनुमति है, लेकिन यह अभी भी काम नहीं करता है । मैंने उपरोक्त अपना कोड जोड़ा है। –

+2

http://stackoverflow.com/questions/5907369/file-upload-in-webview –

उत्तर

4

को देखते हुए वोट की उच्च संख्या मैं है अनुमान लगाते हुए किसी ने तीसरी टिप्पणी नहीं देखी है (David Esteves से) a link with to answer to this question.

Michel Olivier ने कहा:

यह समाधान हनीकॉम और आइस क्रीम सैंडविच के लिए भी काम करता है। लगता है जैसे Google ने एक अच्छी नई सुविधा (विशेषता स्वीकार करें) और पीछे की संगतता के लिए ओवरलोड को लागू करने के लिए भूल गए।

public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) 
{ 
    this.openFileChooser(uploadMsg); 
} 

इस मैं अपने कोड को चलाने के लिए सक्षम था कर:

तो तुम सिर्फ अपने MyWebChromeClient वर्ग कि स्ट्रिंग पैरामीटर स्वीकार करता है करने के लिए एक openFileChooser अधिभार को जोड़ने के लिए और फिर एक जो ऐसा नहीं करता फोन की जरूरत है जैसा सोचा वैसा।

3

WebChromeClient.OpenFileChooser में AlertDialog का आह्वान करते समय, आपको वेबच्रोम को सूचित करने की आवश्यकता है कि बैक बटन के मामले में कोई चयन नहीं किया गया था। चूंकि बैक बटन आपके संवाद में दबाया जा सकता है, इसलिए आपको संवाद के ऑनकैनल को संभालना होगा और वेबक्रॉम को यह बताएं कि चयन समाप्त हो गया है।

जैसे:

private ValueCallback<Uri> mUploadMessage; 
private final static int FILECHOOSER_RESULTCODE = 1; 
private final static int CAMERA_RESULTCODE = 2; 

... 

    webView.setWebChromeClient(new WebChromeClient() { 
     public boolean onConsoleMessage(ConsoleMessage cm) { 
     Log.d("MyApplication", cm.message() + " -- From line " 
          + cm.lineNumber() + " of " 
          + cm.sourceId()); 
     return true; 
     } 

     public void onExceededDatabaseQuota(String url, String 
      databaseIdentifier, long currentQuota, long estimatedSize, long 
      totalUsedQuota, QuotaUpdater quotaUpdater) { 
          quotaUpdater.updateQuota(estimatedSize+65536); 
      } 

    @SuppressWarnings("unused") 
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String AcceptType, String capture) { 
      this.openFileChooser(uploadMsg); 
     } 

    @SuppressWarnings("unused") 
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String AcceptType) { 
     this.openFileChooser(uploadMsg); 
    } 

    public void openFileChooser(ValueCallback<Uri> uploadMsg) { 

     mUploadMessage = uploadMsg; 
     PesScreen.this.openImageIntent(); 
    } 
    });   

...

private void openImageIntent() { 

final File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); //file storage 


    root.mkdirs(); 
    int nCnt = 1; 
    if (root.listFiles() != null) 
     nCnt = root.listFiles().length; 
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); 
    final String fname = String.format("/dest-%s-%d.jpg", sdf.format(Calendar.getInstance().getTime()), nCnt); 

    final File sdImageMainDirectory = new File(root.getAbsolutePath() + fname); 
    outputFileUri = Uri.fromFile(sdImageMainDirectory); 
//selection Photo/Gallery dialog 
    AlertDialog.Builder alert = new AlertDialog.Builder(this); 

    alert.setTitle("Select source"); 

    final CharSequence[] items = {"Photo", "Gallery"}; 
    alert.setItems(items, new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton) { 

      dialog.dismiss(); 
      if(whichButton == 0) 
      { 
       Intent chooserIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       chooserIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
       startActivityForResult(chooserIntent, CAMERA_RESULTCODE); 
      } 
      if(whichButton == 1) 
      { 
       Intent chooserIntent = new Intent(Intent.ACTION_GET_CONTENT); 
       chooserIntent.setType("image/*"); 
       startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE); 
      } 
     } 
    }); 
    alert.setOnCancelListener(new DialogInterface.OnCancelListener() { 
     @Override 
     public void onCancel(DialogInterface dialog) { 

     //here we have to handle BACK button/cancel 
      if (mUploadMessage!= null){ 
       mUploadMessage.onReceiveValue(null); 
      } 
      mUploadMessage = null; 
      dialog.dismiss(); 
     } 
    }); 
    alert.create().show(); 
5

Mike Olivier's referenceFr33dan द्वारा साझा महत्वपूर्ण है। मैं साझा कर रहा हूं कि मेरे लिए परिस्थिति में मेरे लिए क्या काम करता है।

wv.setWebChromeClient(new WebChromeClient() { 

// For Android 3.0+ 
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { 
mUploadMessage = uploadMsg; 
Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
i.addCategory(Intent.CATEGORY_OPENABLE); 
i.setType("image/*"); 
MainActivity.this.startActivityForResult(Intent.createChooser(i, getString(R.string.fileselect)), MainActivity.FILECHOOSER_RESULTCODE); 
} 

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


// For Android > 4.1 
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){ 
openFileChooser(uploadMsg, ""); 
} 
}); 

नोट: मैं ढेर अतिप्रवाह और m0s' blog में यह कहीं नहीं मिला।

इसके अलावा, लिंट चेतावनियों को अनदेखा करें। यह एपीआई 8 और ऊपर के लिए बहुत अच्छी तरह से काम करता है।

+0

thx एंड्रॉइड संस्करणों के बारे में महत्वपूर्ण भाग के लिए – BigTarakan

+0

कई उपयोगकर्ता अभी भी एंड्रॉइड 3.0 और नीचे का उपयोग कर रहे हैं। यहां तक ​​कि, मैंने इसे एसओ समुदाय में पाया। :) – Chirag

+0

जब भी मैं इस फ़ंक्शन को कार्यान्वित करता हूं तब भी कुछ भी नहीं हो रहा है। और क्या यह फ़ंक्शन कॉल रिकर्सिव नहीं करता है? – Sagar

4

यह कोड 100% परीक्षण किया गया है और मैंने इसे अपने आवेदन में उपयोग किया। कार्य ठीक

private class MyWebChromeClient extends WebChromeClient { 
    //The undocumented magic method override 
    //Eclipse will swear at you if you try to put @Override here 


    // For Android 3.0+ 
    public void openFileChooser(ValueCallback uploadMsg, String acceptType) { 
     mUploadMessage = uploadMsg; 
     Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
     intent.addCategory(Intent.CATEGORY_OPENABLE); 
     intent.setType("*/*"); 
     startActivityForResult(Intent.createChooser(intent, "File Browser"), FILECHOOSER_RESULTCODE); 
    } 

    //For Android 4.1+ only 
    protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { 
     mUploadMessage = uploadMsg; 
     Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
     intent.addCategory(Intent.CATEGORY_OPENABLE); 
     intent.setType("*/*"); 
     startActivityForResult(Intent.createChooser(intent, "File Browser"), FILECHOOSER_RESULTCODE); 
    } 

    protected void openFileChooser(ValueCallback<Uri> uploadMsg) { 
     mUploadMessage = uploadMsg; 
     Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
     intent.addCategory(Intent.CATEGORY_OPENABLE); 
     intent.setType("*/*"); 
     startActivityForResult(Intent.createChooser(intent, "File Chooser"), FILECHOOSER_RESULTCODE); 
    } 

    // For Lollipop 5.0+ Devices 
    public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) { 
     if (uploadMessage != null) { 
      uploadMessage.onReceiveValue(null); 
      uploadMessage = null; 
     } 

     uploadMessage = filePathCallback; 
     Intent intent = null; 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      intent = fileChooserParams.createIntent(); 
     } 
     try { 
      startActivityForResult(intent, REQUEST_SELECT_FILE); 
     } catch (ActivityNotFoundException e) { 
      uploadMessage = null; 
      Toast.makeText(getApplicationContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show(); 
      return false; 
     } 
     return true; 
    } 
+1

मेरे लिए अपना काम अच्छा लगा। लेकिन जब मैं marshmallow डिवाइस में दस्तावेज़ चयनकर्ता से गैलरी का चयन करता हूं तो उसे फ़ाइल नहीं मिलती है। –

+0

अपलोड मैसेज क्या है? REQUEST_SELECT_FILE क्या है? – asdjfiasd

+0

अपलोड मैसेज ValueCallback है (प्रश्न कोड जांचें) और REQUEST_SELECT_FILE एक्टिविटी रिसैट() के लिए इसे ट्रैक करने के लिए कोई पूर्णांक संख्या है। –

1

इस कोड को मुद्दा

हल करने के लिए बस MainActivity अद्यतन करने में मदद करेगा।जावा bellow कोड

public class MainActivity extends Activity{ 
    private WebView mWebview ; 
    private ValueCallback<Uri> mUploadMessage; 
    public ValueCallback<Uri[]> uploadMessage; 
    public static final int REQUEST_SELECT_FILE = 100; 
    private final static int FILECHOOSER_RESULTCODE = 1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_main); 

     mWebview = (WebView) findViewById(R.id.help_webview); 
     WebSettings webSettings = mWebview.getSettings(); 
     webSettings.setJavaScriptEnabled(true); 
     webSettings.setUseWideViewPort(true); 
     webSettings.setLoadWithOverviewMode(true); 
     webSettings.setAllowFileAccess(true); 
     webSettings.setJavaScriptCanOpenWindowsAutomatically(true); 
     webSettings.setBuiltInZoomControls(true); 
     webSettings.setPluginState(WebSettings.PluginState.ON); 
     webSettings.setSupportZoom(true); 
     webSettings.setAllowContentAccess(true); 



     final Activity activity = this; 
     mWebview.setWebViewClient(new WebViewClient() { 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
       Toast.makeText(activity, description, Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     mWebview .loadUrl("http://www.google.com"); 

     String permission = Manifest.permission.CAMERA; 
     int grant = ContextCompat.checkSelfPermission(this, permission); 
     if (grant != PackageManager.PERMISSION_GRANTED) { 
      String[] permission_list = new String[1]; 
      permission_list[0] = permission; 
      ActivityCompat.requestPermissions(this, permission_list, 1); 
     } 


     mWebview.setWebChromeClient(new WebChromeClient() 
     { 
      // For 3.0+ Devices (Start) 
      // onActivityResult attached before constructor 
      protected void openFileChooser(ValueCallback uploadMsg, String acceptType) 
      { 
       mUploadMessage = uploadMsg; 
       Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
       i.addCategory(Intent.CATEGORY_OPENABLE); 
       i.setType("image/*"); 
       startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE); 
      } 


      // For Lollipop 5.0+ Devices 
      @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 
      public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) 
      { 
       if (uploadMessage != null) { 
        uploadMessage.onReceiveValue(null); 
        uploadMessage = null; 
       } 

       uploadMessage = filePathCallback; 

       Intent intent = fileChooserParams.createIntent(); 
       try 
       { 
        startActivityForResult(intent, REQUEST_SELECT_FILE); 
       } catch (ActivityNotFoundException e) 
       { 
        uploadMessage = null; 
        Toast.makeText(MainActivity.this.getApplicationContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show(); 
        return false; 
       } 
       return true; 
      } 

      //For Android 4.1 only 
      protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) 
      { 
       mUploadMessage = uploadMsg; 
       Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
       intent.addCategory(Intent.CATEGORY_OPENABLE); 
       intent.setType("image/*"); 
       startActivityForResult(Intent.createChooser(intent, "File Browser"), FILECHOOSER_RESULTCODE); 
      } 

      protected void openFileChooser(ValueCallback<Uri> uploadMsg) 
      { 
       mUploadMessage = uploadMsg; 
       Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
       i.addCategory(Intent.CATEGORY_OPENABLE); 
       i.setType("image/*"); 
       startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); 
      } 
     }); 



    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent intent) 
    { 
     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
     { 
      if (requestCode == REQUEST_SELECT_FILE) 
      { 
       if (uploadMessage == null) 
        return; 
       uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent)); 
       uploadMessage = null; 
      } 
     } 
     else if (requestCode == FILECHOOSER_RESULTCODE) 
     { 
      if (null == mUploadMessage) 
       return; 
      // Use MainActivity.RESULT_OK if you're implementing WebView inside Fragment 
      // Use RESULT_OK only if you're implementing WebView inside an Activity 
      Uri result = intent == null || resultCode != MainActivity.RESULT_OK ? null : intent.getData(); 
      mUploadMessage.onReceiveValue(result); 
      mUploadMessage = null; 
     } 
     else 
      Toast.makeText(MainActivity.this.getApplicationContext(), "Failed to Upload Image", Toast.LENGTH_LONG).show(); 
    } 

साथ फ़ाइल इस कोड के साथ की कोशिश निम्नलिखित लाइनों

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

यह मेरे लिए काम किया AndroidManifest.xml अद्यतन में अनुमति सक्षम करना सुनिश्चित करें।

+0

यह काम करता है लेकिन यह केवल मुझे वीडियो अपलोड करने की इजाजत देता है, यह कैमरे के विकल्प को भी नहीं लाया बल्कि इसके बजाय फ़ाइल चयनकर्ता खोला गया। – TheLearner

+0

तो अगर आपको यह स्पष्ट रूप से बताया जाए तो आपको वास्तव में क्या चाहिए, यह सहायक होगा @TheLearner –

+0

मुझे वेबव्यू के माध्यम से वेबसाइट पर छवियों और वीडियो अपलोड करने की आवश्यकता है। मुझे यह अच्छा लगेगा कि किसी उपयोगकर्ता ने अपने कैमरे को अपलोड करने के लिए फोटो अपलोड करने का निर्णय लिया और यदि उन्होंने वेबव्यू पर अपलोड करने के लिए एक छवि/वीडियो चुना है। – TheLearner

संबंधित मुद्दे