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 ?.
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