簡體   English   中英

如何在領域數據庫中將多個圖像Path作為String保存為一個條目

[英]How to save multiple image Path as String in realm database as one entry

我正在開發一個Android應用,應用的目的是通過廣告信息,如廣告標題,廣告描述和廣告圖片路徑(圖片路徑可以很多)來發布廣告。

我正在使用realm數據庫進行此操作。 將這些信息保存到數據庫時,我遇到了困難。 我想知道,如何通過一個廣告條目保存多個圖像路徑?

我的領域是:

Ad Title  
Ad Description  
Ad Image Path (Multi)

這是我的RealmObject類

public class FacebookAds extends RealmObject {

@PrimaryKey
@Index
private int id;
private String title;
private String tags;
private String description;
private String imageName; // How to use this for multiple image Path

public  FacebookAds() {

}

public String getImageName() {
    return imageName;
}

public void setImageName(String imageName) {
    this.imageName = imageName;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getTags() {
    return tags;
}

public void setTags(String tags) {
    this.tags = tags;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

}

這是我的廣告新標題對話框

請仔細閱讀首先將圖像轉換為ByteArray我正在進行演示

Bitmap bmp = intent.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

然后將byte []保存到Realm數據庫中

注意:字符串和字節數組(byte [])不能大於16 MB

Realm支持以下字段類型:boolean,byte,short,¯nt,long,float,double,String,Date和byte []。 整數類型byte,short,int和long都映射到Realm中的相同類型(實際上很長)。 此外,支持RealmObject和RealmList的子類來建模關系。

一種簡單直觀的方法是將它們連接起來,然后將它們存儲為單個字符串。 當您想要檢索它們時,只需從Realm數據庫中獲取字符串並將其拆分為分隔符。

例如:

String imagePath1 = ...
String imagePath2 = ...

// Store this string
String imagePath = imagePath1 + "|" + imagePath2;

// Retrieve paths like this
String[] paths = imagePath.split("|");

// imagePath1
paths[0]

// imagePath2
paths[1]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM