Tuesday, 14 March 2017

Resize Android image without losing quality


   Here I am going to explain how to convert image into scaled bitmap without losing quality




 public Bitmap resizeBitmap(String photoPath, int targetW, int targetH)
 {
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(photoPath, bmOptions);
        int photoW = bmOptions.outWidth;
        int photoH = bmOptions.outHeight;

        int scaleFactor = 1;
        if ((targetW > 0) || (targetH > 0)) {
            scaleFactor = Math.min(photoW/targetW, photoH/targetH);
        }

        bmOptions.inJustDecodeBounds = false;
        bmOptions.inSampleSize = scaleFactor;
        bmOptions.inPurgeable = true; //Deprecated API 21

        return BitmapFactory.decodeFile(bmOptionsphotoPath);
    }


Happy coding :)

Pass LinkedHashMap between activities


                   Here I am going to explain how to pass LikedHashMap data between activity.Before that we need to know GSON.Google Gson is a simple Java-based library to serialize Java objects to JSON and vice versa.

Add dependencies to gradle

  compile 'com.google.code.gson:gson:2.6.2'


First Activity

        Gson gson = new Gson();

        LinkedHashMap<String, String> map = new 
                                              LinkedHashMap<String, String>();
        map .put("Name", "Dhina");
        map .put("Blog", "Androiddhina");
        map .put("City", "Chennai");
        
        Intent intent = new Intent(activity, DetailsActivity.class);
        intent.putExtra("values",  map);
        startActivity(intent);


Second Activity

         LinkedHashMap<String, String> hashMap = new 
                                     LinkedHashMap<String, String>();

        String str = getIntent().getStringExtra("values");
        Gson gson = new Gson();
        Type entityType = new TypeToken
            <LinkedHashMap<String, String>>() {}.getType();
        hashMap = gson.fromJson(strentityType);


Happy coding :)


Wednesday, 19 October 2016

Android Image Preview Activity


                       Here I am going to explain how to show image preview in android using Dialog Fragment



                           


To pass image string to dailog fragment use bundle

               Bundle bundle = new Bundle();
               bundle.putString("data", imageData);


To call dialog fragment 

                FragmentManager fm = activity.getFragmentManager();
                ImageDialogFragment newFragment =                                                                                                   ImageDialogFragment.newInstance();
                newFragment .setArguments(bundle);

                newFragment .show(fm, "slideshow");




ImageDialogFragment.java


import android.app.DialogFragment;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ImageView;

 public class ImageDialogFragment extends DialogFragment {


   public    static ImageDialogFragment newInstance()
   {
        ImageDialogFragment f = new ImageDialogFragment();

        return ;
    }

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.image_popup, container, false);

        ImageView imageViewPreview =(ImageView)v
                                                .findViewById(R.id.image_preview);

        try {
              showImage(getArguments().getString("data"),imageViewPreview );
           }
       catch (Exception e)
       {

       }

        return v;
    }

    private void showImage(String data,ImageView image) {
   
          try {

            Bitmap bitmap = null;
            try {
                byte[] bytes = android.util.Base64.decode(data,                                                                                               android.util.Base64.DEFAULT);

                bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes .length);
                image.setImageBitmap(bitmap);


            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        catch (Exception e)
        {

        }
  }

   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
           setStyle(DialogFragment.STYLE_NORMAL,
             android.R.style.Theme_Black_NoTitleBar_Fullscreen);
       }
}


image_popup.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black">

    <ImageView
         android:id="@+id/close"
        android:background="@android:drawable/ic_menu_close_clear_cancel"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="27dp"
        android:layout_marginEnd="27dp"
        android:layout_marginTop="14dp" />

    <ImageView
        android:id="@+id/image_preview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitCenter"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>



Happy Coding :)
            

Tuesday, 20 September 2016

TreeMap vs LinkedHashMap

Here I am going to explain difference between HashMap vs TreeMap  vs LinkedHashMap

HashMap 

     HashMap accepts key-value pair,It allows null value for both key and Value and HashMap is Unsynchronized.

  HashMap<String,String> hashmap=new HashMap<String,String>();

  hashmap.put("1","Dhina");
  hashmap.put("2",null);


TreeMap  

    TreeMap accepts key-value pair,It didn't allows null value for key and  allows null value for Value and TreeMap is Unique.It is same as HashMap instead maintains ascending order.

  TreeMap <String,String> treemap=new TreeMap <String,String>();

  treemap.put("1","Dhina");
  treemap.put("2",null);




LinkedHashMap

    LinkedHashMap accepts key-value pair,It allows one null  key and  allows null value for Value and LinkedHashMap is Unique.It is same as HashMap instead maintains insertion order.

  LinkedHashMap <String,String> linkedhash=new LinkedHashMap<String,String>();

  linkedhash.put("1","Dhina");
  linkedhash.put("null",Karan);