我是Android和英语的新手,对此感到抱歉。我创建了一个包含图像的表,一些字符串表达式。我想在SQLite中将此图像存储为blob,因为我不知道另一种存储图像的方法。无论如何我想在imageView中显示这个图像但是当我点击buttonNew活动关闭时。我无法理解这个问题是什么?我希望有人帮助我或建议我另一种存储图像的方法。感谢您的阅读
Database.java文件
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.Toast;
import org.apache.http.util.ByteArrayBuffer;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class Database {
DatabaseHelper databaseHelper;
private Context context;
SQLiteDatabase sqLiteDatabase;
public Database(Context context){
databaseHelper = new DatabaseHelper(context);
}
static class DatabaseHelper extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "alesuygulama.db";
public static final String TABLE_NAME = "alesuygulamatablo";
private static final int DATABASE_VERSION = 2;
//tablo kolonları
private static final String ID = "_id";
private static final String YIL = "yil";
private static final String MEVSIM = "mevsim";
private static final String KISIM = "kisim";
public static final String SORU = "soru";
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME
/*+ " ( " + ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ YIL + " TEXT NOT NULL, "
+ MEVSIM + " TEXT NOT NULL, "
+ KISIM +" TEXT NOT NULL, "*/
+ " ( " +SORU + " BLOB );";
private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
private Context context;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE);
} catch (SQLException e) {
System.out.println("Veri tabanı oluşturulamadı.");
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
db.execSQL(DROP_TABLE);
onCreate(db);
} catch (SQLException e) {
System.out.println("Veri tabanı yenilenemedi.");
e.printStackTrace();
}
}
}
}
活动文件
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.os.SystemClock;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ActivitySinav extends ActionBarActivity implements View.OnClickListener {
Button buttonOld;
Button buttonNew;
ImageView imageView;
SQLiteDatabase sqLiteDatabase;
Database.DatabaseHelper databaseHelper;
Cursor c = null;
byte[] img = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sinav);
buttonOld = (Button)findViewById(R.id.buttonOld);
buttonNew = (Button)findViewById(R.id.buttonNew);
imageView = (ImageView)findViewById(R.id.imageView);
buttonOld.setOnClickListener(this);
buttonNew.setOnClickListener(this);
databaseHelper = new Database.DatabaseHelper(getApplicationContext());
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.resim1);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, bos);
img = bos.toByteArray();
sqLiteDatabase = databaseHelper.getWritableDatabase();
}
@Override
public void onClick(View v) {
if(buttonOld == v)
{
ContentValues values = new ContentValues();
values.put("SORU", img);
sqLiteDatabase.insert(databaseHelper.TABLE_NAME, null, values);
Toast.makeText(this, "inserted successfully", Toast.LENGTH_SHORT).show();
}
else if(buttonNew == v){
String[] col = {"SORU"};
c = sqLiteDatabase.query(databaseHelper.TABLE_NAME, col, null, null, null, null,null);
if(c != null){
c.moveToFirst();
do{
img = c.getBlob(c.getColumnIndex("SORU"));
}while(c.moveToNext());
Bitmap b1 = BitmapFactory.decodeByteArray(img, 0, img.length);
imageView.setImageBitmap(b1);
Toast.makeText(this, "Retrieve successfully", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_activity_sinav, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
和布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="1dp"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:paddingTop="1dp"
tools:context="com.example.emri.alesuygulama.ActivitySinav">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="00:00:00"
android:id="@+id/timerValue"
android:layout_marginLeft="5dp" />
<ImageView
android:layout_width="370dp"
android:layout_height="320dp"
android:id="@+id/imageView"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="7dp" />
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="A"
android:id="@+id/buttonA"
android:layout_below="@+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="15dp" />
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="B"
android:id="@+id/buttonB"
android:layout_alignBottom="@+id/buttonA"
android:layout_toRightOf="@+id/buttonA"
android:layout_toEndOf="@+id/buttonA"
android:layout_marginLeft="25dp" />
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="C"
android:id="@+id/buttonC"
android:layout_alignBottom="@+id/buttonB"
android:layout_toRightOf="@+id/buttonB"
android:layout_toEndOf="@+id/buttonB"
android:layout_marginLeft="25dp" />
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="D"
android:id="@+id/buttonD"
android:layout_alignBottom="@+id/buttonC"
android:layout_toRightOf="@+id/buttonC"
android:layout_toEndOf="@+id/buttonC"
android:layout_marginLeft="25dp" />
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="E"
android:id="@+id/buttonE"
android:layout_below="@+id/imageView"
android:layout_alignRight="@+id/imageView"
android:layout_alignEnd="@+id/imageView"
android:layout_marginLeft="25dp"
android:layout_marginRight="15dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Önceki Soru"
android:id="@+id/buttonOld"
android:layout_below="@+id/buttonA"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView"
android:layout_marginTop="25dp"
android:layout_marginLeft="35dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sonraki soru"
android:id="@+id/buttonNew"
android:layout_alignBottom="@+id/buttonOld"
android:layout_toRightOf="@+id/buttonC"
android:layout_toEndOf="@+id/buttonC"
android:layout_marginTop="25dp"
android:layout_marginRight="35dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pas geç"
android:id="@+id/buttonEmpty"
android:layout_below="@+id/buttonOld"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Sınavı Bitir"
android:id="@+id/buttonEnd"
android:layout_below="@+id/buttonEmpty"
android:layout_centerHorizontal="true" />
</RelativeLayout>
答案 0 :(得分:0)
将图像保存到本地数据库有三种方法: 1 - 保存图像blob。 2将图像保存到目录,然后将目录路径保存到DB。 3在db。中将图像保存为base64。
但是选项1和2主要由开发人员采用。所以只是展望这些选项。