In my app I have two tables that I want to link so that I can keep track of the specific users images per user post. I am using the ID of the user in the table of the images to associate the images with the specific user that uploaded them. Is this the correct way to do this? Any help will be much appreciated.
Here is my code:
PostAd.java
public class PostAd extends AppCompatActivity implements View.OnClickListener{
Button imageUpload;
Button vidUpload;
Button postAd;
static ImageView img1, img2, img3, img4, img5, img6, img7, img8, img9;
public static final int SELECT_IMAGE = 1;
public static final int SELECT_IMAGE1 = 2;
public static final int SELECT_IMAGE2 = 3;
public static final int SELECT_IMAGE3 = 4;
public static final int SELECT_IMAGE4 = 5;
public static final int SELECT_IMAGE5 = 6;
public static final int SELECT_IMAGE6 = 7;
public static final int SELECT_IMAGE7 = 8;
public static final int SELECT_IMAGE8 = 9;
String [] imgPaths = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.post_ad);
initializeVariables();
initOnclickListeners();
}
private void initializeVariables() {
imageUpload = (Button) findViewById(R.id.bImages);
vidUpload = (Button) findViewById(R.id.bVideo);
postAd = (Button) findViewById(R.id.bPostAd);
img1 = (ImageView) findViewById(R.id.imgImages);
img2 = (ImageView) findViewById(R.id.imgImages1);
img3 = (ImageView) findViewById(R.id.imgImages2);
img4 = (ImageView) findViewById(R.id.imgImages3);
img5 = (ImageView) findViewById(R.id.imgImages4);
img6 = (ImageView) findViewById(R.id.imgImages5);
img7 = (ImageView) findViewById(R.id.imgImages6);
img8 = (ImageView) findViewById(R.id.imgImages7);
img9 = (ImageView) findViewById(R.id.imgImages8);
}
private void initOnclickListeners() {
if(imageUpload != null) imageUpload.setOnClickListener(this);
vidUpload.setOnClickListener(this);
postAd.setOnClickListener(this);
img1.setOnClickListener(this);
img2.setOnClickListener(this);
img3.setOnClickListener(this);
img4.setOnClickListener(this);
img5.setOnClickListener(this);
img6.setOnClickListener(this);
img7.setOnClickListener(this);
img8.setOnClickListener(this);
img9.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imgImages:
Intent image = new Intent(MediaStore.Images.Media.DATA);
image.setType("image/*");
image.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(image, SELECT_IMAGE);
break;
case R.id.imgImages1:
Intent image1 = new Intent(MediaStore.Images.Media.DATA);
image1.setType("image/*");
image1.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(image1, SELECT_IMAGE1);
break;
case R.id.imgImages2:
Intent image2 = new Intent();
image2.setType("image/*");
image2.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(image2, SELECT_IMAGE2);
break;
case R.id.imgImages3:
Intent image3 = new Intent();
image3.setType("image/*");
image3.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(image3, SELECT_IMAGE3);
break;
case R.id.imgImages4:
Intent image4 = new Intent();
image4.setType("image/*");
image4.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(image4, SELECT_IMAGE4);
break;
case R.id.imgImages5:
Intent image5 = new Intent();
image5.setType("image/*");
image5.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(image5, SELECT_IMAGE5);
break;
case R.id.imgImages6:
Intent image6 = new Intent();
image6.setType("image/*");
image6.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(image6, SELECT_IMAGE6);
break;
case R.id.imgImages7:
Intent image7 = new Intent();
image7.setType("image/*");
image7.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(image7, SELECT_IMAGE7);
break;
case R.id.imgImages8:
Intent image8 = new Intent();
image8.setType("image/*");
image8.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(image8, SELECT_IMAGE8);
break;
case R.id.bImages:
Intent i = new Intent(this, ImageViewTest.class);
i.putExtra("images", imgPaths);
startActivity(i);
break;
case R.id.bVideo:
break;
case R.id.bPostAd:
break;
}
}
@Override
public void onActivityResult ( int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_IMAGE && resultCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView1 = (ImageView) findViewById(R.id.imgImages);
// imageView1.setImageBitmap(BitmapFactory.decodeFile(picturePath));
imageView1.setImageBitmap(getScaledBitmap(picturePath, 100, 100));
}else if (requestCode == SELECT_IMAGE1 && resultCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView2 = (ImageView) findViewById(R.id.imgImages1);
//imageView1.setImageBitmap(BitmapFactory.decodeFile(picturePath));
imageView2.setImageBitmap(getScaledBitmap(picturePath, 100, 100));
}else if (requestCode == SELECT_IMAGE2 && resultCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView3 = (ImageView) findViewById(R.id.imgImages2);
imageView3.setImageBitmap(getScaledBitmap(picturePath, 100, 100));
}else if (requestCode == SELECT_IMAGE3 && resultCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView4 = (ImageView) findViewById(R.id.imgImages3);
// imageView1.setImageBitmap(BitmapFactory.decodeFile(picturePath));
imageView4.setImageBitmap(getScaledBitmap(picturePath,100,100));
}else if (requestCode == SELECT_IMAGE4 && resultCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView5 = (ImageView) findViewById(R.id.imgImages4);
// imageView1.setImageBitmap(BitmapFactory.decodeFile(picturePath));
imageView5.setImageBitmap(getScaledBitmap(picturePath,100,100));
}else if (requestCode == SELECT_IMAGE5 && resultCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView6 = (ImageView) findViewById(R.id.imgImages5);
imageView6.setImageBitmap(getScaledBitmap(picturePath, 100, 100));
}else if (requestCode == SELECT_IMAGE6 && resultCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView7 = (ImageView) findViewById(R.id.imgImages6);
imageView7.setImageBitmap(getScaledBitmap(picturePath, 100, 100));
}else if (requestCode == SELECT_IMAGE7 && resultCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView8 = (ImageView) findViewById(R.id.imgImages7);
//imageView1.setImageBitmap(BitmapFactory.decodeFile(picturePath));
imageView8.setImageBitmap(getScaledBitmap(picturePath,100,100));
}else if (requestCode == SELECT_IMAGE8 && resultCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView9 = (ImageView) findViewById(R.id.imgImages8);
// imageView1.setImageBitmap(BitmapFactory.decodeFile(picturePath));
imageView9.setImageBitmap(getScaledBitmap(picturePath,100,100));
}
}
private Bitmap getScaledBitmap(String picturePath, int width, int height) {
BitmapFactory.Options sizeOptions = new BitmapFactory.Options();
sizeOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(picturePath, sizeOptions);
int inSampleSize = calculateInSampleSize(sizeOptions, width, height);
sizeOptions.inJustDecodeBounds = false;
sizeOptions.inSampleSize = inSampleSize;
return BitmapFactory.decodeFile(picturePath, sizeOptions);
}
private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
}
DBHelper class:
public class EquiRushDB {
private static final String KEY_ROWID = "_id";
private static final String KEY_USERNAME = "user_username";
private static final String KEY_EMAIL = "user_email";
private static final String KEY_PASSWORD = "user_password";
private static final String KEY_PHONE = "user_phone";
private static final String KEY_ID = "ad_id";
private static final String KEY_TITLE = "ad_title";
private static final String KEY_DESCRIPTION = "ad_description";
private static final String KEY_IMAGEURI = "ad_image_uri";
private static final String KEY_VIDEOURI = "ad_video_uri";
private static final String KEY_PRICE = "ad_price";
private static final String DATABASE_NAME = "EquiRushDB";
private static final String DATABASE_TABLE = "user_authentication";
private static final String DATABASE_TABLE1 = "get_images";
private static final String DATABASE_TABLE2 = "post_ad";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
public class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + DATABASE_TABLE + "( " +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_USERNAME + " TEXT NOT NULL, " +
KEY_EMAIL + " TEXT NOT NULL, " +
KEY_PHONE + " INTEGER NOT NULL, " +
KEY_PASSWORD + " TEXT NOT NULL);"
);
db.execSQL("CREATE TABLE " + DATABASE_TABLE1 + "( " +
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_ROWID + " INTEGER NOT NULL, " +
KEY_IMAGEURI1 + " TEXT NOT NULL, " +
KEY_IMAGEURI2 + " TEXT NOT NULL, " +
KEY_IMAGEURI3 + " TEXT(500) NOT NULL, " +
KEY_IMAGEURI4 + " TEXT NOT NULL, " +
KEY_IMAGEURI5 + " TEXT NOT NULL, " +
KEY_IMAGEURI6 + " TEXT NOT NULL, " +
KEY_IMAGEURI7 + " TEXT NOT NULL, " +
KEY_IMAGEURI8 + " TEXT NOT NULL, " +
KEY_IMAGEURI9 + " TEXT NOT NULL);"
);
db.execSQL("CREATE TABLE " + DATABASE_TABLE2 + "( " +
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_USERNAME + " TEXT NOT NULL, " +
KEY_TITLE + " TEXT NOT NULL, " +
KEY_DESCRIPTION + " TEXT(500) NOT NULL, " +
KEY_IMAGEURI + " TEXT NOT NULL, " +
KEY_VIDEOURI + " TEXT NOT NULL, " +
KEY_PRICE + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(" DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public EquiRushDB(Context context) {
ourContext = context; //initialize the context
}
public EquiRushDB open() throws SQLException { //We have to make it EquiRushDb as return type
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close() throws SQLException {
ourHelper.close();
}
public long createEntry(String username, String email, String phone, String password) {
ContentValues cv = new ContentValues();
cv.put(KEY_USERNAME, username);
cv.put(KEY_EMAIL, email);
cv.put(KEY_PHONE, phone);
cv.put(KEY_PASSWORD, password);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() throws SQLException {
String[] columns = {KEY_ROWID, KEY_USERNAME, KEY_EMAIL, KEY_PHONE, KEY_PASSWORD};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = " ";
int iRow = c.getColumnIndex(KEY_ROWID);
int iUserName = c.getColumnIndex(KEY_USERNAME);
int iEmail = c.getColumnIndex(KEY_EMAIL);
int iPhone = c.getColumnIndex(KEY_PHONE);
int iPassword = c.getColumnIndex(KEY_PASSWORD);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { //init move to first, loop length move to last and increment moveToNext
result = result + c.getString(iRow) + " " + c.getString(iUserName) + " " + c.getString(iEmail) + " " + c.getString(iPhone) + " " + c.getString(iPassword) + " n";
}
c.moveToFirst(); //Why return cursor to 1st?
c.close();
return result;
}
public String getUsername(long l) throws SQLException{
String[] columns = {KEY_ROWID, KEY_USERNAME, KEY_EMAIL, KEY_PHONE, KEY_PASSWORD};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null);
c.moveToFirst();
String username = c.getString(1);
c.close();
return username;
}
public String getEmail(String userEmail) throws SQLException{
String[] columns = {KEY_ROWID, KEY_USERNAME, KEY_EMAIL, KEY_PHONE, KEY_PASSWORD};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns,KEY_EMAIL,null, null, null, null);
c.moveToFirst();
userEmail = c.getString(2);
c.close();
return userEmail;
}
public String getPhone(long l) throws SQLException{
String[] columns = {KEY_ROWID, KEY_USERNAME, KEY_EMAIL, KEY_PHONE, KEY_PASSWORD};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns,KEY_ROWID + "=" + l,null, null, null, null);
c.moveToFirst();
String userPhone = c.getString(3);
c.close();
return userPhone;
}
public String getSingleEntry(String email) throws SQLException{
//We leave the columns String[] out here and set it to null in our query
Cursor c = ourDatabase.query(DATABASE_TABLE, null, KEY_EMAIL + "=?" , new String[]{email}, null, null, null);
if(c.getCount() < 1){
c.close();
return "DATA DOES NOT EXIST";
}
c.moveToFirst();
String password = c.getString(c.getColumnIndex(KEY_PASSWORD));
c.close();
return password;
}
public void updateEntry (long lRow, String mUserName, String mEmail, int mPhone, String mPassword) throws SQLException{
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(KEY_USERNAME, mUserName);
cvUpdate.put(KEY_EMAIL, mEmail);
cvUpdate.put(KEY_PHONE, mPhone);
cvUpdate.put(KEY_PASSWORD, mPassword);
ourDatabase.update(DATABASE_TABLE,cvUpdate, KEY_ROWID + "=" + lRow, null);
}
public void deleteEntry(long lDel) throws SQLException{
ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + lDel, null);
}
public String getPost() throws SQLException{
String[] columns = {KEY_ID, KEY_TITLE, KEY_DESCRIPTION, KEY_IMAGEURI, KEY_VIDEOURI, KEY_PRICE};
Cursor c = ourDatabase.query(DATABASE_TABLE2, columns, null, null, null, null,null);
String result = " ";
int iID = c.getColumnIndex(KEY_ID);
int iTitle = c.getColumnIndex(KEY_TITLE);
int iDescription = c.getColumnIndex(KEY_DESCRIPTION);
int iImagePath = c.getColumnIndex(KEY_IMAGEURI);
int iVideoPath = c.getColumnIndex(KEY_VIDEOURI);
int iPrice = c.getColumnIndex(KEY_PRICE);
for( c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iID) + " " + c.getString(iTitle) + " " + c.getString(iDescription) + " " + c.getString(iImagePath)
+ " " + c.getString(iVideoPath) + " " + c.getString(iPrice) + "n";
}
c.moveToFirst();
c.close();
return result;
}
public long createPost(String postID, String postTitle, String postDescription, String postImage, String postVideo, String postPrice) throws SQLException{
ContentValues cv = new ContentValues();
cv.put(KEY_ID, postID);
cv.put(KEY_TITLE, postTitle);
cv.put(KEY_DESCRIPTION, postDescription);
cv.put(KEY_IMAGEURI, postImage);
cv.put(KEY_VIDEOURI, postVideo);
cv.put(KEY_PRICE, postPrice);
return ourDatabase.insert(DATABASE_TABLE2, null, cv);
}
//Get the images
public long getImages(String postID, String userID, String image1, String image2, String image3, String image4, String image5, String image6, String image7, String image8, String image9) throws SQLException{
ContentValues cv = new ContentValues();
cv.put(KEY_ID, postID);
cv.put(KEY_ROWID, userID);
cv.put(KEY_IMAGEURI1, image1);
cv.put(KEY_IMAGEURI2, image2);
cv.put(KEY_IMAGEURI3, image3);
cv.put(KEY_IMAGEURI4, image4);
cv.put(KEY_IMAGEURI5, image5);
cv.put(KEY_IMAGEURI6, image6);
cv.put(KEY_IMAGEURI7, image7);
cv.put(KEY_IMAGEURI8, image8);
cv.put(KEY_IMAGEURI9, image9);
return ourDatabase.insert(DATABASE_TABLE2, null, cv);
}
}
Aucun commentaire:
Enregistrer un commentaire