Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added images to database #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions MemeifyMe/app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
<option name="SELECTED_TEST_ARTIFACT" value="_android_test_" />
<option name="ASSEMBLE_TASK_NAME" value="assembleDebug" />
<option name="COMPILE_JAVA_TASK_NAME" value="compileDebugSources" />
<option name="SOURCE_GEN_TASK_NAME" value="generateDebugSources" />
<option name="ASSEMBLE_TEST_TASK_NAME" value="assembleDebugAndroidTest" />
<option name="COMPILE_JAVA_TEST_TASK_NAME" value="compileDebugAndroidTestSources" />
<afterSyncTasks>
<task>generateDebugAndroidTestSources</task>
<task>generateDebugSources</task>
</afterSyncTasks>
<option name="TEST_SOURCE_GEN_TASK_NAME" value="generateDebugAndroidTestSources" />
<option name="ALLOW_USER_CONFIGURATION" value="false" />
<option name="MANIFEST_FILE_RELATIVE_PATH" value="/src/main/AndroidManifest.xml" />
<option name="RES_FOLDER_RELATIVE_PATH" value="/src/main/res" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

import android.content.ContentResolver;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.FrameLayout;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
Expand All @@ -26,6 +22,8 @@ public Bitmap loadBitmapFromView(FrameLayout view) {
return bm;
}



public void saveMeme(Bitmap bm, String imgName, ContentResolver c) {

OutputStream fOut = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
Expand All @@ -26,6 +28,8 @@
import android.widget.ToggleButton;
import android.widget.ViewSwitcher;

import accesscode.c4q.nyc.memeifyme.database.MyDB;


public class Template extends ActionBarActivity {
private Spinner drop;
Expand All @@ -37,15 +41,24 @@ public class Template extends ActionBarActivity {
private Bitmap photo;
private static final String photoSave = "photo";

// database
MyDB dba;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_template);

dba = new MyDB(this);
dba.open();



initializeViews();
setTypeAssets();


// Set up spinner and set drawables on select
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.memeArray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Expand All @@ -55,41 +68,61 @@ protected void onCreate(Bundle savedInstanceState) {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String str = adapterView.getItemAtPosition(i).toString().toLowerCase();
Drawable d;


switch (str) {
case "cool":
d = getResources().getDrawable(R.drawable.cool);
dba.insertUser("cool",drawableToBitmap(d));
draw(d);
break;
case "yao ming":
d = getResources().getDrawable(R.drawable.yaoming);
dba.insertUser("yao ming",drawableToBitmap(d));

draw(d);
break;
case "evil plotting raccoon":
d = getResources().getDrawable(R.drawable.evilplottingraccoon);

dba.insertUser("evil plotting raccoon",drawableToBitmap(d));

draw(d);
break;
case "philosoraptor":
d = getResources().getDrawable(R.drawable.philosoraptor);
dba.insertUser("philosoraptor",drawableToBitmap(d));

draw(d);
break;
case "socially awkward penguin":
d = getResources().getDrawable(R.drawable.sociallyawkwardpenguin);
dba.insertUser("socially awkward penguin",drawableToBitmap(d));

draw(d);
break;
case "success kid":
d = getResources().getDrawable(R.drawable.successkid);
dba.insertUser("success kid",drawableToBitmap(d));

draw(d);
break;
case "scumbag steve":
d = getResources().getDrawable(R.drawable.scumbagsteve);
dba.insertUser("scumbag steve",drawableToBitmap(d));

draw(d);
break;
case "one does not simply":
d = getResources().getDrawable(R.drawable.onedoesnotsimply);
dba.insertUser("one does not simply",drawableToBitmap(d));

draw(d);
break;
case "i don't always":
d = getResources().getDrawable(R.drawable.idontalways);
dba.insertUser("i don't always",drawableToBitmap(d));

draw(d);
break;
}
Expand Down Expand Up @@ -232,4 +265,28 @@ public void onClick(DialogInterface dialog, int id) {
builder.setView(layout);
return builder.create();
}

public static Bitmap drawableToBitmap (Drawable drawable) {
Bitmap bitmap = null;

if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}

if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}

Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package accesscode.c4q.nyc.memeifyme.database;

/**
* Created by c4q-john on 7/18/15.
*/
public class Constants {

public static final String DATABASE_NAME = "memetemplates.db";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "TemplatesTable";
public static final String KEY_ID ="_id";
public static final String TEMPLATE_NAME = "img_name";
public static final String TEMPLATE_IMG = "template_img";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package accesscode.c4q.nyc.memeifyme.database;

import android.accounts.Account;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteProgram;
import android.database.sqlite.SQLiteStatement;
import android.graphics.Bitmap;
import android.util.Log;

import java.io.ByteArrayOutputStream;

/**
* Created by c4q-john on 7/18/15.
*/
public class MyDB {

private SQLiteDatabase db;
private final Context context;
private final MyDBhelper dbhelper;
public MyDB(Context c){
context = c;
dbhelper = new MyDBhelper(context,Constants.DATABASE_NAME, null,Constants.DATABASE_VERSION);
}
public void close() {
db.close();
}

public void open() throws SQLiteException {
try{
db = dbhelper.getWritableDatabase();
} catch (SQLiteException ex){
Log.v("Open db exceptionCaught", ex.getMessage());
db = dbhelper.getReadableDatabase();
}
}

//code to add imgs and their corresponding names to created database
public void insertUser(String imgName, Bitmap img){
SQLiteDatabase db = dbhelper.getWritableDatabase();

// String delSql = "DELETE FROM ACCOUNTS";
// SQLiteStatement delStmt = db.compileStatement(delSql);
// delStmt.execute();

String sql = "INSERT INTO TemplatesTable (_id,img_name,template_img) VALUES(?,?,?)";
SQLiteStatement insertStmt = db.compileStatement(sql);
insertStmt.clearBindings();
insertStmt.bindString(1, Constants.KEY_ID);

insertStmt.bindString(2,imgName);

byte[] data = getBitmapAsByteArray(img);

insertStmt.bindBlob(3, data);
//insertStmt.executeInsert();
db.close();
}
public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream);
return outputStream.toByteArray();
}

//class is supposed to retreive database data but specifications only asked for storage, retrieval has been left
//as it was.
// public Cursor getCurrentAccount() {
// SQLiteDatabase db = dbhelper.getWritableDatabase();
// String sql = "SELECT * FROM TemplatesTable";
// Cursor cursor = db.rawQuery(sql, new String[] {});
//
// if(cursor.moveToFirst()){
// String name = cursor.getString(0);
// String img = cursor.getString(1);
//
// }
// if (cursor != null && !cursor.isClosed()) {
// cursor.close();
// }
// db.close();
// if(cursor.getCount() == 0){
// return null;
// } else {
//
// Cursor c = db.query(Constants.TABLE_NAME,null,null,null,null,null,null);
//
// return c;
// }
// }






}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package accesscode.c4q.nyc.memeifyme.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
* Created by c4q-john on 7/18/15.
*/
public class MyDBhelper extends SQLiteOpenHelper {

private static final String CREATE_TABLE = "create table "+
Constants.TABLE_NAME+" ("+
Constants.KEY_ID+" integer primary key autoincrement, "+
Constants.TEMPLATE_NAME+" text not null, "+
Constants.TEMPLATE_IMG+" long);";


public MyDBhelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, null, version);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
Log.v("MyDBhelper onCreate", "Creating all the tables");
try{
sqLiteDatabase.execSQL(CREATE_TABLE);
} catch(SQLiteException ex){
Log.v("Create table Exception", ex.getMessage());
}

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {

Log.v("TaskDBAdapter", "Upgrading from version "+oldVersion+
" to "+newVersion+", which will destroy all old data");
sqLiteDatabase.execSQL("drop table if exists "+Constants.TABLE_NAME);
onCreate(sqLiteDatabase);

}
}