ذخیره آدرس عکس در پایگاه داده sqlite و فراخوانی آنها در برنامه
من از کد زیر برای نمایش عکس هایی که در دیتابیس sqlite به صورت تصویر ذخیره شده اند استفاده کردم اما برای ذخیره آدرس عکس ها در دیتابیس sqlite و نمایش آنها در برنامه نمی دانم چه کار کنم؟
Database.java:
package com.example.newdatabaseproject;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import android.R.raw;
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.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class Database extends SQLiteOpenHelper {
String DB_PATH = null;
public static String DB_NAME = "bank1";
private SQLiteDatabase myDataBase;
private final Context myContext;
public Database(Context context) {
super(context,DB_NAME, null, 2);
this.myContext = context;
DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
} else {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
// database does't exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myInput.close();
myOutput.flush();
myOutput.close();
}
public byte[] getpic(int id){
Cursor cu=myDataBase.rawQuery("select * from tbl2 where ID="+id,null);
cu.moveToFirst();
byte[] s;
s=cu.getBlob(2);
return s;
}
public void openDataBase() throws SQLException {
// Open the database
String myPath = DB_PATH + DB_NAME;
// SQLiteDatabase.NO_LOCALIZED_COLLATORS
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY
| SQLiteDatabase.NO_LOCALIZED_COLLATORS
| SQLiteDatabase.CREATE_IF_NECESSARY);
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
// return cursor
public Cursor query(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having,
String orderBy) {
return myDataBase.query("pwp_singers", null, null, null, null, null,
null);
}
@Override
public void onUpgrade(SQLiteDatabase myDataBase, int oldVersion,int newVersion)
{
}
public void useable() {
boolean checkdb=checkDataBase();
if(checkdb){
}else{
this.getReadableDatabase();
try{
copyDataBase();
}catch(IOException e){
}
}
}
public void open(){
myDataBase=SQLiteDatabase.openDatabase(DB_PATH+DB_NAME, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}
}
************************************
Main.java
package com.example.newdatabaseproject;
import java.io.InputStream;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class Main extends Activity {
private EditText et;
private Button btn;
private Database db;
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et=(EditText)findViewById(R.id.et1);
img=(ImageView)findViewById(R.id.img);
btn=(Button)findViewById(R.id.btn);
db=new Database(this);
db.useable();
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
db.open();
byte[] p=db.getpic(Integer.parseInt(et.getText().toString()));
if(p!=null){
Bitmap bm=BitmapFactory.decodeByteArray(p, 0, p.length);
img.setImageBitmap(bm);
}else{
}
db.close();
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
omid1="image";//esme aks be soorat string int omid; image=(ImageView) findViewById(R.id.im1); Context context=image.getContext(); omid=context.getResources().getIdentifier(omid1, "drawable", context.getPackageName()); image.setImageResource(omid);
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .