[英]Image of an ImageView to int Android Studio
好朋友,我怎樣才能將ImageView
的圖像傳遞給 andrid studio 中的 int 以便能夠將其保存在 BD 中,我以這種方式從數據庫中加載圖像:
Intent intent = getIntent ();
Bundle b = intent.getExtras ();
AdminSQLiteOpenHelper adminPlants = new AdminSQLiteOpenHelper (this, "Plants", null, 1);
SQLiteDatabasePlantsDatabase = PlantsPlants.getWritableDatabase ();
Cursor file_plants = BaseDeDatosPlantas.rawQuery
("SELECT * from species where commonname = '" + b.getString ("name") + "'", null);
if (file_plantas.moveToFirst ()) {
tvNameE.setText (b.getString ("name"));
tvSpeciesE.setText (file_plants.getString (2));
tvDescripcionE.setText (file_plantas.getString (4));
imgFotoE.setImageResource (file_plantas.getInt (5)); // Here I send the image to the ImageView
}
PlantsDatabase.close ();
在應用程序中,圖像在設備上被更改為另一個,現在我做相反的事情,從ImageView
獲取圖像並將其傳遞給 int,從而能夠再次將其保存在 BD 中。
public void SaveE (View view) {
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper (this, "Plants", null, 1);
SQLiteDatabase DataBase = admin.getWritableDatabase ();
Database.execSQL ("update species set description = '" + tvDescripcionE.getText () + "', photo =" + imageResource
+ "where CommonName = '" + tvNameE.getText () + "';"); // This is where I need the int of the image
Database.close ();
}
由於ImageView
可以托管任何類型的Drawable
,而不僅僅是由資源 id 指定的,因此沒有 API 可以從ImageView 中取回資源 id。
也就是說,您始終可以在分配資源 ID 時將其作為tag
保存到 ImageView 中:
int resourceId = file_plantas.getInt (5);
imgFotoE.setImageResource (resourceId);
imgFotoE.setTag(resourceId);
然后您可以稍后檢索它:
int resourceId = (int) imgFotoE.getTag();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.