rikisaraan

AutoCompleteTextView get ID

Jun 13th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 31.85 KB | None | 0 0
  1. package com.japati14.ngabsensecurityguaindividual;
  2.  
  3. import android.Manifest;
  4. import android.app.Activity;
  5. import android.app.AlertDialog;
  6. import android.app.ProgressDialog;
  7. import android.content.Context;
  8. import android.content.DialogInterface;
  9. import android.content.Intent;
  10. import android.content.SharedPreferences;
  11. import android.content.pm.PackageManager;
  12. import android.graphics.Bitmap;
  13. import android.graphics.BitmapFactory;
  14. import android.net.Uri;
  15. import android.os.AsyncTask;
  16. import android.os.Bundle;
  17. import android.os.Environment;
  18. import android.os.StrictMode;
  19. import android.text.Spannable;
  20. import android.text.SpannableString;
  21. import android.util.Log;
  22. import android.view.MenuItem;
  23. import android.view.View;
  24. import android.view.ViewGroup;
  25. import android.widget.ArrayAdapter;
  26. import android.widget.AutoCompleteTextView;
  27. import android.widget.Button;
  28. import android.widget.EditText;
  29. import android.widget.ImageView;
  30. import android.widget.LinearLayout;
  31. import android.widget.Toast;
  32.  
  33. import androidx.appcompat.app.AppCompatActivity;
  34. import androidx.core.content.ContextCompat;
  35.  
  36. import com.android.volley.AuthFailureError;
  37. import com.android.volley.DefaultRetryPolicy;
  38. import com.android.volley.Request;
  39. import com.android.volley.Response;
  40. import com.android.volley.VolleyError;
  41. import com.android.volley.VolleyLog;
  42. import com.android.volley.toolbox.JsonArrayRequest;
  43. import com.android.volley.toolbox.StringRequest;
  44. import com.japati14.ngabsensecurityguaindividual.app.AppController;
  45. import com.japati14.ngabsensecurityguaindividual.data.DataKategoriLaporan;
  46. import com.japati14.ngabsensecurityguaindividual.data.DataLaporanKLB;
  47. import com.japati14.ngabsensecurityguaindividual.util.AlertDialogMsg;
  48. import com.japati14.ngabsensecurityguaindividual.util.RequestPermissionHandler;
  49. import com.japati14.ngabsensecurityguaindividual.util.Server;
  50. import com.japati14.ngabsensecurityguaindividual.util.TypefaceSpan;
  51. import com.kosalgeek.android.photoutil.GalleryPhoto;
  52. import com.kosalgeek.android.photoutil.ImageBase64;
  53. import com.kosalgeek.android.photoutil.PhotoLoader;
  54.  
  55. import org.json.JSONArray;
  56. import org.json.JSONException;
  57. import org.json.JSONObject;
  58.  
  59. import java.io.File;
  60. import java.io.FileNotFoundException;
  61. import java.io.IOException;
  62. import java.net.MalformedURLException;
  63. import java.net.URL;
  64. import java.text.SimpleDateFormat;
  65. import java.util.ArrayList;
  66. import java.util.Arrays;
  67. import java.util.Date;
  68. import java.util.HashMap;
  69. import java.util.List;
  70. import java.util.Locale;
  71. import java.util.Map;
  72.  
  73. import static com.japati14.ngabsensecurityguaindividual.util.Constants.EXTRA_ITEM;
  74. import static com.japati14.ngabsensecurityguaindividual.util.Constants.TIME_OUT_POST;
  75.  
  76.  
  77. public class LaporanKLB extends AppCompatActivity {
  78.     public static final String mypreference = "mypref";
  79.     SharedPreferences sharedpreferences;
  80.     AlertDialogMsg msg = new AlertDialogMsg();
  81.     private static String url_insert_laporan   = Server.URL + "insertLaporanKLB.php";
  82.     private static String url_upload_image   = Server.URL + "uploadImage.php";
  83.  
  84.     private static final String TAG = LaporanKLB.class.getSimpleName();
  85.     String laporanContent,laporanTitle,store_id,path,KategoriLaporanId;
  86.     String Ilaporan_id,Ilaporan_title,Ilaporan_content,Ilaporan_date_created,staf_no,stafPelapor;
  87.  
  88.     EditText txtLaporanContent,txtLaporanTitle,etStafName,etLaporanDate;
  89.     GalleryPhoto galleryPhoto;
  90.     Uri fileUri;
  91.     final int GALLERY_REQUEST = 1200;
  92.     public final int REQUEST_CAMERA = 0;
  93.     Intent intent;
  94.     LinearLayout linearMain;
  95.     Bitmap bitmap;
  96.     ArrayList<String> imageList = new ArrayList<>();
  97.     ArrayList<String> imageRemove = new ArrayList<>();
  98.     final CharSequence[] items = {"Remove","Cancel"};
  99.     private Button ivGallery;
  100.     private static String URLImage   = Server.URL_IMAGE;
  101.     private RequestPermissionHandler mRequestPermissionHandler;
  102.     ArrayList<String> listKategoriLaporan = new ArrayList<String>();
  103.     List<DataKategoriLaporan> dataKategoriLaporan = new ArrayList<>();
  104.     AutoCompleteTextView acKategoriLaporan;
  105.  
  106.  
  107.     class GetData extends AsyncTask<String, Void, String> {
  108.  
  109.         Bitmap bitmap;
  110.         ImageView image = new ImageView(LaporanKLB.this);
  111.  
  112.         @Override
  113.         protected void onPreExecute() {
  114.         };
  115.  
  116.         @Override
  117.         protected String doInBackground(String... params) {
  118.             String ImageString = params[0];
  119.             try {
  120.                 URL url = new URL(URLImage+ImageString);
  121.                 bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
  122.             }catch (MalformedURLException e1) {
  123.                 e1.printStackTrace();
  124.             }
  125.             catch (IOException e) {
  126.                 e.printStackTrace();
  127.             }
  128.             String imgv  = (ImageString.equals("")) ? "null": ImageString;
  129.             return imgv;
  130.         }
  131.  
  132.         @Override
  133.         protected void onPostExecute(final String result) {
  134.             super.onPostExecute(result);
  135.  
  136.             final String imageString = result;
  137.  
  138.             if(!imageString.equals("null")){
  139.                 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
  140.                 layoutParams.setMargins(0, 0, 0, 50);
  141.                 image.setLayoutParams(layoutParams);
  142.                 image.setScaleType(ImageView.ScaleType.FIT_CENTER);
  143.                 image.setPadding(10, 10, 10, 10);
  144.                 image.setAdjustViewBounds(true);
  145.                 image.setBackgroundResource(R.drawable.border_image);
  146.                 image.setImageBitmap(bitmap);
  147.                 path = URLImage+imageString;
  148.                 imageList.add(path);
  149.                 linearMain.addView(image);
  150.                 Log.e(TAG,"Path : "+path);
  151.             }
  152.  
  153.         }
  154.     }
  155.  
  156.     public void getGambarLaporan(String laporan_id){
  157.  
  158.         String UrlGetGambarLaporan= Server.URL+"getGambarLaporan.php&laporan_id="+laporan_id;
  159.  
  160.         Log.e(TAG,UrlGetGambarLaporan);
  161.         // membuat request JSON
  162.         JsonArrayRequest jArr = new JsonArrayRequest(UrlGetGambarLaporan, new Response.Listener<JSONArray>() {
  163.             @Override
  164.             public void onResponse(JSONArray response) {
  165.             Log.d(TAG, response.toString());
  166.             if(response.length()<1){
  167.                 msg.messageDialog( LaporanKLB.this, "Informasi","Tidak ada gambar", "OK",null, null, null);
  168.                 //Log.e(TAG,response.length()+" tidak ada gmbar");
  169.             }else{
  170.                 // Parsing json
  171.                 for (int i = 0; i < response.length(); i++) {
  172.                     try {
  173.                         JSONObject obj = response.getJSONObject(i);
  174.                         new GetData().execute(obj.getString("img_name"));
  175.                     } catch (JSONException e) {
  176.                         e.printStackTrace();
  177.                     }
  178.                 }
  179.             }
  180.  
  181.             }
  182.         }, new Response.ErrorListener() {
  183.  
  184.             @Override
  185.             public void onErrorResponse(VolleyError error) {
  186.                 //VolleyLog.d(TAG, "Error: " + error.getMessage());
  187.                 Log.e(TAG,error.getMessage());
  188.                 msg.messageDialog( LaporanKLB.this, "Informasi","Check your connecton to server", "OK",null, null, null);
  189.             }
  190.         });
  191.  
  192.         // menambah request ke request queue
  193.         AppController.getInstance().addToRequestQueue(jArr);
  194.  
  195.     }
  196.  
  197.     public boolean onOptionsItemSelected(MenuItem item) {
  198.         switch(item.getItemId()) {
  199.             case android.R.id.home:
  200.                 finish();
  201.                 return true;
  202.         }
  203.         return(super.onOptionsItemSelected(item));
  204.     }
  205.  
  206.     @Override
  207.     protected void onCreate(Bundle savedInstanceState) {
  208.         super.onCreate(savedInstanceState);
  209.         setContentView(R.layout.activity_laporan_klb);
  210.         SpannableString s = new SpannableString("Ngabsen");
  211.         s.setSpan(new TypefaceSpan(this, "Handycheera.otf"), 0, s.length(),
  212.                 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  213.  
  214.         androidx.appcompat.app.ActionBar actionBar = getSupportActionBar();
  215.         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  216.         getSupportActionBar().setDisplayUseLogoEnabled(true);
  217.         actionBar.setTitle(s);
  218.  
  219.         sharedpreferences = getSharedPreferences(mypreference, Context.MODE_PRIVATE);
  220.         store_id= sharedpreferences.getString("store_id", "");
  221.  
  222.         txtLaporanContent = (EditText) findViewById(R.id.txtLaporanContent);
  223.         txtLaporanTitle = (EditText) findViewById(R.id.txtLaporanTitle);
  224.         etStafName = findViewById(R.id.et_staf_name);
  225.         etLaporanDate = findViewById(R.id.et_laporan_date);
  226.         acKategoriLaporan = findViewById(R.id.ac_kategori_laporan);
  227.  
  228.         Button btnKirim = (Button)findViewById(R.id.btnKirim);
  229.         linearMain = (LinearLayout)findViewById(R.id.linearMain);
  230.         galleryPhoto = new GalleryPhoto(getApplicationContext());
  231.  
  232.         getListKategoriLaporan();
  233.  
  234.         btnKirim.setOnClickListener(new Button.OnClickListener(){
  235.             public void onClick(View v){
  236.                 sharedpreferences = getSharedPreferences(mypreference,Context.MODE_PRIVATE);
  237.                 staf_no = sharedpreferences.getString("staf_no", "");
  238.                 KategoriLaporanId = dataKategoriLaporan.get(listKategoriLaporan.indexOf(acKategoriLaporan.getText().toString())).getKategori_klb_id();
  239.  
  240.                 laporanContent = txtLaporanContent.getText().toString();
  241.                 laporanTitle = txtLaporanTitle.getText().toString();
  242.  
  243.                 Log.e(TAG, "Laporan Content : " + laporanContent);
  244.  
  245.                 if(laporanContent.equals("") || laporanTitle.equals("")){
  246.                     msg.messageDialog(LaporanKLB.this,"Informasi","Judul dan Isi tidak boleh kosong","OK", null, null, null);
  247.                 }else{
  248.                     Runnable ifTrue = new Runnable() {
  249.                         @Override
  250.                         public void run() {
  251.                             insertLaporan();
  252.                         }
  253.                     };
  254.  
  255.                     msg.messageDialog(LaporanKLB.this,"Informasi","Anda yakin ingin mengirim laporan ini ?","Ya", "Tidak", ifTrue, null);
  256.                 }
  257.  
  258.             }
  259.         });
  260.  
  261.  
  262.         Button ivGallery = (Button) findViewById(R.id.ivGallery);
  263.         ivGallery.setOnClickListener(new Button.OnClickListener() {
  264.             @Override
  265.             public void onClick(View v) {
  266.                 final CharSequence[] items = {"Take Photo", "Choose from Library",
  267.                         "Cancel"};
  268.  
  269.                 if(imageList.size() >2){
  270.                     msg.messageDialog(LaporanKLB.this,"Informasi","Gambar yang boleh dilampirkan maksimal 3","OK", null, null, null);
  271.                 }else{
  272.                     android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(LaporanKLB.this);
  273.                     builder.setTitle("Add Photo");
  274.                     builder.setIcon(R.mipmap.ic_launcher);
  275.                     builder.setItems(items, new DialogInterface.OnClickListener() {
  276.                         @Override
  277.                         public void onClick(DialogInterface dialog, int item) {
  278.                             if (items[item].equals("Take Photo")) {
  279.                                 callCamera();
  280.                             } else if (items[item].equals("Choose from Library")) {
  281.                                 callGalerry();
  282.                             } else if (items[item].equals("Cancel")) {
  283.                                 dialog.dismiss();
  284.                             }
  285.                         }
  286.                     });
  287.                     builder.show();
  288.                 }
  289.             }
  290.         });
  291.  
  292.         etStafName.setVisibility(View.GONE);
  293.         etLaporanDate.setVisibility(View.GONE);
  294.  
  295.         DataLaporanKLB laporanKLB = getIntent().getParcelableExtra(EXTRA_ITEM);
  296.         if(laporanKLB!=null){
  297.             ivGallery.setVisibility(View.GONE);
  298.             btnKirim.setVisibility(View.GONE);
  299.             txtLaporanTitle.setText(laporanKLB.getLaporan_title());
  300.             txtLaporanContent.setText(laporanKLB.getLaporan_content());
  301.             etStafName.setText(laporanKLB.getStafName());
  302.             etLaporanDate.setText(laporanKLB.getLaporan_date_created());
  303.             getGambarLaporan(laporanKLB.getLaporan_id());
  304.  
  305.             txtLaporanTitle.setEnabled(false);
  306.             txtLaporanContent.setEnabled(false);
  307.             etStafName.setEnabled(false);
  308.             etLaporanDate.setEnabled(false);
  309.             etLaporanDate.setVisibility(View.VISIBLE);
  310.             etStafName.setVisibility(View.VISIBLE);
  311.             etLaporanDate.setVisibility(View.VISIBLE);
  312.         }
  313.  
  314.  
  315.     }
  316.  
  317.  
  318.  
  319.     public void insertLaporan(){
  320.         final ProgressDialog progressDialog = new ProgressDialog(LaporanKLB.this, R.style.Theme_AppCompat_Light_Dialog);
  321.         progressDialog.setIndeterminate(true);
  322.         progressDialog.setMessage("Process  ...");
  323.         progressDialog.setCancelable(false);
  324.         progressDialog.show();
  325.  
  326.         StringRequest strReq = new StringRequest(Request.Method.POST, url_insert_laporan, new Response.Listener<String>() {
  327.  
  328.             @Override
  329.             public void onResponse(String response) {
  330.                 //Log.d(TAG, "Response: " + response.toString());
  331.  
  332.                 try {
  333.                     JSONObject jObj = new JSONObject(response);
  334.                     int success = jObj.getInt("success");
  335.  
  336.                     Log.e("Response",response);
  337.  
  338.                     // Cek error node pada json
  339.                     if (success == 1) {
  340.  
  341.                         final String pesan = jObj.getString("message");
  342.                         final String laporan_id = jObj.getString("laporan_id");
  343.  
  344.                         new android.os.Handler().postDelayed(
  345.                                 new Runnable() {
  346.                                     public void run() {
  347.                                         uploadImage(laporan_id);
  348.  
  349.                                         Runnable ifTrue = new Runnable() {
  350.                                             @Override
  351.                                             public void run() {
  352.                                                 Intent data = new Intent(LaporanKLB.this, DaftarLaporanKLB.class);
  353.                                                 startActivity(data);
  354.                                             }
  355.                                         };
  356.  
  357.                                         msg.messageDialog(LaporanKLB.this,"Informasi",pesan,"OK", null, ifTrue, null);
  358.                                         // onLoginFailed();
  359.                                         progressDialog.dismiss();
  360.                                     }
  361.                                 }, 3000);
  362.  
  363.                     } else {
  364.                         msg.messageDialog(LaporanKLB.this,"Informasi",jObj.getString("Message"),"OK",null, null, null);
  365.                         progressDialog.dismiss();
  366.                     }
  367.                 } catch (JSONException e) {
  368.                     // JSON error
  369.                     e.printStackTrace();
  370.                 }
  371.  
  372.             }
  373.         }, new Response.ErrorListener() {
  374.  
  375.             @Override
  376.             public void onErrorResponse(VolleyError error) {
  377.                 Log.e(TAG, "Error: " + error.getMessage());
  378.                 msg.messageDialog(LaporanKLB.this,"Informasi","Check your connecton to server "+error.getMessage(),"OK",null, null, null);
  379.                 progressDialog.dismiss();
  380.             }
  381.         }) {
  382.  
  383.             @Override
  384.             protected Map<String, String> getParams() {
  385.                 // Posting parameters ke post url
  386.                 Map<String, String> params = new HashMap<String, String>();
  387.  
  388.                 params.put("store_id", store_id);
  389.                 params.put("laporan_content", laporanContent);
  390.                 params.put("laporan_title", laporanTitle);
  391.                 params.put("staf_no", staf_no);
  392.                 return params;
  393.             }
  394.  
  395.         };
  396.  
  397.         strReq.setRetryPolicy(new DefaultRetryPolicy(
  398.                 TIME_OUT_POST,
  399.                 DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
  400.                 DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)
  401.         );
  402.  
  403.         AppController.getInstance().addToRequestQueue(strReq,"json_obj_req");
  404.     }
  405.  
  406.     public void showImageLog(){
  407.         for(String imagePath: imageList){
  408.             //Log.e(TAG,"List Array image : "+imagePath);
  409.         }
  410.         for(String remove: imageRemove){
  411.             //Log.e(TAG,"List array Remove : "+String.valueOf(Integer.parseInt(remove)-1));
  412.  
  413.         }
  414.     }
  415.  
  416.     public void cekarray(String bit,String index){
  417.         //Log.e(TAG,"Bitmap Yang dihapus : "+bit);
  418.         imageList.remove(bit);
  419.         if(!index.equals("")){
  420.             imageRemove.add(index);
  421.             showImageLog();
  422.         }
  423.     }
  424.  
  425.     public void uploadImage(final String laporan_id){
  426.         //Log.e(TAG,"detail_scoring_id : "+detail_scoring_id);
  427.         for(String imagePath: imageList){
  428.  
  429.             if(imagePath.contains("http")){
  430.                 //Log.e(TAG,"from URL : "+no_urut);
  431.             }else{
  432.                 //Log.e(TAG,"from Local : "+no_urut);
  433.                 actUploadImage(imagePath,laporan_id);
  434.             }
  435.         }
  436.     }
  437.  
  438.     public void actUploadImage(String imagePath,final String laporan_id){
  439.         try {
  440.             final String fileName;
  441.             bitmap = PhotoLoader.init().from(imagePath).requestSize(300, 300).getBitmap();
  442.             bitmap = getResizedBitmap(bitmap ,500);
  443.             File file = new File(imagePath);
  444.             fileName = file.getName();
  445.             //Log.e(TAG,"fromLocal : "+imagePath);
  446.             //Log.e(TAG,String.valueOf(bitmap));
  447.             final String encodedString = ImageBase64.encode(bitmap);
  448.  
  449.             //final String nama_file =detail_scoring_id+"_IMG_"+no_urut+".jpeg";
  450.             //Log.e(TAG,"Nama File : "+fileName);
  451.  
  452.             StringRequest strReq = new StringRequest(Request.Method.POST, url_upload_image, new Response.Listener<String>() {
  453.                 @Override
  454.                 public void onResponse(String response) {
  455.                     //Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
  456.                 }
  457.             }, new Response.ErrorListener() {
  458.                 @Override
  459.                 public void onErrorResponse(VolleyError error) {
  460.                     //Toast.makeText(getApplicationContext(), "Error while uploading image", Toast.LENGTH_SHORT).show();
  461.                 }
  462.             }){
  463.                 @Override
  464.                 protected Map<String, String> getParams() throws AuthFailureError {
  465.                     Map<String, String> params = new HashMap<>();
  466.                     //Log.e(TAG,"no urut : "+urut);
  467.  
  468.                     params.put("image", encodedString);
  469.                     params.put("laporan_id",laporan_id);
  470.                     params.put("nama_file",fileName);
  471.  
  472.                     return params;
  473.                 }
  474.             };
  475.             AppController.getInstance().addToRequestQueue(strReq, "json_obj_req");
  476.         } catch (Exception e) {
  477.             System.out.println(e.getMessage());
  478.             Toast.makeText(getApplicationContext(), "Error while loading image", Toast.LENGTH_SHORT).show();
  479.         }
  480.     }
  481.  
  482.  
  483.     @Override
  484.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  485.         super.onActivityResult(requestCode, resultCode, data);
  486.         if (resultCode == Activity.RESULT_OK) {
  487.             final ImageView imageView = new ImageView(getApplicationContext());
  488.             LinearLayout.LayoutParams layoutParams =
  489.                     new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
  490.             layoutParams.setMargins(0, 0, 0, 50);
  491.             if (requestCode == GALLERY_REQUEST) {
  492.                 try {
  493.                     galleryPhoto.setPhotoUri(data.getData());
  494.                     final String photoPath = galleryPhoto.getPath();
  495.                     //Log.e(TAG,"Alamat Photo : "+photoPath);
  496.                     bitmap = PhotoLoader.init().from(photoPath).requestSize(512, 512).getBitmap();
  497.                     imageView.setLayoutParams(layoutParams);
  498.                     imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
  499.                     imageView.setPadding(10, 10, 10, 10);
  500.                     imageView.setAdjustViewBounds(true);
  501.                     imageView.setBackgroundResource(R.drawable.border_image);
  502.                     imageView.setImageBitmap(bitmap);
  503.                     linearMain.addView(imageView);
  504.  
  505.                     if (imageRemove.size() < 1) {
  506.                         imageList.add(photoPath);
  507.                     } else {
  508.                         for (int i = 0; i < 1; i++) {
  509.                             //Log.e(TAG,"Set ke index : "+String.valueOf(Integer.parseInt(imageRemove.get(i))-1));
  510.                             imageList.add(Integer.parseInt(imageRemove.get(i)) - 1, photoPath);
  511.                             imageRemove.remove(i);
  512.                         }
  513.                         showImageLog();
  514.                     }
  515.  
  516.  
  517.                     imageView.setOnClickListener(new View.OnClickListener() {
  518.                         public void onClick(View v) {
  519.                             AlertDialog.Builder builder = new AlertDialog.Builder(LaporanKLB.this);
  520.                             builder.setTitle("Add Photo");
  521.                             builder.setIcon(R.mipmap.ic_launcher);
  522.                             builder.setItems(items, new DialogInterface.OnClickListener() {
  523.                                 @Override
  524.                                 public void onClick(DialogInterface dialog, int item) {
  525.                                     if (items[item].equals("Take Photo")) {
  526.                                         callCamera();
  527.                                     } else if (items[item].equals("Choose from Library")) {
  528.                                         callGalerry();
  529.                                     } else if (items[item].equals("Remove")) {
  530.                                         linearMain.removeView(imageView);
  531.                                         cekarray(photoPath, "");
  532.                                     } else if (items[item].equals("Cancel")) {
  533.                                         dialog.dismiss();
  534.                                     }
  535.                                 }
  536.                             });
  537.                             builder.show();
  538.                         }
  539.                     });
  540.  
  541.                 } catch (FileNotFoundException e) {
  542.                     Toast.makeText(getApplicationContext(), "Error while loading image", Toast.LENGTH_SHORT).show();
  543.                 }
  544.             } else {
  545.                 try {
  546.                     //Log.e("CAMERA", fileUri.getPath());
  547.                     final String photoPath = fileUri.getPath();
  548.                     bitmap = PhotoLoader.init().from(photoPath).requestSize(512, 512).getBitmap();
  549.                     imageView.setLayoutParams(layoutParams);
  550.                     imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
  551.                     imageView.setPadding(10, 10, 10, 10);
  552.                     imageView.setAdjustViewBounds(true);
  553.                     imageView.setBackgroundResource(R.drawable.border_image);
  554.                     imageView.setImageBitmap(bitmap);
  555.                     linearMain.addView(imageView);
  556.  
  557.                     //imageList.add(photoPath);
  558.                     if (imageRemove.size() < 1) {
  559.                         imageList.add(photoPath);
  560.                     } else {
  561.                         for (int i = 0; i < 1; i++) {
  562.                             //Log.e(TAG,"Set ke index : "+String.valueOf(Integer.parseInt(imageRemove.get(i))-1));
  563.                             imageList.add(Integer.parseInt(imageRemove.get(i)) - 1, photoPath);
  564.                             imageRemove.remove(i);
  565.                         }
  566.                         showImageLog();
  567.                     }
  568.  
  569.                     imageView.setOnClickListener(new View.OnClickListener() {
  570.                         public void onClick(View v) {
  571.                             AlertDialog.Builder builder = new AlertDialog.Builder(LaporanKLB.this);
  572.                             builder.setTitle("Add Photo");
  573.                             builder.setIcon(R.mipmap.ic_launcher);
  574.                             builder.setItems(items, new DialogInterface.OnClickListener() {
  575.                                 @Override
  576.                                 public void onClick(DialogInterface dialog, int item) {
  577.                                     if (items[item].equals("Take Photo")) {
  578.                                         callCamera();
  579.                                     } else if (items[item].equals("Choose from Library")) {
  580.                                         callGalerry();
  581.                                     } else if (items[item].equals("Remove")) {
  582.                                         linearMain.removeView(imageView);
  583.                                         cekarray(photoPath, "");
  584.                                     } else if (items[item].equals("Cancel")) {
  585.                                         dialog.dismiss();
  586.                                     }
  587.                                 }
  588.                             });
  589.                             builder.show();
  590.                         }
  591.                     });
  592.  
  593.                 } catch (Exception e) {
  594.                     e.printStackTrace();
  595.                 }
  596.             }
  597.         }
  598.     }
  599.  
  600.  
  601.     // Untuk resize bitmap
  602.     public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
  603.         int width = image.getWidth();
  604.         int height = image.getHeight();
  605.  
  606.         float bitmapRatio = (float) width / (float) height;
  607.         if (bitmapRatio > 1) {
  608.             width = maxSize;
  609.             height = (int) (width / bitmapRatio);
  610.         } else {
  611.             height = maxSize;
  612.             width = (int) (height * bitmapRatio);
  613.         }
  614.         return Bitmap.createScaledBitmap(image, width, height, true);
  615.     }
  616.  
  617.     public Uri getOutputMediaFileUri() {
  618.         return Uri.fromFile(getOutputMediaFile());
  619.         /*return FileProvider.getUriForFile(LaporanKLB.this, BuildConfig.APPLICATION_ID, getOutputMediaFile());
  620.         return FileProvider.getUriForFile(getApplicationContext(), "com.example.asd", getOutputMediaFile());*/
  621.     }
  622.  
  623.     private static File getOutputMediaFile() {
  624.  
  625.         // External sdcard location
  626.         File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Ngabsen");
  627.  
  628.         // Create the storage directory if it does not exist
  629.         if (!mediaStorageDir.exists()) {
  630.             if (!mediaStorageDir.mkdirs()) {
  631.                 Log.e("Monitoring", "Oops! Failed create Monitoring directory");
  632.                 return null;
  633.             }
  634.         }
  635.  
  636.         // Create a media file name
  637.         String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
  638.         File mediaFile;
  639.         mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_Ngabsen_" + timeStamp + ".jpg");
  640.  
  641.         return mediaFile;
  642.     }
  643.  
  644.  
  645.     public void callGalerry(){
  646.         int result = ContextCompat.checkSelfPermission(LaporanKLB.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
  647.         if (result == PackageManager.PERMISSION_GRANTED) {
  648.             Intent in = galleryPhoto.openGalleryIntent();
  649.             startActivityForResult(in, GALLERY_REQUEST);
  650.         } else {
  651.             //Requested permission.
  652.             mRequestPermissionHandler = new RequestPermissionHandler();
  653.             handleButtonClicked();
  654.         }
  655.  
  656.     }
  657.     public void callCamera(){
  658.         int result = ContextCompat.checkSelfPermission(LaporanKLB.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
  659.         if (result == PackageManager.PERMISSION_GRANTED) {
  660.             StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
  661.             StrictMode.setVmPolicy(builder.build());
  662.             intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  663.             fileUri = getOutputMediaFileUri();
  664.             intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, fileUri);
  665.             startActivityForResult(intent, REQUEST_CAMERA);
  666.         } else {
  667.             //Requested permission.
  668.             mRequestPermissionHandler = new RequestPermissionHandler();
  669.             handleButtonClicked();
  670.         }
  671.  
  672.     }
  673.  
  674.     public void handleButtonClicked(){
  675.         mRequestPermissionHandler.requestPermission(this, new String[] {
  676.                 android.Manifest.permission.ACCESS_FINE_LOCATION,android.Manifest.permission.CAMERA, android.Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE
  677.         }, 123, new RequestPermissionHandler.RequestPermissionListener() {
  678.             @Override
  679.             public void onSuccess() {
  680.                 //Toast.makeText(Login.this, "request permission success", Toast.LENGTH_SHORT).show();
  681.             }
  682.  
  683.             @Override
  684.             public void onFailed() {
  685.                 //Toast.makeText(Login.this, "request permission failed", Toast.LENGTH_SHORT).show();
  686.             }
  687.         });
  688.  
  689.     }
  690.  
  691.     @Override
  692.     public void onRequestPermissionsResult(int requestCode,String[] permissions,
  693.                                            int[] grantResults) {
  694.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  695.         mRequestPermissionHandler.onRequestPermissionsResult(requestCode, permissions,
  696.                 grantResults);
  697.     }
  698.  
  699.     private void getListKategoriLaporan() {
  700.         final String urlKategoriLaporan = Server.URL+"getKategoriLaporanKLB.php";
  701.  
  702.         JsonArrayRequest jArr = new JsonArrayRequest(urlKategoriLaporan, new Response.Listener<JSONArray>() {
  703.             @Override
  704.             public void onResponse(JSONArray response) {
  705.  
  706.                 listKategoriLaporan.clear();
  707.                 if (response.length() < 1) {
  708.                     AlertDialogMsg.messageDialog( LaporanKLB.this, "Informasi","Data Tidak Tersedia", "OK",null, null, null);
  709.                 } else {
  710.                     for (int i = 0; i < response.length(); i++) {
  711.                         try {
  712.                             JSONObject obj = response.getJSONObject(i);
  713.  
  714.                             DataKategoriLaporan item = new DataKategoriLaporan();
  715.  
  716.                             item.setKategori_klb_id(obj.getString("kategori_klb_id"));
  717.                             item.setKategori_klb_name(obj.getString("kategori_klb_name"));
  718.  
  719.                             // menambah item ke array
  720.                             dataKategoriLaporan.add(item);
  721.                             listKategoriLaporan.add(obj.getString("kategori_klb_name"));
  722.  
  723.                         } catch (JSONException e) {
  724.                             e.printStackTrace();
  725.                         }
  726.                     }
  727.                 }
  728.  
  729.                 ArrayAdapter<String> listKategori = new ArrayAdapter<String>
  730.                         (LaporanKLB.this, android.R.layout.simple_dropdown_item_1line, listKategoriLaporan);
  731.                 acKategoriLaporan.setThreshold(2);
  732.                 acKategoriLaporan.setAdapter(listKategori);
  733.  
  734.             }
  735.         }, new Response.ErrorListener() {
  736.  
  737.             @Override
  738.             public void onErrorResponse(VolleyError error) {
  739.                 Log.e(TAG, "Error.Response" + error.getMessage());
  740.  
  741.             }
  742.         });
  743.         // menambah request ke request queue
  744.         jArr.setRetryPolicy(new DefaultRetryPolicy(
  745.                 TIME_OUT_POST,
  746.                 DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
  747.                 DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)
  748.         );
  749.         AppController.getInstance().addToRequestQueue(jArr);
  750.     }
  751. }
Add Comment
Please, Sign In to add comment