Aktifkan fitur upload dari webview di skatchware

Aktifkan fitur upload dari webview di skatchware

Sekarang Sketchware memiliki block untuk menambahkan kode java langsung di proyek, dimungkinkan untuk mengaktifkan unggahan file di webview.

Untuk mengaktifkan unggahan file di tampilan web di aplikasi sketchware, ikuti langkah-langkah yang diberikan di bawah ini:

1. Masukkan webview di VIEW area pada proyek. Perhatikan ID dari webview, biasanya itu adalah webview1.

2. Di area LOGIC proyek, dalam event onCreate, tambahkan block add source directly dan salin kode berikut di dalamnya:

webview1.setWebChromeClient(new WebChromeClient() {
// For 3.0+ Devices
protected void openFileChooser(ValueCallback uploadMsg, String acceptType) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*"); startActivityForResult(Intent.createChooser(i, "File Browser"), 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(getApplicationContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show(); return false; }
return true; }

//For Android 4.1 only
protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
mUploadMessage = uploadMsg; Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("image/*"); startActivityForResult(Intent.createChooser(intent, "File Browser"), FILECHOOSER_RESULTCODE);
}

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

});

Perhatikan bahwa jika ID dari tampilan web bukan webview1, ubah sesuai dengan kode di atas. Kode ini harus ada di onCreate sebelum block WebView loadUrl.


3. Tambahkan block webview loadUrl dan tulis url untuk dimuat di webview.

4. Tambahkan komponen  FilePicker fp.

5. Tambahkan More Block baru extra.

6. Untuk mendifisinikan block extra ini, gunakan add source directly block and masukkan kode dibawah ini:
}

private ValueCallback<Uri> mUploadMessage;
public ValueCallback<Uri[]> uploadMessage;
public static final int REQUEST_SELECT_FILE = 100;

private final static int FILECHOOSER_RESULTCODE = 1;
{


7. Tambahkan event FilePicker onFilesPicked  Di sini, gunakan add source directly block  dan memasukkan kode berikut:
}
break;

case REQUEST_SELECT_FILE:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (uploadMessage == null) return; uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(_resultCode, _data)); uploadMessage = null; }
break;

case FILECHOOSER_RESULTCODE:
if (null == mUploadMessage){
return; }
Uri result = _data == null || _resultCode != RESULT_OK ? null : _data.getData(); mUploadMessage.onReceiveValue(result);
mUploadMessage = null;

if (true){



8. Simpan dan jalankan proyek. Sekarang pengguna dapat mengunggah file apa pun melalui tautan WebView.

Share this:

Disqus Comments