Friday, 4 August 2017

Capture Image & Resize and adding Text WaterMark Android

    static final int REQUEST_IMAGE_CAPTURE = 1;
    Uri photoURI;
    File file;
    String imagepath;
    BitmapFactory.Options bmOptions;
    Bitmap bitmap;
private void dispatchTakePictureIntent() { if (loadingDialog != null && loadingDialog.isShowing()==false) loadingDialog.show(); Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); String root = Environment.getExternalStorageDirectory().toString(); File myDir = new File(root + "/saved_images"); myDir.mkdirs(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss"); String fileName = dateFormat.format(new Date()) + ".png"; file = new File(myDir, fileName); photoURI = Uri.fromFile(file); if (takePictureIntent.resolveActivity(getActivity().getPackageManager())
         != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

    public void resize(File file, String benchMark) {
        try {
            bmOptions = new BitmapFactory.Options();
            bmOptions.inJustDecodeBounds = false;
            bmOptions.inPreferredConfig = Bitmap.Config.RGB_565;
            bmOptions.inDither = true;
            bitmap = BitmapFactory.decodeFile(imagepath, bmOptions);
            int w = bitmap.getWidth();
            int h = bitmap.getHeight();
            Log.e("width & Height", "width " + bitmap.getWidth());
            if (bitmap.getWidth() > 1200) {
                w = bitmap.getWidth() * 20 / 100;
                h = bitmap.getHeight() * 20 / 100;
            }

            Log.e("width & Height", "width " + w + " height " + h);
            bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);

            Canvas c = new Canvas(bitmap);
            Paint p = new Paint();
            p.setColor(getResources().getColor(R.color.bgcolor));
            p.setStyle(Paint.Style.FILL_AND_STROKE);
           // paint.setColor(Color.BLACK);            p.setTextSize(20);
            c.drawText(benchMark, 10, 20, p);

            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, bytes);
            try {
                Log.e("Compressing", "Compressing");
                FileOutputStream fo = new FileOutputStream(file);
                fo.write(bytes.toByteArray());
                fo.close();
            } catch (Exception e) {
                Log.e("Exception", "Image Resizing" + e.getMessage());
            }
    }

    catch(
    Exception e
    )

}


@Overridepublic void onActivityResult(int requestCode, int resultCode,
 Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
 if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == getActivity()
   .RESULT_OK)
 {
imagepath = photoURI.getPath();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
resize(file, dateFormat.format(date));

}
}

Store Complete Object in SharedPrefrence


Add Given Dependancy in your gradle file-
compile 'com.google.code.gson:gson:2.7'
and Add this class -

public class Shprefrences {

    public static final String PREFERENCES = "Data";
    Context context;
    SharedPreferences sharedpreferences;
    SharedPreferences.Editor editor;

    public void clearData()
    {
        editor.remove("Data");
        editor.clear();
        editor.commit();
    }

    public Shprefrences(Context context) {
        this.context = context;
        sharedpreferences = context.getSharedPreferences(PREFERENCES,
 Context.MODE_PRIVATE);
        editor = sharedpreferences.edit();
    }

    public void setString(String key, String val) {
        editor.putString(key, val);
        editor.commit();
    }

    public void setBoolean(String key, boolean val) {
        editor.putBoolean(key, val);
        editor.commit();
    }

    public void setInt(String key, int val) {
        editor.putInt(key, val);
        editor.commit();
    }


    public String getString(String key, String val) {
        return sharedpreferences.getString(key,val).toString();
    }

    public boolean getBoolean(String key, boolean val) {
        return sharedpreferences.getBoolean(key,val);
    }


    public void setFrmBasicObject(String key, FormDataModel obj)
    {
        Gson gson = new Gson();
        String json = gson.toJson(obj);
        editor.putString(key, json);
        editor.commit();
    }

    public FormDataModel getFrmBasicObject(String key)
    {
        Gson gson = new Gson();
        String json = sharedpreferences.getString(key, "");
        FormDataModel ob= gson.fromJson(json, FormDataModel.class);
        return ob;
    }

    public void setCompanyModel(String key, ArrayList<CompanyNameModel> obj)
    {
        Gson gson = new Gson();
        String json = gson.toJson(obj);
        editor.putString(key, json);
        editor.commit();
    }

    public ArrayList<CompanyNameModel> getComapnyModel(String key)
    {
        Gson gson = new Gson();
        String json = sharedpreferences.getString(key, "");
        Type type = new TypeToken<ArrayList<CompanyNameModel>>() {}.getType();
        ArrayList<CompanyNameModel> arrayList = gson.fromJson(json, type);
        return arrayList;
    }


    public void setLoginModel(String key, LoginModel obj)
    {
        Gson gson = new Gson();
        String json = gson.toJson(obj);
        editor.putString(key, json);
        editor.commit();
    }

    public LoginModel getLoginModel(String key)
    {
        Gson gson = new Gson();
        String json = sharedpreferences.getString(key, "");
        LoginModel ob= gson.fromJson(json, LoginModel.class);
        return ob;
    }

    public void setCaseListModel(String key, ArrayList<CaseListModel> obj)
    {
        Gson gson = new Gson();
        String json = gson.toJson(obj);
        editor.putString(key, json);
        editor.commit();
    }

    public ArrayList<CaseListModel> getCaseListModel(String key)
    {
        Gson gson = new Gson();
        String json = sharedpreferences.getString(key, "");
        Type type = new TypeToken<ArrayList<CaseListModel>>() {}.getType();
        ArrayList<CaseListModel> arrayList = gson.fromJson(json, type);
        return arrayList;
    }

    public int getInt(String key, int val) {
        return sharedpreferences.getInt(key,val);
    }

}

Getting App Version Code of PlayStore


Add given dependency in your gradle file-

compile 'org.jsoup:jsoup:1.8.3'

and add this code in your Activity or Fragment

private class GetVersionCode extends AsyncTask<Void, String, String> {
    @Override    protected String doInBackground(Void... voids) {

        String newVersion = null;
        try {
            newVersion = Jsoup.connect("https://play.google.com/store/apps/
details?id="+ getActivity().getPackageName() + "&hl=it")
                    .timeout(30000)
                    .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US;
 rv1.8.1.6)Gecko/20070725 Firefox/2.0.0.6")
                    .referrer("http://www.google.com")
                    .get()
                    .select("div[itemprop=softwareVersion]")
                    .first()
                    .ownText();
            return newVersion;
        } catch (Exception e) {
            return newVersion;
        }
    }

    @Override    protected void onPostExecute(String onlineVersion) {
        super.onPostExecute(onlineVersion);
        if (onlineVersion != null && !onlineVersion.isEmpty()) {
            if (Float.valueOf(currentVersion) < Float.valueOf(onlineVersion)) {
                showDialog();
            }
        }
    }
}


currentVersion = getActivity().getPackageManager().getPackageInfo(getActivity()
.getPackageName(), 0).versionName;
GetVersionCode ver = new GetVersionCode();
ver.execute();