gpt4 book ai didi

java - Android Webview 应用程序,上传图片不起作用,点击选择照片时没有任何反应

转载 作者:行者123 更新时间:2023-11-30 01:03:14 24 4
gpt4 key购买 nike

我已经创建了一个 Android Webview 应用程序并且它工作正常,但是当我点击用于上传照片的按钮时,没有任何反应。我附上我的代码,所以你们可以帮助我

主 Activity .java

public class MainActivity extends AppCompatActivity {
private WebView myWebView;


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

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

myWebView = new WebView(this);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebChromeClient(new WebChromeClient() {
//The undocumented magic method override
//Eclipse will swear at you if you try to put @Override here
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
MainActivity.this.showAttachmentDialog(uploadMsg);
}

// For Android > 3.x
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
MainActivity.this.showAttachmentDialog(uploadMsg);
}

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

this.setContentView(myWebView);

myWebView.loadUrl("http://www.droidocial.com");
}

private void showAttachmentDialog(ValueCallback<Uri> uploadMsg) {
this.mUploadMessage = uploadMsg;

Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");

this.startActivityForResult(Intent.createChooser(i, "Choose type of attachment"), FILECHOOSER_RESULTCODE);
}

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

@Override
public void onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}

AndroidManifest.xml

  <uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.CAMERA"></uses-permission>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name=".Splashscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="com.official.droidocial.droidocial.MAINACTIVITY" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>

我尝试了太多,但仍然卡在这个错误中,谁能告诉我如何解决这个错误。

最佳答案

看起来这可以帮助 https://www.opengeeks.me/2015/08/filechooser-and-android-webview/ .

Uri[] results = null;
String dataString = intent.getDataString();
if (dataString != null) {
results = new Uri[]{Uri.parse(dataString)};
}
mUploadMessage.onReceiveValue(results);

此外,您还需要此权限才能访问设备上的文件:

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

关于java - Android Webview 应用程序,上传图片不起作用,点击选择照片时没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39186485/

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