Skip to content

Instantly share code, notes, and snippets.

Working from home

Aveek NsAveek

Working from home
View GitHub Profile
NsAveek /
Created Aug 19, 2018
The magic method createTable() to handle creation of the given fields automatically
private void createTable(String tableName, Map<String, String> fields, SQLiteDatabase db) {
Iterator iter = fields.entrySet().iterator();
String columns = "(";
Log.d("column", columns);
int counter = 1;
while (iter.hasNext()) {
Map.Entry mEntry = (Map.Entry);
columns += mEntry.getKey() + " " + mEntry.getValue();
if (counter == fields.size()) {
columns += ")";
NsAveek /
Last active Aug 19, 2018
The Actual DatabaseHelper class which is responsible for creating tables with default values in Sqlite
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "data.db";
private static final int DATABASE_VERSION = 1;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
public void onCreate(SQLiteDatabase db) {
NsAveek /
Last active Aug 19, 2018
Database tables
public class DatabaseInfo {
// Database Tables
public static String DATABASE_TABLE_A = "table_A";
public static String DATABASE_TABLE_A_DETAILS = "table_a_details";
public static String DATABASE_TABLE_B = "table_B";
public static String DATABASE_TABLE_B_DETAILS = "table_b_details";
public enum EnumFields {
// Add more types
private String fields;
Fields(String fields){
this.fields = fields;
public String getField(){
NsAveek / Builder
Created Sep 11, 2018
Builder Pattern example
View Builder
public class Student {
private String name;
private String address;
private String email;
private String phoneNumber;
private String passportNumber;
private int age;
NsAveek / IRepository
Created Oct 12, 2018
Repository interface
View IRepository
interface IStoreRepository {
fun saveStudentData(student: Student) : Boolean
fun updateStudentData(student: Student) : Int
fun deleteStudentData (student: Student) : Boolean
NsAveek / DatabaseRepository
Created Oct 12, 2018
The Database class with Repository pattern
View DatabaseRepository
class DatabaseRepository : IStoreRepository {
private var realm: Realm = Realm.getDefaultInstance()
override fun saveStudentData(student: Student): Boolean {
try {
realm.executeTransaction {
} catch (e: Exception) {
NsAveek /
Last active Nov 9, 2018
Select Video Using Intent
public void getFile(View view) {
Intent intent = new Intent();
startActivityForResult(Intent.createChooser(intent, "Select Video"), REQUEST_CODE_PICKER);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
NsAveek / UploadFile.kt
Created Oct 20, 2018
Upload file using Retrofit
View UploadFile.kt
private void uploadFile(File file){
RequestBody videoBody = RequestBody.create(MediaType.parse("video/"), file);
MultipartBody.Part vFile = MultipartBody.Part.createFormData("upload-file", file.getName(), videoBody);
UploadVideoService videoService = RetrofitClientInstance.Companion.getRetrofitInstance();
Call<ResponseBody> serverCom=videoService.uploadVideo(vFile);
serverCom.enqueue(new Callback<ResponseBody>() {
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
ResponseBody body = response.body();
try {
NsAveek / GetRealPathFromURI.kt
Created Nov 9, 2018
Pass an Uri and get the actual path of the file in device
View GetRealPathFromURI.kt
fun getRealVideoPathFromURI(contentResolver: ContentResolver,
contentURI: Uri): String? {
val cursor=contentResolver.query(contentURI, null, null, null, null)
if (cursor == null)
return contentURI.path
else {
val idx=cursor.getColumnIndex(MediaStore.Video.VideoColumns.DATA)
try {
return cursor.getString(idx)