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);

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 :)