Android : Input type="file" ไม่ทำงานบน webView
เพิ่งเขียนให้ app hatyaicityclimate.org สามารถรายงานสถานการณ์ พร้อมอัพโหลดภาพได้ และรายงานระดับน้ำจากสมาชิกด้วย แต่ติดว่าเมื่อกดปุ่มเลือกไฟล์ภาพจากกล้อง ด้วย tag input type="file" จะไม่เด้งหน้าเลือกไฟล์มาให้
คำแนะนำคือให้เปลี่ยนไปใช้ WebChromeClient แทน แก้ไขตามตัวอย่าง หรือไปดูจากที่มาได้เลยครับ
public class MyWb extends Activity {
WebView web;
private ValueCallback<Uri> mUploadMessage;<br />
private final static int FILECHOOSER_RESULTCODE=1;<br />
@Override<br />
protected void onActivityResult(int requestCode, int resultCode,<br />
Intent intent) {<br />
if(requestCode==FILECHOOSER_RESULTCODE)<br />
{<br />
if (null == mUploadMessage) return;<br />
Uri result = intent == null || resultCode != RESULT_OK ? null<br />
: intent.getData();<br />
mUploadMessage.onReceiveValue(result);<br />
mUploadMessage = null;<br />
}
}<br />
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
web = (WebView) findViewById(R.id.webview01);
<br />
web = new WebView(this);<br />
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://www.script-tutorials.com/demos/199/index.html");
web.setWebViewClient(new myWebClient());
web.setWebChromeClient(new WebChromeClient()<br />
{<br />
//The undocumented magic method override<br />
//Eclipse will swear at you if you try to put @Override here<br />
// For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg) {<br />
mUploadMessage = uploadMsg;<br />
Intent i = new Intent(Intent.ACTION_GET_CONTENT);<br />
i.addCategory(Intent.CATEGORY_OPENABLE);<br />
i.setType("image/*");<br />
MyWb.this.startActivityForResult(Intent.createChooser(i,"File Chooser"), FILECHOOSER_RESULTCODE);<br />
}
// 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("*/*");
MyWb.this.startActivityForResult(
Intent.createChooser(i, "File Browser"),
FILECHOOSER_RESULTCODE);
}
//For Android 4.1
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
mUploadMessage = uploadMsg;<br />
Intent i = new Intent(Intent.ACTION_GET_CONTENT);<br />
i.addCategory(Intent.CATEGORY_OPENABLE);<br />
i.setType("image/*");<br />
MyWb.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), MyWb.FILECHOOSER_RESULTCODE );
}
}); <br />
<br />
<br />
setContentView(web); <br />
<br />
<br />
}
public class myWebClient extends WebViewClient
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
<br />
}
}
//flipscreen not loading again
@Override
public void onConfigurationChanged(Configuration newConfig){<br />
super.onConfigurationChanged(newConfig);
}
}}
ที่มา openFileChooser not called when input type="file" is clicked on android 4.4 webview
Relate topics
- Flutter App Guide
- My app on macOS - ใช้แอบอะไรบ้างนะบน macOS
- Build App & Upload to store
- Flutter :: Package Info for About us
- ทำแอปหลายภาษา
- Flutter Permission
- Flutter App Lifecycle
- My First Flutter App
- Run Flutter บน iPad
- สร้างปุ่มเมนูบน Action Bar ของ Template Buttom Navigator Activity
- วิธีการรับส่งข้อมูลของ Android Fragment
- Application Green Smile
- Android : เพิ่มภาษาไทยให้กับ app
- Affinity Photo for desktop or Clip Studio Paint ?
- Add Firebase Analytics & Crashlytics to Android App
- Copy and rename an Android project in Android Studio
- ทำให้ Web App คุยกับ Android ผ่านทาง WebView - Building Web Apps in WebView
- Android เขียนยาก กูเกิลเลยเปิดคอร์ส Android Performance ปรับแต่งประสิทธิภาพแอพ เรียนฟรีผ่านเน็ต
- เขียน App Android จะว่ายากก็ยาก จะว่าง่ายก็ง่าย
- Bug : isset($body['location']) ดันเป็นจริงเมื่อ $body ไม่ใช่ array