Hallo! Selamat Datang di Marketplace produk digital, Freelancer terbaik di indonesia (67507 Members, 783 Products, 5291 Topic, 12956 Comments)

Aplikasi Android untuk Swarakalibata Ci - Android Studio 3.3 Project

Dibuat
Login Terakhir 5 Bulan lalu,
Telah Dilihat 1142 Kali
Fadil Maulana Malik Mengatakan : Sultan Account
  1. Assalamualaikum Warahmatullahi Wabarakatuh...
  2.  
  3. saya sedang menggunakan aplikasi webview ini dan mendapat kendala pada upload foto yang tidak berfungsi..
  4.  
  5. apakah ada tambahan code untuk mengaktifkan fungsi upload foto?
  6.  
  7. terimakasih

Ada 2 Jawaban

Fadil Maulana Malik
commented on 11 Apr 2019 13:02:00
  1. Assalam,..
  2. hahaha setelah cari-cari dibeberapa forum akhirnya app android untuk swarakalibata CI ini bisa saya fungsikan untuk tombol Upload Image nya.
  3.  
  4. ok langsung saja saya bagikan disni ..
  5.  
  6. masuk ke Java-com.example.swarakalibataci-MainActivity
  7.  
  8. tambahkan code berikut
  9.  
  10. WebView webviewku;
  11. private static final String TAG = MainActivity.class.getSimpleName();
  12. private String mCM;
  13. private ValueCallback<Uri> mUM;
  14. private ValueCallback<Uri[]> mUMA;
  15. private final static int FCR=1;
  16. WebSettings websettingku;
  17.  
  18. //select whether you want to upload multiple files (set 'true' for yes)
  19. private boolean multiple_files = false;
  20.  
  21. @SuppressLint({"SetJavaScriptEnabled", "WrongViewCast"})
  22. @SuppressWarnings({"findViewById", "RedundantCast"})
  23. @Override
  24. protected void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.activity_main);
  27.  
  28. webviewku = (WebView)findViewById(R.id.WebView1);
  29. webviewku.getSettings().setJavaScriptEnabled(true);
  30. webviewku.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
  31. websettingku = webviewku.getSettings();
  32. webviewku.setWebViewClient(new WebViewClient());
  33.  
  34.  
  35. webviewku = (WebView) findViewById(R.id.WebView1);
  36. assert webviewku != null;
  37. WebSettings webSettings = webviewku.getSettings();
  38. webSettings.setJavaScriptEnabled(true);
  39. webSettings.setAllowFileAccess(true);
  40.  
  41. if(Build.VERSION.SDK_INT >= 21){
  42. webSettings.setMixedContentMode(0);
  43. webviewku.setLayerType(View.LAYER_TYPE_HARDWARE, null);
  44. }else if(Build.VERSION.SDK_INT >= 19){
  45. webviewku.setLayerType(View.LAYER_TYPE_HARDWARE, null);
  46. }else {
  47. webviewku.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
  48. }
  49. webviewku.setWebViewClient(new Callback());
  50. webviewku.loadUrl("http://makole.id"); //add your test web/page address here
  51. webviewku.setWebChromeClient(new WebChromeClient() {
  52. /*
  53.   * openFileChooser is not a public Android API and has never been part of the SDK.
  54.   */
  55. //handling input[type="file"] requests for android API 16+
  56. @SuppressLint("ObsoleteSdkInt")
  57. @SuppressWarnings("unused")
  58. public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
  59. mUM = uploadMsg;
  60. Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  61. i.addCategory(Intent.CATEGORY_OPENABLE);
  62. i.setType("*/*");
  63. if (multiple_files && Build.VERSION.SDK_INT >= 18) {
  64. i.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
  65. }
  66. startActivityForResult(Intent.createChooser(i, "File Chooser"), FCR);
  67. }
  68.  
  69. //handling input[type="file"] requests for android API 21+
  70. @SuppressLint("InlinedApi")
  71. public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
  72. if (file_permission()) {
  73. String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
  74.  
  75. //checking for storage permission to write images for upload
  76. if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
  77. ActivityCompat.requestPermissions(MainActivity.this, perms, FCR);
  78.  
  79. //checking for WRITE_EXTERNAL_STORAGE permission
  80. } else if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
  81. ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, FCR);
  82.  
  83. //checking for CAMERA permissions
  84. } else if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
  85. ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, FCR);
  86. }
  87. if (mUMA != null) {
  88. mUMA.onReceiveValue(null);
  89. }
  90. mUMA = filePathCallback;
  91. Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  92. if (takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null) {
  93. File photoFile = null;
  94. try {
  95. photoFile = createImageFile();
  96. takePictureIntent.putExtra("PhotoPath", mCM);
  97. } catch (IOException ex) {
  98. Log.e(TAG, "Image file creation failed", ex);
  99. }
  100. if (photoFile != null) {
  101. mCM = "file:" + photoFile.getAbsolutePath();
  102. takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
  103. } else {
  104. takePictureIntent = null;
  105. }
  106. }
  107. Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
  108. contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
  109. contentSelectionIntent.setType("*/*");
  110. if (multiple_files) {
  111. contentSelectionIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
  112. }
  113. Intent[] intentArray;
  114. if (takePictureIntent != null) {
  115. intentArray = new Intent[]{takePictureIntent};
  116. } else {
  117. intentArray = new Intent[0];
  118. }
  119.  
  120. Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
  121. chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
  122. chooserIntent.putExtra(Intent.EXTRA_TITLE, "File Chooser");
  123. chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
  124. startActivityForResult(chooserIntent, FCR);
  125. return true;
  126. }else{
  127. return false;
  128. }
  129. }
  130. });
  131. webviewku.loadUrl("http://makole.id/");
  132. }
  133.  
  134. public boolean onKeyDown(int keyCode, KeyEvent event) {
  135. //ketika disentuh tombol back
  136. if ((keyCode == KeyEvent.KEYCODE_BACK) && webviewku.canGoBack()) {
  137. webviewku.goBack(); //method goback(),untuk kembali ke halaman sebelumnya
  138. return true;
  139. }
  140. // Jika tidak ada halaman yang pernah dibuka
  141. // maka akan keluar dari activity (tutup aplikasi)
  142. return super.onKeyDown(keyCode, event);
  143. }
  144.  
  145. private class MyBrowser extends WebViewClient {
  146. @Override
  147. public boolean shouldOverrideUrlLoading(WebView view, String url) {
  148. view.loadUrl(url);
  149. return true;
  150. }
  151. }
  152.  
  153. @Override
  154. protected void onActivityResult(int requestCode, int resultCode, Intent intent){
  155. super.onActivityResult(requestCode, resultCode, intent);
  156. if(Build.VERSION.SDK_INT >= 21){
  157. Uri[] results = null;
  158. //checking if response is positive
  159. if(resultCode== Activity.RESULT_OK){
  160. if(requestCode == FCR){
  161. if(null == mUMA){
  162. return;
  163. }
  164. if(intent == null || intent.getData() == null){
  165. if(mCM != null){
  166. results = new Uri[]{Uri.parse(mCM)};
  167. }
  168. }else{
  169. String dataString = intent.getDataString();
  170. if(dataString != null){
  171. results = new Uri[]{Uri.parse(dataString)};
  172. } else {
  173. if(multiple_files) {
  174. if (intent.getClipData() != null) {
  175. final int numSelectedFiles = intent.getClipData().getItemCount();
  176. results = new Uri[numSelectedFiles];
  177. for (int i = 0; i < numSelectedFiles; i++) {
  178. results[i] = intent.getClipData().getItemAt(i).getUri();
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }
  185. }
  186. mUMA.onReceiveValue(results);
  187. mUMA = null;
  188. }else{
  189. if(requestCode == FCR){
  190. if(null == mUM) return;
  191. Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
  192. mUM.onReceiveValue(result);
  193. mUM = null;
  194. }
  195. }
  196. }
  197.  
  198.  
  199.  
  200. //callback reporting if error occurs
  201. public class Callback extends WebViewClient{
  202. public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
  203. Toast.makeText(getApplicationContext(), "Failed loading app!", Toast.LENGTH_SHORT).show();
  204. }
  205. }
  206.  
  207. public boolean file_permission(){
  208. if(Build.VERSION.SDK_INT >=23 && (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)) {
  209. ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
  210. return false;
  211. }else{
  212. return true;
  213. }
  214. }
  215.  
  216. //creating new image file here
  217. private File createImageFile() throws IOException{
  218. @SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  219. String imageFileName = "img_"+timeStamp+"_";
  220. File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
  221. return File.createTempFile(imageFileName,".jpg",storageDir);
  222. }
  223.  
  224.  
  225. @Override
  226. public void onConfigurationChanged(Configuration newConfig){
  227. super.onConfigurationChanged(newConfig);
  228. }
  229.  
  230.  
  231. }
Bukhori Muslim
commented on 12 Apr 2019 07:00:47
  1. Assalam,.. alhamdulillah baik gan,
  2. terima kasih sudah berbagi, saya juga masih awam di android. :D
Maaf, Untuk Memberikan Komentar Anda Harus Login !!!