Android-字符串不存储在数据库中

时间:2017-04-25 15:37:20

标签: android onactivityresult

我将用户输入的值存储到全局变量中的编辑文本字段中。然后我尝试将此值与视频URI一起插入数据库中的两个单独的列中。它似乎没有输入任何值到我的数据库中。我的代码如下:

public class SetMediaName extends Activity {
    EditText mediaName;
    TextView namePrompt;
    Button setTheName;
    MediaContentDB uriDB;
    Uri videoUri;
    Uri photoURI;
    Button recordVideo;
    public static String medName;

    public String vidName;
    private static final int REQUEST_CODE_VIDEO = 1;
    private static final int REQUEST_CODE_PHOTO = 2;


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

    mediaName = (EditText) findViewById(R.id.editText);
    namePrompt = (TextView) findViewById(R.id.textView);
    setTheName = (Button) findViewById(R.id.setButtonName);
    recordVideo = (Button) findViewById(R.id.recbtn);


    setTheName.setOnClickListener(new View.OnClickListener() {
        @Override
                public void onClick(View view)
                {
                  sendMedName = setMediaName();
                }
            });

    recordVideo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent startVideoFunction = new Intent();
            startVideoFunction.setAction(MediaStore.ACTION_VIDEO_CAPTURE);

            startActivityForResult(startVideoFunction, REQUEST_CODE_VIDEO);
        }
    });




}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    try {
        switch (requestCode) {
            case REQUEST_CODE_VIDEO:


                videoUri = data.getData();
                String videoPath = videoUri.toString();
                uriDB.addVideoRow(videoPath, sendMedName);


                break;

            case REQUEST_CODE_PHOTO:

                String photoPath = photoURI.toString();
                uriDB.addPhotoRow(photoPath,sendMedName);

                break;
        }
    } catch (NullPointerException e) {
        //prevent crash when returning with Null data to mainactivity.
    }

}

public String setMediaName(){
    medName =  mediaName.getText().toString();

     return medName;
 }





}

当此活动开始时,要求用户输入标题,然后按录制按钮开始录制他们的视频。同样,我的数据不会进入我的数据库。如果有人能发现我的错误,我将不胜感激,因为如果在运行时之前将字符串medName声明为预定义值,则数据将进入。

数据库代码:

public class MediaContentDB {

public static final String KEY_ID = "content_id";
public static final String KEY_PHOTO_ID = "photo_id";
public static final String KEY_MEDIA_PATH = "media_path";
public static final String KEY_MEDIA_NAME ="media_name";
public static final String KEY_PHOTO_NAME ="photo_name";
public static final String KEY_PHOTO_PATH = "photo_path";
private MyDataBaseHelper myDataBaseHelper;
Context context;


public MediaContentDB(Context context){
    this.context=context;

    myDataBaseHelper = new MyDataBaseHelper(context,MyDataBaseHelper.DATABASE_NAME,null, MyDataBaseHelper.DATABASE_VERSION);

}

public void closeDatabase() {
    myDataBaseHelper.close();
}

public void addVideoRow(String videoPath, String mediaName) {

    ContentValues newVideo = new ContentValues();

    newVideo.put(KEY_MEDIA_PATH, videoPath );
    newVideo.put(KEY_MEDIA_NAME, mediaName);


    SQLiteDatabase db = myDataBaseHelper.getWritableDatabase();
    db.insert(MyDataBaseHelper.DATABASE_TABLE, null, newVideo);
}

public void addPhotoRow(String photoPath, String photoName) {

    ContentValues newPhoto = new ContentValues();

    newPhoto.put(KEY_PHOTO_PATH, photoPath );
    newPhoto.put(KEY_PHOTO_NAME, photoName);

    SQLiteDatabase db = myDataBaseHelper.getWritableDatabase();
    db.insert(MyDataBaseHelper.DATABASE_PHOTO, null, newPhoto);
}



public String getVideoPath(String mediaName) {


    String[] result_columns = new String[]{KEY_MEDIA_PATH};



    String where = KEY_MEDIA_NAME + " = ?";
    String whereArgs[] = {mediaName};
    String groupBy = null;
    String having = null;
    String order = null;

    SQLiteDatabase db = myDataBaseHelper.getWritableDatabase();
    Cursor cursor = db.query(MyDataBaseHelper.DATABASE_TABLE,
            result_columns, where,
            whereArgs, groupBy, having, order);
    //
    boolean result = cursor.moveToFirst();
    if (result) {
        return cursor.getString(cursor.getColumnIndex(KEY_MEDIA_PATH));




    }
    return "";
}

public String[] getMediaTitle() {

    ArrayList<String> nameArray = new ArrayList<String>();
    String[] result_columns = new String[]{KEY_MEDIA_NAME};

    String mediaName;

    String where = null;
    String whereArgs[] = null;
    String groupBy = null;
    String having = null;
    String order = null;

    SQLiteDatabase db = myDataBaseHelper.getWritableDatabase();
    Cursor cursor = db.query(MyDataBaseHelper.DATABASE_TABLE,
            result_columns, where,
            whereArgs, groupBy, having, order);
    //
    boolean result = cursor.moveToFirst();
    while (result) {
        mediaName = cursor.getString(cursor.getColumnIndex(KEY_MEDIA_NAME));

        nameArray.add(mediaName);

        result = cursor.moveToNext();


    }
    return nameArray.toArray(new String[nameArray.size()]);
}

public String[] getPhotoTitle() {

    ArrayList<String> nameArray = new ArrayList<String>();
    String[] result_columns = new String[]{KEY_PHOTO_NAME};

    String mediaName;

    String where = null;
    String whereArgs[] = null;
    String groupBy = null;
    String having = null;
    String order = null;

    SQLiteDatabase db = myDataBaseHelper.getWritableDatabase();
    Cursor cursor = db.query(MyDataBaseHelper.DATABASE_PHOTO,
            result_columns, where,
            whereArgs, groupBy, having, order);
    //
    boolean result = cursor.moveToFirst();
    while (result) {
        mediaName = cursor.getString(cursor.getColumnIndex(KEY_PHOTO_NAME));

        nameArray.add(mediaName);

        result = cursor.moveToNext();


    }
    return nameArray.toArray(new String[nameArray.size()]);
}

public String getPhotoPath(String mediaName) {


    String[] result_columns = new String[]{KEY_PHOTO_PATH};

    String where = KEY_PHOTO_NAME + " = ?";;
    String whereArgs[] = {mediaName};
    String groupBy = null;
    String having = null;
    String order = null;

    SQLiteDatabase db = myDataBaseHelper.getWritableDatabase();
    Cursor cursor = db.query(MyDataBaseHelper.DATABASE_PHOTO,
            result_columns, where,
            whereArgs, groupBy, having, order);
    //
    boolean result = cursor.moveToFirst();
    if (result) {
        return cursor.getString(cursor.getColumnIndex(KEY_PHOTO_PATH));




    }
    return "";
}





private static class MyDataBaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "mediaContent.db";
    private static final String DATABASE_TABLE = "Videos";
    private static final String DATABASE_PHOTO = "Photos";
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_CREATE = "create table " +
            DATABASE_TABLE + " (" + KEY_ID +
            " integer primary key autoincrement, " + KEY_MEDIA_NAME + " text not null, " +
            KEY_MEDIA_PATH + " text not null);";

    private static final String DATABASE_PHOTO_CREATE = "create table " +
            DATABASE_PHOTO + " (" + KEY_PHOTO_ID +
            " integer primary key autoincrement, " + KEY_PHOTO_NAME + " text not null, " +
            KEY_PHOTO_PATH + " text not null);";



    public MyDataBaseHelper(Context context, String name,
                            CursorFactory factory, int version) {
        super(context, name, factory, version);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);
        db.execSQL(DATABASE_PHOTO_CREATE);

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

        Log.w("TaskDBAdapter", "Upgrading from version " +
                oldVersion + " to " +
                newVersion + ", which will destroy all old data");

        db.execSQL("DROP TABLE IF IT EXISTS " + DATABASE_TABLE);
        db.execSQL("DROP TABLE IF IT EXISTS " + DATABASE_PHOTO);


        onCreate(db);
    }

}

}

2 个答案:

答案 0 :(得分:0)

尝试删除您的Android应用并重新安装,因为SQLiteOpenHelper.OnCreate方法将调用一次。

答案 1 :(得分:0)

发现问题,我从未初始化我的数据库引用,因此addVideoRow方法不起作用。

git push origin wpf-design-guidelines --force