i have two classes which i am trying to pass my values in other class for insertion in db, through getters and setters i made but i get the the exception and my application crashes as soon as i click the Save button ..please somebody take a look on it its been three days i cant seem to solve it Thanks here are my both classes:
/**
* Created by Sanya on 11/22/2015.
*/
public class User_overlay_save extends Activity implements View.OnClickListener
{
private static int RESULT_LOAD_IMAGE = 1;
private static int RESULT_LOAD_VIDEO = 2;
private int position = 0;
private SQLiteDatabase db; DatabaseHelper helper;
File myapp;
private MediaController mediaControls;
Button save_button;
ImageButton vid_button,img_button;
EditText name_overlay;
TextView real_Imgpath, real_Vidpath;
protected void onCreate(Bundle savedInstanceState){
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.user_overlay_save);
save_button=(Button)findViewById(R.id.save_Ouser);
img_button=(ImageButton)findViewById(R.id.img_button);
vid_button=(ImageButton)findViewById(R.id.vid_button);
real_Imgpath =(TextView)findViewById(R.id.real_Imgpath);
real_Vidpath =(TextView)findViewById(R.id.real_Vidpath);
name_overlay=(EditText)findViewById(R.id.name_overlay);
save_button.setOnClickListener(this);
img_button.setOnClickListener(this);
vid_button.setOnClickListener(this);
name_overlay.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.save_Ouser:
{
boolean result= helper.Insert();
if(result==true)
{
Toast.makeText(getApplicationContext(), "saved..in DB..!!!",
Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "ERROR--the path could not b saved..!!",
Toast.LENGTH_LONG).show();
}
}
break;
case R.id.img_button:
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
break;
case R.id.vid_button:
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_VIDEO);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String realPath;
//FOR IMAGE PATH SAVING
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
String selectedImagePath;
Uri selectedImageUri = data.getData();
//MEDIA GALLERY
selectedImagePath = ImageFilePath.getPath(getApplicationContext(), selectedImageUri);
Log.i("Image-->File Path-->", " " + selectedImagePath);
setImgTextViews(Build.VERSION.SDK_INT, selectedImagePath);
SET_saveImg(selectedImagePath);
}
else if(Build.VERSION.SDK_INT < 19)
{
realPath = this.getRealPathFromURI_FromAPI11_18(this, data.getData());
setImgTextViews(Build.VERSION.SDK_INT, realPath);
}
//----------->>>>>>>>>>>---------->>>>FOR VIDEO PATH SAVING------>>>>>>------------>>>>>>>>>>>-------->>>>>>>>>---------->>>>
if (requestCode == RESULT_LOAD_VIDEO && resultCode == RESULT_OK && null != data)
{
//FOR VIDEO PATH SAVING
String selectedVidPath;
Uri selectedVidUri = data.getData();
//VIDEO____PLAY___LIST
selectedVidPath = ImageFilePath.getPath(getApplicationContext(), selectedVidUri);
Log.i("VIDEO-->File Path-->", "" + selectedVidPath);
setVIDTextViews(Build.VERSION.SDK_INT, selectedVidPath);
SET_saveVid(selectedVidPath);
}
}
public String getRealPathFromURI_FromAPI11_18(Context context, Uri contentUri)
{
String[] proj = { MediaStore.Images.Media.DATA };
String result = null;
CursorLoader cursorLoader = new CursorLoader(context, contentUri, proj, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
if(cursor != null){
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
result = cursor.getString(column_index);
}
return result;
}
protected void setImgTextViews(int sdk,String realPath){
this.real_Imgpath.setText
("CURRENT VERSION--SDK-->"+sdk+"Real Path: "+realPath);
}
protected void setVIDTextViews(int sdk,String realPath)
{
this.real_Vidpath.setText
("CURRENT VERSION--SDK-->"+sdk+"Real Path: "+realPath);
}
//---->>------>>>------->>>>SETTERS--FOR IMAGE-->SET_saveImg(selectedImagePath) For &VIDEO -->>SET_saveVid(selectedVidPath)--->>AND--->>GETTERS FOR IMAGE AND VIDEO--------->>>>>
String realPath; String vidPath;
public String GET_saveImg()//IS CALLED FROM DATABASE_HELPER CLASS TO PROVIDE THE SETTER VALUES
{
return realPath;
}
protected void SET_saveImg(String realPath)
{
this.realPath=realPath;
}
protected String GET_saveVid()
{
return vidPath;
}
protected void SET_saveVid(String vidPath)
{
this.vidPath= vidPath;
}}
---------------------<<<<<<------------>>>>>>>>>>>>--------- DATABASEHELPER_class
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
SQLiteDatabase db; User_overlay_save obj ;
// Database Name
public static String DATABASE_NAME = "MY_database.sql";
// Current version of database
private static final int DATABASE_VERSION = 1;
// Name of table
private static final String TABLE_Images = "images";
// All Keys used in table
static final String KEY_ID = "id";
static final String KEY_IMAGE = "image";
static final String KEY_FILE = "file";
public static String TAG = "tag";
private static final String CREATE_TABLE_Images = "CREATE TABLE "
+ TABLE_Images + "(" + KEY_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_IMAGE + "TEXT" + KEY_FILE + " TEXT );";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
/*This method is called by system if the database is accessed but not yet created*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_Images); // create Image table
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_Images);
// drop table if exists
onCreate(db);
}
public boolean Insert()
{ boolean success = false;
String imgpath = obj.GET_saveImg();
String vidPath = obj.GET_saveVid();
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_IMAGE, imgpath);
values.put(KEY_FILE, vidPath);
db.insert(TABLE_Images, null, values);
success=true;
db.close();
if(success) {
return true;
}
else
{
return false;
}
}
}