好的,所以继续瘦我是整个Android平台的新手,我试图基本上将我需要的所有信息放入数据库表,然后将信息拉入我的活动基本上就像.... 每行代表一个项目(假设有100个)然后有一列用于描述,食物,位置,名称和可能的图像。目前我只是手动将所有信息添加到单独的xml布局中,这似乎比我从数据库中提取信息更加耗时和困难?
我目前在做什么:
<?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" />
答案 0 :(得分:0)
使用以下代码创建Db 这将创建新的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
}
}
将图像保存到DB中:
如果您不想使用SQL Query,您还可以使用内容值将数据插入数据库。
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);
}
}
}