Advertisement
darmawan76

ImageVisitAdapter

Oct 25th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1. package com.nurif.codelabs.maxistyle.adapter;
  2.  
  3. import android.content.ContentResolver;
  4. import android.content.Context;
  5. import android.content.DialogInterface;
  6. import android.content.Intent;
  7. import android.graphics.Bitmap;
  8. import android.graphics.BitmapFactory;
  9. import android.net.Uri;
  10. import android.provider.MediaStore;
  11. import android.support.v7.app.AlertDialog;
  12. import android.support.v7.widget.RecyclerView;
  13. import android.util.Base64;
  14. import android.view.LayoutInflater;
  15. import android.view.View;
  16. import android.view.ViewGroup;
  17. import android.widget.ImageView;
  18. import android.widget.Toast;
  19.  
  20. import com.nurif.codelabs.maxistyle.R;
  21.  
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27.  
  28. import butterknife.BindView;
  29. import butterknife.ButterKnife;
  30. import id.zelory.compressor.Compressor;
  31.  
  32. /**
  33. * Created by ifalan on 9/19/2017.
  34. */
  35.  
  36. public class ImageVisitTailorAdapter extends RecyclerView.Adapter<ImageVisitTailorAdapter.ImageVisitTailorViewHolder> {
  37. Context context;
  38. String title;
  39. List<String> mCurrenPath= new ArrayList<>();
  40.  
  41. List<Bitmap> items = new ArrayList<>();
  42. List<String> base64s = new ArrayList<>();
  43.  
  44. public ImageVisitTailorAdapter(Context context) {
  45. this.context = context;
  46. }
  47.  
  48. public void addData(Bitmap item) {
  49. this.items.add(item);
  50. this.base64s.add(setToBase64(item));
  51. notifyDataSetChanged();
  52. }
  53. public void addImagePath(String mCurrentPhotoPath){
  54. this.mCurrenPath.add(mCurrentPhotoPath);
  55. }
  56.  
  57. public String setToBase64(Bitmap bitmap) {
  58. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  59. bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
  60. byte[] imagesByte = outputStream.toByteArray();
  61.  
  62. return Base64.encodeToString(imagesByte, Base64.DEFAULT);
  63. }
  64.  
  65. @Override
  66. public ImageVisitTailorAdapter.ImageVisitTailorViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  67. View v = LayoutInflater.from(parent.getContext())
  68. .inflate(R.layout.item_image_visit_tailor, parent, false);
  69.  
  70. return new ImageVisitTailorAdapter.ImageVisitTailorViewHolder(v);
  71. }
  72.  
  73. public class ImageVisitTailorViewHolder extends RecyclerView.ViewHolder {
  74. @BindView(R.id.iv_content)
  75. ImageView ivContent;
  76.  
  77. @BindView(R.id.remove)
  78. ImageView remove;
  79.  
  80. public ImageVisitTailorViewHolder(View itemView) {
  81. super(itemView);
  82. ButterKnife.bind(this, itemView);
  83. }
  84. }
  85.  
  86. @Override
  87. public void onBindViewHolder(final ImageVisitTailorAdapter.ImageVisitTailorViewHolder holder, final int position) {
  88. final Bitmap data = items.get(position);
  89. Bitmap image = Bitmap.createScaledBitmap(data,280,360,false);
  90. // final File imageFile = new File(mCurrenPath.get(po sition));
  91. // try {
  92. //
  93. // image = new Compressor(context)
  94. // .setMaxWidth(100)
  95. // .setMaxHeight(100 )
  96. // .setQuality(100)
  97. // .setCompressFormat(Bitmap.CompressFormat.JPEG)
  98. // .compressToBitmap(imageFile);
  99. // } catch (IOException e) {
  100. // e.printStackTrace();
  101. // }
  102.  
  103. holder.ivContent.setImageBitmap(image);
  104. holder.remove.setOnClickListener(new View.OnClickListener() {
  105. @Override
  106. public void onClick(View v) {
  107. AlertDialog.Builder alert = new AlertDialog.Builder(context);
  108. alert.setTitle("Delete Item ?");
  109. alert.setPositiveButton("Hapus", new DialogInterface.OnClickListener() {
  110. @Override
  111. public void onClick(DialogInterface dialog, int which) {
  112. if (mCurrenPath.size()==0){
  113. items.remove(items.get(position));
  114. base64s.remove(base64s.get(position));
  115. }
  116.  
  117. else{
  118. items.remove(items.get(position));
  119. base64s.remove(base64s.get(position));
  120. File file = new File(mCurrenPath.get(position));
  121. if (file.exists()){
  122. file.delete();
  123. }
  124. else{
  125. Toast.makeText(context, "Image not exist", Toast.LENGTH_SHORT).show();
  126. }
  127. context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(mCurrenPath.get(position)))));
  128. }
  129. notifyDataSetChanged();
  130. }
  131. });
  132. alert.setNegativeButton("Tidak",new DialogInterface.OnClickListener() {
  133. public void onClick(DialogInterface dialog, int id) {
  134. // jika tombol ini diklik, akan menutup dialog
  135. // dan tidak terjadi apa2
  136. dialog.cancel();
  137. }
  138. });
  139. alert.setCancelable(false);
  140. alert.show();
  141. }
  142. });
  143. }
  144.  
  145. @Override
  146. public int getItemCount() {
  147. return items.size();
  148. }
  149.  
  150. public List<String> getBase64s() {
  151. return base64s;
  152. }
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement