如何将图像导入SQLite并在单独的活动中检索?

时间:2017-04-21 09:52:26

标签: android sqlite

我正在尝试将图像保存到我的Android应用程序上的SQLite数据库中。我希望保存这些图像,然后能够在单独的活动中检索和显示它们。我的数据库运行正常,我正在努力解决如何从数据库导入,保存和检索图像所需的所有字符串。优选地,用户可以从他们的图库中选择这些图像(可能使用图库意图?)任何帮助将不胜感激!

数据库帮助中的代码

public class DBHelperTrips extends SQLiteOpenHelper {

//declaring the structure of the SQLite database
private static final int DATADASE_VERSION = 1;
private static final String DATABASE_NAME = "tripinformation.db";
private static final String TABLE_NAME = "tripinformation";
private static final String COLUMN_ID = "Id";
private static final String COLUMN_TRIPNAME = "tripName";
private static final String COLUMN_LOCATION = "location";
private static final String COLUMN_TRIPBEGIN = "tripBegin";
private static final String COLUMN_TRIPEND = "tripEnd";
private static final String COLUMN_EXTRA = "extra";
private static final String TAG = "";
//tag needs input?

//naming the database
SQLiteDatabase dbtripinformation;

//creating the table
private static final String TABLE_CREATE = "create table tripinformation(Id integer primary key not null ," +
        "tripName text not null , location text not null , tripBegin text not null , tripEnd text not null, extra text);";


public DBHelperTrips(Context context)
{
    super(context , DATABASE_NAME, null , DATADASE_VERSION);
}


//on create method
public void onCreate(SQLiteDatabase db)
{
    db.execSQL(TABLE_CREATE);
    this.dbtripinformation = db;

}

//inserting user information method
//this is called when a new user is registered
public void insertTripInformation(TripInformation user)
{
    dbtripinformation = this.getWritableDatabase();
    ContentValues value = new ContentValues();

    //getting content of db that is already present by querying with cursor information

    String query = "select * from tripinformation";
    Cursor cursor = dbtripinformation.rawQuery(query, null);
    int count = cursor.getCount();


    //this places the inputs in to the various columns in the SQlite db
    value.put(COLUMN_ID, count);
    value.put(COLUMN_TRIPNAME, user.getTripName());
    value.put(COLUMN_LOCATION, user.getLocation());
    value.put(COLUMN_TRIPBEGIN, user.getTripBegin());
    value.put(COLUMN_TRIPEND, user.getTripEnd());
    value.put(COLUMN_EXTRA, user.getExtra());


    //insert table
    dbtripinformation.insert(TABLE_NAME, null, value);
    //closing the database
    dbtripinformation.close();
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
    String queryDrop = "DROP TABLE IF EXISTS " + TABLE_NAME;
    db.execSQL(queryDrop);
    this.onCreate(dbtripinformation);

}

public Cursor getData(){
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "SELECT * FROM " + TABLE_NAME;
    Cursor data = db.rawQuery(query, null);
    return data;

}

创建TRIP活动实例

public class createTrip extends AppCompatActivity {

DBHelperTrips helper = new DBHelperTrips(this);

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

    final Button buttonReturn = (Button) findViewById(R.id.buttonReturn);
    buttonReturn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            returnToMain();
        }
    });

    final Button makeTrip = (Button) findViewById(R.id.makeYourTrip);
    makeTrip.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            createTrip();
        }
    });
}

public void returnToMain() {
    Intent intent = new Intent(this, mainTripPage.class);
    startActivity(intent);
    this.finish();
}

public void createTrip() {
    //THIS NEEDS TO CREATE A FILE TO STORE THE INFORMATION TO USING A DATABASE CONNECTION AND THEN DISPLAY A TOAST SHOWING THAT THE
    //TRIP HAS BEEN CREATED, AFTER THAT THE TEXTBOXES SHOULD CLEAR AND THE USER SHOULD BE ABLE TO RETURN TO MAIN MENU


    //creates an instance of each edittext on the screen
    EditText tripName = (EditText) findViewById(R.id.edtTextTitle);
    EditText location = (EditText) findViewById(R.id.edtTextTripLocation);
    EditText startDate = (EditText) findViewById(R.id.edtDateStart);
    EditText endDate = (EditText) findViewById(R.id.edtDateEnd);
    EditText extra = (EditText) findViewById(R.id.edtTextExtra);

    //assigns string values to each input from the user
    String tripTitle = tripName.getText().toString();
    String locationOfTrip = location.getText().toString();
    String dateStart = startDate.getText().toString();
    String dateEnd = endDate.getText().toString();
    String extraInfo = extra.getText().toString();

    TripInformation user = new TripInformation();

    user.setExtra(extraInfo);
    user.setLocation(locationOfTrip);
    user.setTripBegin(dateStart);
    user.setTripEnd(dateEnd);
    user.setTripName(tripTitle);

    helper.insertTripInformation(user);

    Toast created = Toast.makeText(createTrip.this, "New Trip Created", Toast.LENGTH_SHORT);
    TextView v = (TextView) created.getView().findViewById(android.R.id.message);
    if (v != null) v.setGravity(Gravity.CENTER);
    created.show();

    tripName.setText("");
    location.setText("");
    startDate.setText("");
    endDate.setText("");
    extra.setText("");
}

}

1 个答案:

答案 0 :(得分:0)

由于我在SQLite中保存图像的个人经验不是很好的做法,它会增加应用程序存储内存。

所以将图像存储在设备本地(内部/外部)内存中的良好做法然后在SQLite数据库中保存尊重的图像路径供您参考。

当涉及到在SQLite中保存图像路径的使用提取时,然后使用该路径从本地存储内存加载图像并将其显示在屏幕上。

希望这个过程有用。

@Rajesh