I am creating an app and i want to have the ability to change the images later on without uninstalling the app. Since we cannot write to drawable folder, i figured that i use a database to save the images and i can manipulate the data there in later on.
I tested a little code, but it doesnt seem to work. I can pull image from the url and display. But i still havent been able to figure out how to put the images on the database from the url.
Look at my codes.
public class MainActivity extends AppCompatActivity {
Intent intent;
Button btn;
DataBaseHandler db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
btn = (Button)findViewById(R.id.btn);
final ImageView img = (ImageView)findViewById(R.id.img);
//initialize db
db = new DataBaseHandler(this);
URL url = null;
try {
url = new URL("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQSh3goeAXlletPgkm3pm1F4DgxwArOKS9STyK02ocNn0AZ6Q9u");
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
url = new URL("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQSh3goeAXlletPgkm3pm1F4DgxwArOKS9STyK02ocNn0AZ6Q9u");
final Bitmap image = BitmapFactory.decodeStream(url.openStream());
byte[]inpudata = Utils.getImageBytes(image);
db.insertImage(inpudata);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
byte[]inpudata = db.retreiveImageFromDB();
img.setImageBitmap(Utils.getImage(inpudata));
}
});
} catch(IOException e) {
System.out.println(e);
}
}
}
Also the Database Helper class
public class DataBaseHandler{
public static final String IMAGE_ID = "id";
public static final String IMAGE = "image";
private final Context mContext;
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_NAME = "Images.db";
private static final int DATABASE_VERSION = 1;
private static final String IMAGES_TABLE = "ImagesTable";
private static final String CREATE_IMAGES_TABLE =
"CREATE TABLE " + IMAGES_TABLE + " (" +
IMAGE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ IMAGE + " BLOB NOT NULL );";
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_IMAGES_TABLE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + CREATE_IMAGES_TABLE);
onCreate(db);
}
}
public void Reset() {
mDbHelper.onUpgrade(this.mDb, 1, 1);
}
public DataBaseHandler(Context ctx) {
mContext = ctx;
mDbHelper = new DatabaseHelper(mContext);
}
public DataBaseHandler open() throws SQLException {
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
// Insert the image to the Sqlite DB
public void insertImage(byte[] imageBytes) {
ContentValues cv = new ContentValues();
cv.put(IMAGE, imageBytes);
mDb.insert(IMAGES_TABLE, null, cv);
}
// Get the image from SQLite DB
// We will just get the last image we just saved for convenience...
public byte[] retreiveImageFromDB() {
Cursor cur = mDb.query(true, IMAGES_TABLE, new String[]{IMAGE,},
null, null, null, null,
IMAGE_ID + " DESC", "1");
if (cur.moveToFirst()) {
byte[] blob = cur.getBlob(cur.getColumnIndex(IMAGE));
cur.close();
return blob;
}
cur.close();
return null;
}
}