2016-03-10 134 views
1

Dears 我如何Android可以保存圖像並從Sqlite數據庫獲取圖像我使用Android Studio?Android保存並從Sqlite數據庫中獲取圖像

+0

將圖像轉換爲** Base 64 String **,然後將其存儲到數據庫中,並在取圖像時反之亦然。有關Android中的Base 64 API,請參閱[this](http://developer.android.com/reference/android/util/Base64.html)。 –

+0

如果要將二進制文件存儲到sqlite中,請將其保存爲blob類型。 也許這個鏈接可以幫助你:http://stackoverflow.com/questions/7331310/how-to-store-image-as-blob-in-sqlite-how-to-retrieve-it – thuongle

回答

0

您可以查看本教程以在Android中實現Sqlite。 http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

而是與聯繫,例如工作,可以實現這樣

public class Image{ 
    String imagePath; //it is your absolute image file path 
} 

而且你DatabaseHandler可以像下面

public class DatabaseHandler extends SQLiteOpenHelper { 

    // All Static variables 
    // Database Version 
    private static final int DATABASE_VERSION = 1; 

    // Database Name 
    private static final String DATABASE_NAME = "imagedb"; 

    // Contacts table name 
    private static final String TABLE_IMAGE = "images"; 

    // Contacts Table Columns names 
    private static final String KEY_ID = "id"; 
    private static final String KEY_IMAGE_PATH = "name"; 

    public DatabaseHandler(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    // Creating Tables 
    @Override 
    public void onCreate(SQLiteDatabase db) { 
     String sqlQuery = "CREATE TABLE " + TABLE_IMAGE + "(" 
       + KEY_ID + " INTEGER PRIMARY KEY," + KEY_IMAGE_PATH + " TEXT)"; 
     db.execSQL(sqlQuery); 
    } 

    // Upgrading database 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // Drop older table if existed 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_IMAGE); 

     // Create tables again 
     onCreate(db); 
    } 

    // Adding new image 
    public void addImage(Image image) { 
     SQLiteDatabase db = this.getWritableDatabase(); 

     ContentValues values = new ContentValues(); 
     values.put(KEY_IMAGE_PATH, image.imagePath); // Image path 

     // Inserting Row 
     db.insert(TABLE_IMAGE, null, values); 
     db.close(); // Closing database connection 
    } 


    // Getting single image 
    public Image getImage(int id) { 
     SQLiteDatabase db = this.getReadableDatabase(); 

     Cursor cursor = db.query(TABLE_IMAGE, new String[] { KEY_ID, 
         KEY_IMAGE_PATH}, KEY_ID + "=?", 
       new String[] { String.valueOf(id) }, null, null, null, null); 
     if (cursor != null) 
      cursor.moveToFirst(); 

     Image image = new Image(Integer.parseInt(cursor.getString(0)), 
       cursor.getString(1)); 
     // return image 
     return image; 
    } 
} 
+0

我想他想存儲整個圖像對象進入數據庫,而不是圖像路徑。 –

+0

謝謝@ᖷAIAAƸ。我以爲他在問一個sqlite教程。 – thuongle

1

可能爲時已晚來實現。但對未來的讀者有用..

import android.content.Context; 
import android.database.Cursor; 
import android.database.DatabaseUtils; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.database.sqlite.SQLiteStatement; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 


import java.util.HashMap; 

/** 
    * Created by Noorul on 23-05-2016. 
*/ 

@SuppressWarnings("ALL") 
public class DBSplash extends SQLiteOpenHelper { 

public static final String DATABASE_NAME = "SplashDB.db"; 
public static final String SPLASH_TABLE_NAME = "splash_db"; 

private HashMap hp; 

public DBSplash(Context context) { 
    super(context, DATABASE_NAME, null, 1); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL(
      "create table " + SPLASH_TABLE_NAME + "(name TEXT, image BLOB)" 
    ); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    // TODO Auto-generated method stub 
    db.execSQL("DROP TABLE IF EXISTS contacts"); 
    onCreate(db); 
} 

public boolean insertImage(String name, Bitmap img) { 
    Bitmap storedBitmap = null; 
    String sql = "INSERT INTO " + SPLASH_TABLE_NAME + " (name,image) VALUES(?,?)"; 
    SQLiteDatabase db = this.getWritableDatabase(); 
    SQLiteStatement insertStmt = db.compileStatement(sql); 

    byte[] imgByte = getBitmapAsByteArray(img); 
    try { 
     storedBitmap = getImage(name); 
    } catch (Exception e) { 
     AppLog.exception(e); 
    } 
    if (storedBitmap == null) { 
     insertStmt.bindString(1, name); 
     insertStmt.bindBlob(2, imgByte); 
     insertStmt.executeInsert(); 
     db.close(); 
    } 
    return true; 
} 


public int numberOfRows() { 
    SQLiteDatabase db = this.getReadableDatabase(); 
    int numRows = (int) DatabaseUtils.queryNumEntries(db, SPLASH_TABLE_NAME); 
    return numRows; 
} 


public Bitmap getImage(String name) { 
    String qu = "SELECT * FROM " + SPLASH_TABLE_NAME; 
    Cursor cur = null; 
    SQLiteDatabase db = this.getReadableDatabase(); 

    try { 
     cur = db.rawQuery(qu, new String[]{}); 
    } catch (Exception e) { 
     AppLog.exception(e); 
    } 
    if (cur != null) { 
     if (cur.moveToFirst()) { 
      int index = cur.getColumnIndexOrThrow("image"); 
      byte[] imgByte = cur.getBlob(index); 
      cur.close(); 
      return BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length); 
     } 
     if (cur != null && !cur.isClosed()) { 
      cur.close(); 
     } 
    } 

    return null; 
} 
    public byte[] getBitmapAsByteArray(Bitmap bitmap) { 
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 0, outputStream); 
    return outputStream.toByteArray(); 
} 
} 

使用此代碼。但是將圖像存儲在數據庫中並不是最佳實踐。如果您需要未映射的圖像,請更改圖像大小。圖像具有高內存的blob類型。手機是更小的設備。所以在sqlite數據庫中存儲很多圖像意味着,這將是醜陋的。所以使用@thuongle方法

相關問題