2016-03-23 13 views
0

ich einfach WebView bin mit Website zu durchsuchen, die Datei Formular mit dem Formularfeld mit dem folgenden Code aus stackovrflow laden hat:Android WebView hochladen Dateiname nicht korrekt Set

private ValueCallback<Uri> mUploadMessage; 
public ValueCallback<Uri[]> uploadMessage; 
public static final int REQUEST_SELECT_FILE = 100; 
private final static int FILECHOOSER_RESULTCODE = 1; 

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

    Log.i("ME", String.valueOf(requestCode)); 

    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 != RESULT_OK ? null 
       : intent.getData(); 
     mUploadMessage.onReceiveValue(result); 
     mUploadMessage = null; 
     Log.i("ME", result.toString()); 
    } else 
     Toast.makeText(getActivity().getApplicationContext(), 
       "Failed to Upload Image", Toast.LENGTH_LONG).show(); 
} 



private class MyWebChromeClient extends WebChromeClient { 
    @Override 
    public void onProgressChanged(WebView view, int progress) { 
     // progressDialog.show(); 
     // progressDialog.setProgress(0); 
     // activity.setProgress(progress * 1000); 

     // progressDialog.incrementProgressBy(progress * 1000); 

     progressBar.setProgress(progress); 

     // if (progress == 100 && progressDialog.isShowing()) { 
     // progressDialog.dismiss(); 
     // } 

     // activity.setTitle("Loading..."); 
     // activity.setProgress(progress * 100); // Make the bar disappear 
     // after URL is loaded 
     // Log.i("ME", ""+progress); 

     // Return the app name after finish loading 
     if (progress == 100) { 
      // activity.setTitle(R.string.app_name); 
      loadingUrl = false; 
      progressBar.setProgress(0); 
      progressBar.setVisibility(View.GONE); 
     } 

    } 

    // 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<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); 

    } 

    // 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("*/*"); 
     startActivityForResult(
       Intent.createChooser(i, "File Browser"), 
       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("image/*"); 
     startActivityForResult(
       Intent.createChooser(i, "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 = fileChooserParams.createIntent(); 
     try { 
      startActivityForResult(intent, REQUEST_SELECT_FILE); 
     } catch (ActivityNotFoundException e) { 
      uploadMessage = null; 
      Toast.makeText(getActivity().getApplicationContext(), 
        "Cannot Open File Chooser", Toast.LENGTH_LONG).show(); 
      return false; 
     } 
     return true; 
    } 

} 

Auf Android 5.1 es funktioniert gut. Auf Android 4.xx und senken lädt es die Datei auf den Server normal, aber der Dateiname ist so etwas wie dieses:

image%3A9121 
20098 
24964 

während die eigentlichen Dateinamen so etwas wie sind:

image.jpg 
image.png 
image.jpeg 

Warum ändert die Bilddateinamen zu einer Reihe usw.

die wie dies das Ergebnis Bild-uRL onActivityResult zeigen [mit Log.i ("ME", result.toString());]:

Inhalt: // com .ein droid.providers.media.documents/documents/image% 3A175458

Die Datei ist .jpg, bedeutet das, dass sich das% 3A175458 auf das ".jpg" in irgendeiner Zeichencodierung bezieht ?.

Antwort

0

Gelöst mit der Bibliothek aFileChooser.

habe ich nur die Dateien FileUtils.java und LocalStorageProvider.java aus dieser Bibliothek und modifiziert, um die onActivityResult mit diesem Code:

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
     if (requestCode != INPUT_FILE_REQUEST_CODE || mFilePathCallback == null) { 
      super.onActivityResult(requestCode, resultCode, data); 
      return; 
     } 
     Uri[] results = null; 
     // Check that the response is a good one 
     if (resultCode == Activity.RESULT_OK) { 
      if (data == null) { 
       // If there is not data, then we may have taken a photo 
       if (mCameraPhotoPath != null) { 
        results = new Uri[] { Uri.parse(mCameraPhotoPath) }; 
       } 
      } else { 
       String dataString = data.getDataString(); 
       if (dataString != null) { 
        results = new Uri[] { Uri.parse(dataString) }; 
       } 
      } 
     } 
     mFilePathCallback.onReceiveValue(results); 
     mFilePathCallback = null; 
    } else if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) { 
     if (requestCode != FILECHOOSER_RESULTCODE || mUploadMessage == null) { 
      super.onActivityResult(requestCode, resultCode, data); 
      return; 
     } 
     if (requestCode == FILECHOOSER_RESULTCODE) { 
      if (null == this.mUploadMessage) { 
       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(); 

        Log.i("ME", "Uri = " + result.toString()); 
        try { 
         // Get the file path from the URI 
         final String path = FileUtils.getPath(this, result); 
         //Toast.makeText(this, "File Selected: " + path, Toast.LENGTH_LONG).show(); 
         result = Uri.fromFile(new File(path)); 
        } catch (Exception e) { 
         Log.e("Me", "File select error", e); 
        } 

       } 
      } catch (Exception e) { 
       Toast.makeText(getApplicationContext(), "activity :" + e, Toast.LENGTH_LONG).show(); 
      } 
      mUploadMessage.onReceiveValue(result); 
      mUploadMessage = null; 
     } 
    } 
    return; 
} 
+0

Es ist immer noch ein Problem mit diesem Code. Wenn der Dateiname ein URI-Escapezeichen (wie ein Leerzeichen) enthält, wird beim Hochladen der Datei eine 0KB-Datei angezeigt. Es ist der Fehler: https://bugs.chromium.org/p/chromium/issues/detail?id=488443. – nico