我需要在一个表数据库中存储一组数据,然后在我的android应用程序中从中提取数据
好的,这就是我对整个android平台都是新手,我试图把我需要的所有信息放到一个数据库表中,然后把信息放到我的活动中,基本上就像。。。。 每行代表一个项目(假设有100个),然后有一列描述、食物、位置、名称,如果可能还有图像。目前,我只是手动将所有信息添加到单独的xml布局中,这似乎比从数据库中提取信息要耗时和困难得多,对吗 我目前正在做的事情:我需要在一个表数据库中存储一组数据,然后在我的android应用程序中从中提取数据,android,database,sqlite,metadata,Android,Database,Sqlite,Metadata,好的,这就是我对整个android平台都是新手,我试图把我需要的所有信息放到一个数据库表中,然后把信息放到我的活动中,基本上就像。。。。 每行代表一个项目(假设有100个),然后有一列描述、食物、位置、名称,如果可能还有图像。目前,我只是手动将所有信息添加到单独的xml布局中,这似乎比从数据库中提取信息要耗时和困难得多,对吗 我目前正在做的事情: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:gravity="center"
android:text="@string/bluefish"
android:textColor="@color/black"
android:textSize="30dp"
android:textStyle="bold" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/bluefish"
android:src="@drawable/bluefish" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:gravity="left"
android:text="@string/desc"
android:textColor="@color/black"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:gravity="left"
android:text="text here"
android:textColor="@color/black"
android:textSize="12dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:gravity="left"
android:text="@string/food"
android:textColor="@color/black"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:gravity="left"
android:text="text here"
android:textColor="@color/black"
android:textSize="12dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:gravity="left"
android:text="@string/loc"
android:textColor="@color/black"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:gravity="left"
android:text="text here"
android:textColor="@color/black"
android:textSize="12dp" />
使用以下代码创建数据库 这将创建新的DB SaveImage
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
public class DbHelper extends SQLiteOpenHelper {
public static final int DB_VERSION = 1;
public static final String DB_NAME = "SaveImage";
public DbHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String tblSaveImage = "CREATE TABLE tblSaveImage (_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , Image BLOB);";
db.execSQL(tblSaveImage);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
将图像保存到数据库:
如果不想使用SQL查询,还可以使用内容值将数据插入数据库
import java.io.ByteArrayOutputStream;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class SaveImage extends Activity {
SQLiteDatabase Db;
TextView txt;
Button btnImage,btnSaveimage;
ImageView imageView;
byte[] byteArray;
private static final int PICK_FROM_FILE = 2;
private Uri mImageCaptureUri;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Initialize();
//btnSaveImage Handler Save Byte Array to DB and Navigate to ShowImage Activity
btnSaveimage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
ContentValues cv = new ContentValues();
cv.put("Image", byteArray);
Db.insert("tblSaveImage", null, cv);
startActivity(new Intent(getApplicationContext(),ShowImage.class));
} catch (Exception e) {
// TODO: handle exception
txt.setText(e.toString());
}
}
});
//btnImage Handler Access to Gallery of Device and Display all Images save in Device.
btnImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
}
});
}
// To Initialize all the Components.
private void Initialize() {
// TODO Auto-generated method stub
txt = (TextView)findViewById(R.id.txt);
btnImage = (Button)findViewById(R.id.btnImage);
imageView = (ImageView)findViewById(R.id.imageView);
btnSaveimage = (Button)findViewById(R.id.btnSaveImage);
DbHelper h = new DbHelper(this);
try {
Db = h.getWritableDatabase();
} catch (SQLException e) {
// TODO: handle exception
Db = h.getReadableDatabase();
}
}
// OnActivityResult PICK_IMAGE_FROM_FILE
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
mImageCaptureUri = data.getData();
imageView.setImageURI(mImageCaptureUri);
imageView.buildDrawingCache();
Bitmap bmp = imageView.getDrawingCache();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byteArray = stream.toByteArray();
super.onActivityResult(requestCode, resultCode, data);
}
}
显示保存在DB中的图像:
import android.app.Activity;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
public class ShowImage extends Activity {
ImageView showImage;
SQLiteDatabase Db;
Cursor result;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
showImage = (ImageView)findViewById(R.id.showImage);
DbHelper h = new DbHelper(this);
try {
Db = h.getWritableDatabase();
} catch (SQLException e) {
// TODO: handle exception
Db = h.getReadableDatabase();
}
result = Db.rawQuery("Select Image from tblSaveImage", null);
if (result.moveToLast()) {
byte[] byteArray = result.getBlob(0);
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
showImage.setImageBitmap(bmp);
}
}
}
你到底在问什么,你有什么不是一个问题,你应该熟练地看这是你数据库的起点。上面的XML应该只用于向用户显示内容,而不是存储数据。您最好看看这篇惊人的文章!非常感谢,这帮了大忙!!谢谢@Krishnakant Dalal