gpt4 book ai didi

Android 文件选择器未从 Lollipop 或 Marshmallow 中的 WebView 打开

转载 作者:太空狗 更新时间:2023-10-29 15:40:42 26 4
gpt4 key购买 nike

我正在开发一个应用程序,它使用 WebView 来访问一些逻辑。逻辑中有一部分用户可以上传图片,但是,我似乎无法为该应用找到上传图片的方法。
我用作示例的代码在这个 link 中.

正如您将看到的,自定义 Intent 是从 WebChromeClient 设置的,但是,文件选择器根本不会打开。代码:

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();
}

}

提前致谢!
编辑

所以,在@JoxTraex 启发我之后,我意识到文件选择器没有触发,因为我没有覆盖 WebChromeClient 的任何方法。现在,文件选择器正在提示,但是,如果选择图片,则图片不会上传。
我用于文件选择器的代码:

@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
openFileChooser(filePathCallback, "");
return super.onShowFileChooser(webView, filePathCallback, fileChooserParams);
}

public void openFileChooser(ValueCallback<Uri[]> uploadMsg, String acceptType){
// Update message
//mUploadMessage = uploadMsg.this;
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();
}

}

提前致谢!

最佳答案

好吧,在与一些听众斗争之后,我找到了解决我的情况的代码。不过,感谢@JoxTraex 和 this dude .

关于Android 文件选择器未从 Lollipop 或 Marshmallow 中的 WebView 打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37313177/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com