从ImageView获取图像路径

时间:2017-09-27 14:37:54

标签: android android-imageview

我正在尝试从imageView获取图像路径字符串。 使用SQLite数据库中的字符串路径存储来对imageView进行计费 当我在除了图像路径1之外的一些fiels中修改我的Db的项目时,我无法从imageView读回路径以​​正确更新数据库。因此,在进行任何更新之后,字段图像路径将变为空。

在代码中,当我单击“保存”按钮时,我调用了saveProduct()方法:

private void saveProduct() {
    // Read from input field
    //Use trim to eliminate leading or trailing white space
    String nameString = mNameEditText.getText().toString().trim();
    String qtyString = mQtyEditText.getText().toString().trim();
    String priceString = mPriceEditText.getText().toString().trim();
    String mailString = mEmailEditText.getText().toString().trim();
    String phoneString = mPhoneEditText.getText().toString().trim();

    // Check if this is a new product or an existing one
    if (mcurrentProdUri == null &&
            TextUtils.isEmpty(nameString) && TextUtils.isEmpty(qtyString)
            && TextUtils.isEmpty(priceString)&& TextUtils.isEmpty(mailString)
            && TextUtils.isEmpty(phoneString)) {
        return;
    }

    //Create a ContentValues object to populate the database
    ContentValues values = new ContentValues();
    values.put(InventoryContract.ProductEntry.COLUMN_PRODUCT_NAME, nameString);
    values.put(InventoryContract.ProductEntry.COLUMN_PRODUCT_QTY, qtyString);
    values.put(InventoryContract.ProductEntry.COLUMN_PRODUCT_PRICE, priceString);
    values.put(InventoryContract.ProductEntry.COLUMN_EMAIL, mailString);
    values.put(InventoryContract.ProductEntry.COLUMN_PHONE, phoneString);
    values.put(InventoryContract.ProductEntry.COLUMN_PRODUCT_PIC, picPath);

    //Determine if this is a new product or not by checking if mCurrentProdUri is null or not
    if (mcurrentProdUri == null) {

        Uri newUri = getContentResolver().insert(InventoryContract.ProductEntry.CONTENT_URI, values);

        // check if newUri is null or not
        if (newUri == null){
            //Show an error toast message
            Toast.makeText(this, "There has been an error inserting a new product", Toast.LENGTH_SHORT).show();
        } else {
            //Otherwise show a successful addition message
            Toast.makeText(this, "A new product has been added!", Toast.LENGTH_SHORT).show();

        }
    } else {
        //Otherwise if this is an existing product proceed updating it with new data

        int rowsAffected = getContentResolver().update(mcurrentProdUri, values, null, null);

        if (rowsAffected == 0) {
            // If no rows were affected show error toast message
            Toast.makeText(this, "There has been an error in updating the product", Toast.LENGTH_SHORT).show();
        } else {
            // Otherwise the update was successful and we can show the related Toast message
            Toast.makeText(this, "The product has been updated!", Toast.LENGTH_SHORT).show();
        }

    }

}

我已经阅读了所有相关的帖子,但我真的不知道如何让方法读取imageView以获取相关的图像路径(或Uri)

非常感谢您的支持

1 个答案:

答案 0 :(得分:0)

在加载imageview之前,您需要在活动/片段级别跟踪uri,因为常规ImageView不负责记住它的来源。

但是,您可以构建一个扩展的ImageView,如果它有助于您的情况,则负责该操作:

class MyImageView extends ImageView {

    private Uri mImageUri;

    public MyImageView(Context context) {
        super(context);
    }

    @Override
    public void setImageURI(@Nullable Uri uri) {
        super.setImageURI(uri);
        mImageUri = uri;
    }

    public Uri getImageUri(){
        return mImageUri;
    }
}

如果你确实走了那条路线,你必须在配置改变期间考虑视图状态保存,否则你可能会失去那个uri。