Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.nurif.codelabs.maxistyle.adapter;
- import android.content.ContentResolver;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.net.Uri;
- import android.provider.MediaStore;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.widget.RecyclerView;
- import android.util.Base64;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ImageView;
- import android.widget.Toast;
- import com.nurif.codelabs.maxistyle.R;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import butterknife.BindView;
- import butterknife.ButterKnife;
- import id.zelory.compressor.Compressor;
- /**
- * Created by ifalan on 9/19/2017.
- */
- public class ImageVisitTailorAdapter extends RecyclerView.Adapter<ImageVisitTailorAdapter.ImageVisitTailorViewHolder> {
- Context context;
- String title;
- List<String> mCurrenPath= new ArrayList<>();
- List<Bitmap> items = new ArrayList<>();
- List<String> base64s = new ArrayList<>();
- public ImageVisitTailorAdapter(Context context) {
- this.context = context;
- }
- public void addData(Bitmap item) {
- this.items.add(item);
- this.base64s.add(setToBase64(item));
- notifyDataSetChanged();
- }
- public void addImagePath(String mCurrentPhotoPath){
- this.mCurrenPath.add(mCurrentPhotoPath);
- }
- public String setToBase64(Bitmap bitmap) {
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
- byte[] imagesByte = outputStream.toByteArray();
- return Base64.encodeToString(imagesByte, Base64.DEFAULT);
- }
- @Override
- public ImageVisitTailorAdapter.ImageVisitTailorViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- View v = LayoutInflater.from(parent.getContext())
- .inflate(R.layout.item_image_visit_tailor, parent, false);
- return new ImageVisitTailorAdapter.ImageVisitTailorViewHolder(v);
- }
- public class ImageVisitTailorViewHolder extends RecyclerView.ViewHolder {
- @BindView(R.id.iv_content)
- ImageView ivContent;
- @BindView(R.id.remove)
- ImageView remove;
- public ImageVisitTailorViewHolder(View itemView) {
- super(itemView);
- ButterKnife.bind(this, itemView);
- }
- }
- @Override
- public void onBindViewHolder(final ImageVisitTailorAdapter.ImageVisitTailorViewHolder holder, final int position) {
- final Bitmap data = items.get(position);
- Bitmap image = Bitmap.createScaledBitmap(data,280,360,false);
- // final File imageFile = new File(mCurrenPath.get(po sition));
- // try {
- //
- // image = new Compressor(context)
- // .setMaxWidth(100)
- // .setMaxHeight(100 )
- // .setQuality(100)
- // .setCompressFormat(Bitmap.CompressFormat.JPEG)
- // .compressToBitmap(imageFile);
- // } catch (IOException e) {
- // e.printStackTrace();
- // }
- holder.ivContent.setImageBitmap(image);
- holder.remove.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- AlertDialog.Builder alert = new AlertDialog.Builder(context);
- alert.setTitle("Delete Item ?");
- alert.setPositiveButton("Hapus", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- if (mCurrenPath.size()==0){
- items.remove(items.get(position));
- base64s.remove(base64s.get(position));
- }
- else{
- items.remove(items.get(position));
- base64s.remove(base64s.get(position));
- File file = new File(mCurrenPath.get(position));
- if (file.exists()){
- file.delete();
- }
- else{
- Toast.makeText(context, "Image not exist", Toast.LENGTH_SHORT).show();
- }
- context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(mCurrenPath.get(position)))));
- }
- notifyDataSetChanged();
- }
- });
- alert.setNegativeButton("Tidak",new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- // jika tombol ini diklik, akan menutup dialog
- // dan tidak terjadi apa2
- dialog.cancel();
- }
- });
- alert.setCancelable(false);
- alert.show();
- }
- });
- }
- @Override
- public int getItemCount() {
- return items.size();
- }
- public List<String> getBase64s() {
- return base64s;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement