java - 一个 ImageView 的图像到 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 可以将资源 id从ImageView 中取回。
也就是说,您始终可以在分配资源 idtag
时将其保存到 ImageView 中:
int resourceId = file_plantas.getInt (5);
imgFotoE.setImageResource (resourceId);
imgFotoE.setTag(resourceId);
然后您可以稍后检索它:
int resourceId = (int) imgFotoE.getTag();
推荐阅读
- javascript - 如何使用有条件的 Joi 替代方案进行模式验证?
- telerik-reporting - 报告预览不显示数据
- if-statement - 在销售订单中生成警告消息
- sql - 将数据插入到具有主键的表(多列)中,该表具有来自另一个数据的主键,该数据具有除主键之外的数据
- r - 检查 R 中的数据(是否存在数据框列中的全范围值)
- javascript - 如何使用 php(laravel) 保护 html (blade) 中的变量/标志
- c++ - 带有 QNetworkReply 的架构 x86_64 的未定义符号
- c# - 如何制作用户定义的矩形数组并对其进行迭代
- c++ - 使用迭代方法的垂直顺序遍历
- python - 如何检测主要是数字的字符串