首页 > 解决方案 > 一个 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 ();
    }

标签: javaandroid

解决方案


由于ImageView可以托管任何类型的Drawable,而不仅仅是由资源 id 指定的,因此没有 API 可以将资源 idImageView 中取回。

也就是说,您始终可以在分配资源 idtag时将其保存到 ImageView 中:

int resourceId = file_plantas.getInt (5);
imgFotoE.setImageResource (resourceId);
imgFotoE.setTag(resourceId);

然后您可以稍后检索它:

int resourceId = (int) imgFotoE.getTag();

推荐阅读