SHARE
TWEET

activity upload

m_amin05 Dec 12th, 2019 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.andiarsipnaskahdigitalbengkayang;
  2.  
  3. import androidx.annotation.NonNull;
  4. import androidx.appcompat.app.AppCompatActivity;
  5. import androidx.core.app.ActivityCompat;
  6. import androidx.core.content.ContextCompat;
  7.  
  8. import android.Manifest;
  9. import android.content.Intent;
  10. import android.content.pm.PackageManager;
  11. import android.graphics.Bitmap;
  12. import android.net.Uri;
  13. import android.os.Bundle;
  14. import android.provider.MediaStore;
  15. import android.util.Base64;
  16. import android.util.Log;
  17. import android.view.View;
  18. import android.widget.Button;
  19. import android.widget.ImageView;
  20. import android.widget.Toast;
  21.  
  22. import com.andiarsipnaskahdigitalbengkayang.service.Service_Connector;
  23.  
  24. import org.json.JSONObject;
  25.  
  26. import java.io.ByteArrayOutputStream;
  27. import java.io.IOException;
  28. import java.util.HashMap;
  29. import java.util.Map;
  30.  
  31. public class ContohTambahActivity extends AppCompatActivity implements View.OnClickListener {
  32.     Button btnAdd, btnUpload;
  33.     ImageView imageView;
  34.     String isi_file;
  35.  
  36.     private Bitmap bitmap, decode;
  37.     private Uri filePath;
  38.  
  39.     private int PICK_IMAGE_REQUEST = 1;
  40.     private static final int STORAGE_PERMISSION_CODE = 123;
  41.  
  42.     @Override
  43.     protected void onCreate(Bundle savedInstanceState) {
  44.         super.onCreate(savedInstanceState);
  45.         setContentView(R.layout.activity_contoh_tambah);
  46.  
  47.         btnAdd = findViewById(R.id.btnAdd);
  48.         btnUpload = findViewById(R.id.btnUpload);
  49.         imageView = findViewById(R.id.imageView);
  50.  
  51.         btnAdd.setOnClickListener(this);
  52.         btnUpload.setOnClickListener(this);
  53.  
  54.     }
  55.  
  56.     @Override
  57.     public void onClick(View v) {
  58.         if (v == btnAdd) {
  59.             requestStoragePermission();
  60.             showFileChooser();
  61.         }
  62.         if (v == btnUpload) {
  63.             uploadImage(isi_file);
  64.             Log.i("daaata", isi_file);
  65.         }
  66.     }
  67.  
  68.     @Override
  69.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  70.         super.onActivityResult(requestCode, resultCode, data);
  71.  
  72.         if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
  73.             filePath = data.getData();
  74.             try {
  75.                 bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
  76.                 bitmaptostring(bitmap);
  77.                 imageView.setImageBitmap(bitmap);
  78.  
  79.             } catch (IOException e) {
  80.                 e.printStackTrace();
  81.             }
  82.         }
  83.     }
  84.  
  85.     private void showFileChooser() {
  86.         Intent intent = new Intent();
  87.         intent.setType("image/*");
  88.         intent.setAction(Intent.ACTION_GET_CONTENT);
  89.         startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
  90.     }
  91.  
  92.     private void requestStoragePermission() {
  93.         if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
  94.             return;
  95.  
  96.         if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
  97.             //If the user has denied the permission previously your code will come to this block
  98.             //Here you can explain why you need this permission
  99.             //Explain here why you need this permission
  100.         }
  101.         ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
  102.     }
  103.  
  104.     @Override
  105.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  106.  
  107.         //Checking the request code of our request
  108.         if (requestCode == STORAGE_PERMISSION_CODE) {
  109.  
  110.             //If permission is granted
  111.             if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  112.                 //Displaying a toast
  113.                 Toast.makeText(this, "Permission granted now you can read the storage", Toast.LENGTH_LONG).show();
  114.             } else {
  115.                 //Displaying another toast if permission is not granted
  116.                 Toast.makeText(this, "Oops you just denied the permission", Toast.LENGTH_LONG).show();
  117.             }
  118.         }
  119.     }
  120.  
  121.     public void bitmaptostring(Bitmap bitmap){
  122.         ByteArrayOutputStream bos = new ByteArrayOutputStream();
  123.         bitmap.compress(Bitmap.CompressFormat.PNG,100,bos);
  124.         byte[] bb = bos.toByteArray();
  125.         isi_file = Base64.encodeToString(bb, Base64.DEFAULT);
  126.     }
  127.  
  128.     private void uploadImage(String image){
  129.         Map<String, String> map = new HashMap();
  130.         map.put("image", image);
  131.         //untuk send post session request
  132.         Service_Connector.sendpostrequest(ContohTambahActivity.this, "upload.php", map, new Service_Connector.VolleyResponseListener_v3() {
  133.             @Override
  134.             public void onError(String message) {
  135.                 Toast.makeText(ContohTambahActivity.this, "Gagal menambahkan arsip", Toast.LENGTH_SHORT).show();
  136.             }
  137.  
  138.             @Override
  139.             public void onResponese(String response) {
  140.                 try {
  141.                     JSONObject respon = new JSONObject(response);
  142.                     if (respon.getString("error").equals("false")) {
  143.                         Toast.makeText(ContohTambahActivity.this, "Data berhasil diperbaharui", Toast.LENGTH_SHORT).show();
  144. //                        Intent intent = new Intent(ContohTambahActivity.this, DetailArsipActivity.class);
  145. //                        startActivity(intent);
  146. //                        finish();
  147.  
  148.                     } else if (respon.getString("error").equals("true")){
  149.                         Toast.makeText(ContohTambahActivity.this, respon.getString("message"), Toast.LENGTH_SHORT).show();
  150.                     }
  151.  
  152.                 } catch (Exception e) {
  153.  
  154.                 }
  155.  
  156.             }
  157.  
  158.             @Override
  159.             public void onNoConnection(String message) {
  160.                 Toast.makeText(ContohTambahActivity.this, "Tidak ada koneksi internet", Toast.LENGTH_SHORT).show();
  161.             }
  162.  
  163.             @Override
  164.             public void OnServerError(String message) {
  165.                 Toast.makeText(ContohTambahActivity.this, "Gagal memperbaharui arsip", Toast.LENGTH_SHORT).show();
  166.             }
  167.  
  168.             @Override
  169.             public void OnTimeOut() {
  170.                 Toast.makeText(ContohTambahActivity.this, "Habis waktu, coba lagi", Toast.LENGTH_SHORT).show();
  171.             }
  172.         });
  173.     }
  174. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top