30 Kasım 2017 Perşembe

Patterns Sınıfı

WEB_URL Alanı
Şöyle yaparız.
Patterns.WEB_URL.matcher(url).matches();

URLUtil Sınıfı

isvalid metodu
Şöyle yaparız.
URLUtil.isValidUrl(url);

BitmapFactory Sınıfı

decodeByteArray metodu
Örnek
Şöyle yaparız.
Bitmap bitmap = BitmapFactory.decodeByteArray(...);
Örnek
Şöyle yaparız.
byte[] bytes = ...;
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
decodeFile metodu
Şöyle yaparız.
File imgFile = new  File("/sdcard/Images/sample_image.jpg");
Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
Daha da sıkıştırarak okumak için şöyle yaparız.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inSampleSize = 2;  //you can also calculate your inSampleSize
options.inJustDecodeBounds = false;
options.inTempStorage = new byte[16 * 1024];

Bitmap bitmap = BitmapFactory.decodeFile(filePath,options);
decodeResource metodu
Şöyle yaparız.
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.myImage);
Şöyle yaparız.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inSampleSize = 1;
Bitmap bm = BitmapFactory.decodeResource(getResources(), resId, options);
decodeStream metodu
Şöyle yaparız. URL veya internetten bitmap yükleyebilmemizi sağlar.
Bitmap bitmap = BitmapFactory.decodeStream(...);

YuvImage Sınıfı

constructor
Şöyle yaparız
YuvImage yuv = new YuvImage(data, ImageFormat.YUY2, 1280, 720, null);
compressToJpeg metodu
Şöyle yaparız
ByteArrayOutputStream out = new ByteArrayOutputStream();
yuv.compressToJpeg(new Rect(0, 0, 1280, 720), 100, out);

29 Kasım 2017 Çarşamba

SQLiteOpenHelper Sınıfı

Giriş
Şu satırı dahil ederiz.
import android.database.sqlite.SQLiteOpenHelper;
Bu sınıf veritabanının kendisini yönetmek için var.

constructor - Context
Şöyle yaparız.
public class DatabaseHelper extends SQLiteOpenHelper {
  private static final String DB_NAME = "mytable";
  private static final int DB_VERSION = 1;

  public DatabaseHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
  }
  ...
}
Bu sınıf bir activity içinde onCreate metodunda yaratılır. Sınıfı yaratmak için activity "this" olarak constructor'a geçilir.
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.register);
  // initialize `helper` here
  helper = new DatabaseHelper(this);
  ....
}
Eğer istersek singleton da yapılabilir. Ama ben bu yöntemi sevmiyorum. O zaman şöyle yaparız.
DatabaseHelper.getInstance(context).someFunction(...);
delete metodu
3 parametre alır. İlk parametre tablo ismi, ikinci parametre sql, üçüncü parametre sql parametreleridir.

getReadableDatabase metodu
Şöyle elde ederiz.
SQLiteDatabase db = myDatabaseHelper.getReadableDatabase();
db.query () yapılabilir.

getWritableDatabase metodu
Şöyle elde ederiz.
SQLiteDatabase db = myDatabaseHelper.getWritableDatabase();
db.write yapılabilir.

insert metodu
3 parametre alır. İlk parametre tablo ismi, üçüncü parametre ContentValues nesnesidir.

Örnek
Şöyle yaparız.
void createFoo(Foo foo)
{
  SQLiteDatabase db = this.getWritableDatabase();

  ContentValues values = new ContentValues();
  values.put(KEY_NAME, foo.name);
  values.put(KEY_ID, foo.id);

  // Inserting Row
  db.insert(TABLE_FOO, null, values);

  db.close(); // Closing database connection
}
Örnek
Eğer istersek veritabanına yazma işlemini ayrı bir metoda taşıyabiliriz. Şöyle yaparız.
long internalInsert(SQLiteDatabase db, Foo obj)
{
  ContentValues values = new ContentValues();
  values.put(KEY_NAME, ...);
  values.put(KEY_ID, ...);

  return db.insert(TABLE_FOO, null, values);
}
onCreate metodu
Tabloyu yaratır.
public void onCreate(SQLiteDatabase db){
  String createTable = "CREATE TABLE mytable (...)";
  db.execSQL(createTable);
}

onUpgrade metodu
onUpgrade metodunun ilk parametresi SQLiteDatabase. Dolayısıyla bu sınıfın metodlarını kullanabiliriz.
Örnek - alter table
Şöyle yaparız.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

  // If you want to add a column
  if (newVersion > oldVersion) {
    db.execSQL("ALTER TABLE ADD_NAME ADD COLUMN NEW_COLOUM INTEGER DEFAULT 0");
  }
}
Örnek - drop table
Şöyle yaparız.


public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
  db.execSQL("Drop TABLE IF EXISTS" + DB_NAME);
  onCreate(db);
}
query metodu
Cursor nesnesi döner.
Örnek
Şöyle yaparız.
Foo getFoo (int id)
{
  SQLiteDatabase db = this.getReadableDatabase();

  Cursor cursor = db.query(TABLE_FOO, new String[] { KEY_ID,
    KEY_NAME}, KEY_ID + "=?",
    new String[] { String.valueOf(id) }, null, null, null, null);
  if (cursor != null)
    cursor.moveToFirst();

  Foo foo = new Foo (Integer.parseInt(cursor.getString(0)),
            cursor.getString(1));
  // return contact
  return foo;
}
rawQuery metodu
Cursor nesnesi döner. 2 parametre alır. İlk parametre sql, ikinci parametre sql parametreleridir.

Örnek
Şöyle yaparız.
Cursor cursor= db.rawQuery( "select id from contacts where email='a'",null);
Örnek
Şöyle yaparız.
ArrayList<Foo> getAllContents()
{
  ArrayList<Foo> array_list = new ArrayList<>();

  SQLiteDatabase db = this.getReadableDatabase();
  
  String selectQuery = "SELECT  * FROM " + TABLE_ITEM;
  Cursor res = db.rawQuery(selectQuery, null);
  res.moveToFirst();

  while (res.isAfterLast() == false)
  {
    Foo foo= new Foo();
    ...

  array_list.add(hashmap);
  res.moveToNext();
  }
  return array_list;
}
Örnek
Şöyle yaparız.
Cursor cursor = db.rawQuery("... ", null);

if (cursor.moveToFirst())
{
  do
  {
    long id = cursor.getLong(cursor.getColumnIndexOrThrow(ID));
    String title =  cursor.getString(cursor.getColumnIndexOrThrow(TITLE));
    ...
    list.add(new Foo(id, title);
  }
  while (cursor.moveToNext());
}
cursor.close();
return list;
update metodu
4 parametre alır. İlk parametre tablo ismi, ikinci parametre ContentValues nesnesi, üçüncü parametre where koşulu için sql, dördüncü parametre sql parametreleridir.
Şöyle yaparız.
public int updateFoo(Foo foo)
{
  SQLiteDatabase db = this.getWritableDatabase();

  ContentValues values = new ContentValues();
  values.put(KEY_NAME, foo.name);


  // updating row
  return db.update(TABLE_FOO, values, KEY_ID + " = ?",
    new String[] { String.valueOf(foo.id) });
}