我无法让openFileChooser()
工作。我不在Android KitKat上(因为我们知道这种方法在4.4上不再起作用)。我尝试了不同的解决方案,但它也没有用。
这是html:
<a class="myClass" href="./gallery_files/gallery.html" title="" onclick="document.getElementById('upload_').click(); return false;">
<form class="avatar" action="myUrlIsHere" method="post" enctype="multipart/form-data">
<input id="upload_" class="invisible upload" type="file" name="Avatar">
</form>
这是我的WebChromeClient类:
public class WebViewChromeClient extends WebChromeClient {
private Activity activity;
public Uri imageUri;
public static final int FILECHOOSER_RESULTCODE = 1;
private Uri mCapturedImageURI = null;
private Context context;
private MainActivity mainActivity;
public WebViewChromeClient(MainActivity mainActivity) {
this.activity = mainActivity;
this.context = mainActivity.getApplicationContext();
this.mainActivity = mainActivity;
}
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Log.i("WebChromeClient", "openFileChooser() called.");
// Update message
mainActivity.setmUploadMessage(uploadMsg);
if (uploadMsg == null) {
Log.d("UPLOAD MESSAGE", "NULL");
}
try {
File imageStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"Audi");
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);
mainActivity.setmCapturedImageURI(mCapturedImageURI);
Log.d("Line", "57");
// Camera capture image intent
final Intent captureIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
mainActivity.setmCapturedImageURI(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
activity.startActivityForResult(chooserIntent,
FILECHOOSER_RESULTCODE);
} catch (Exception e) {
Toast.makeText(context, "Exception:" + e, Toast.LENGTH_LONG).show();
}
}
// openFileChooser for Android < 3.0
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
Log.i("WebChromeClient", "openFileChooser() called.");
openFileChooser(uploadMsg, "");
}
// openFileChooser for other Android versions
public void openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType, String capture) {
Log.i("WebChromeClient", "openFileChooser() called.");
openFileChooser(uploadMsg, acceptType);
}
// The webPage has 2 filechoosers and will send a
// console message informing what action to perform,android wml_siso init
// taking a photo or updating the file
public boolean onConsoleMessage(ConsoleMessage cm) {
//onConsoleMessage(cm.message(), cm.lineNumber(), cm.sourceId());
return true;
}
public void onConsoleMessage(String message, int lineNumber, String sourceID) {
Log.d("androidruntime", "Show console messages, Used for debugging: "
+ message);
}
}
您可以在openFileChoser()方法Log.i("WebChromeClient", "openFileChooser() called.");
中看到此行 - LogCat中没有任何内容。
onActivityResult():
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == WebViewChromeClient.FILECHOOSER_RESULTCODE) {
Log.d("MainActivity", "onActivityResult");
if (null == uploadMsg) {
Log.d("FileChooser Result", "58");
return;
}
Log.d("MainActivity", "onActivityResult");
Uri result = null;
try {
if (resultCode != RESULT_OK) {
result = null;
} else {
result = intent == null ? mCapturedImageURI : intent
.getData();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "activity :" + e,
Toast.LENGTH_LONG).show();
}
uploadMsg.onReceiveValue(result);
setmUploadMessage(null);
}
Log.d("MainActivity", "onActivityResult");
}
你知道问题出在哪里吗?
答案 0 :(得分:1)
找到适用于我的解决方案。它在produard-android.txt
中添加了一条规则:
-keepclassmembers class * extends android.webkit.WebChromeClient {
public void openFileChooser(...);
}
答案 1 :(得分:0)
似乎在Kitkat 4.4中没有调用openFileChooser。它被调用直到android 4.3。 虽然它在4.4.3再次起作用。