繁体   English   中英

如何使用内容提供者将相机图像保存在sqlite数据库中?

[英]How to save camera images in sqlite database using content providers?

我正在制作一个示例清单应用程序,使用内容提供程序将一个字符串和两个整数值存储在数据库中,然后填充到列表视图中,并通过列表的详细信息视图中的setOnItemClickListener()进行填充,现在我想添加相机拍摄的图像到editorActivity的每一行并在列表的详细信息视图上膨胀图像,我知道BLOB类型用于添加位图图像,该位图图像被转换为​​byteArray然后存储在表中,但对于内容提供商,我仍然不清楚,非常抱歉,没有明确解释这一点,将不胜感激,谢谢。

现在,相机已将图像拍摄并在imageView上放大了,但该图像没有存储在表格中。

编辑器活动

public class EditorActivity extends AppCompatActivity {

private static final int CAMERA_REQUEST = 1888;

EditText mProductNameEditText;
EditText mProductQuantityEditText;
EditText mProductPriceEditText;
Button mSaveProductButton;
ImageButton mPhotoButton;
ImageView mPhotoImage;
Bitmap mPhoto;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_editor);

    mProductNameEditText = (EditText) findViewById(R.id.product_name);
    mProductQuantityEditText = (EditText) findViewById(R.id.product_quantity);
    mProductPriceEditText = (EditText) findViewById(R.id.product_price);

    mPhotoImage = (ImageView) findViewById(R.id.inventory_editor_photo);
    mPhotoButton = (ImageButton) findViewById(R.id.inventory_editor_camera);

    imageButtonClicker();

    mSaveProductButton = (Button) findViewById(R.id.save_product);

    saveButtonClicker();
}

public void imageButtonClicker(){

    mPhotoButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }
    });
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        mPhoto = (Bitmap) data.getExtras().get("data");
        mPhotoImage.setImageBitmap(mPhoto);
    }
}

public void saveButtonClicker(){

    mSaveProductButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            insertInventory();
            finish();
        }
    });
}

private void insertInventory(){

    String productNameString = mProductNameEditText.getText().toString().trim();
    String productQuantityString = mProductQuantityEditText.getText().toString().trim();
    String productPriceString = mProductPriceEditText.getText().toString().trim();

    if(TextUtils.isEmpty(productNameString) || TextUtils.isEmpty(productQuantityString) || TextUtils.isEmpty(productPriceString)){

        Toast.makeText(this, "Error: at-least one or all of the Inventory fields were blank.", Toast.LENGTH_SHORT).show();
        return;
    }

    ContentValues values = new ContentValues();
    values.put(InventoryContract.InventoryEntry.COLUMN_INVENTORY_NAME, productNameString);
    values.put(InventoryContract.InventoryEntry.COLUMN_INVENTORY_QUANTITIY, productQuantityString);
    values.put(InventoryContract.InventoryEntry.COLUMN_INVENTORY_PRICE, productPriceString);

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

    if (newUri == null) {
        // If the row ID is -1, then there was an error with insertion.
        Toast.makeText(this, "Error with saving Inventory Product", Toast.LENGTH_SHORT).show();
    } else {
        // Otherwise, the insertion was successful and we can display a toast with the row ID.
        Toast.makeText(this, "Inventory Product saved.", Toast.LENGTH_SHORT).show();
    }
}
}

InventoryDbHelper.java

public class InventoryDbHelper extends SQLiteOpenHelper {

public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "Inventory.db";

public InventoryDbHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {

    String SQL_CREATE_INVENTORY_TABLE=
            "CREATE TABLE " + InventoryContract.InventoryEntry.TABLE_NAME + " (" +
                    InventoryContract.InventoryEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                    InventoryContract.InventoryEntry.COLUMN_INVENTORY_NAME + " TEXT  NOT NULL, " +
                    InventoryContract.InventoryEntry.COLUMN_INVENTORY_QUANTITIY + " INTEGER NOT NULL DEFAULT 0, " +
                    InventoryContract.InventoryEntry.COLUMN_INVENTORY_PRICE + " INTEGER NOT NULL," +
                    InventoryContract.InventoryEntry.COLUMN_INVENTORY_KEY_IMAGE + " BLOB," +
                    InventoryContract.InventoryEntry.COLUMN_INVENTORY_KEY_TAG + " TEXT);";


    sqLiteDatabase.execSQL(SQL_CREATE_INVENTORY_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}
}
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
            mPhoto = (Bitmap) data.getExtras().get("data");
            mPhotoImage.setImageBitmap(mPhoto);

     ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                    mPhoto.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                        File file = new File(Environment.getExternalStorageDirectory() + File.separator + "images" + today_date + ".jpg");
                    try {
                        file.createNewFile();
                        FileOutputStream fo = new FileOutputStream(file);
                        //5
                        fo.write(bytes.toByteArray());
                        fo.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        Toast.makeText(Servicing.this, "Oops not able to capture image.", Toast.LENGTH_SHORT).show();
                    }``
        }
    }


save image to sqlite add this code in your insertInventory()

   if (mPhoto != null) {
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    mPhoto.compress(Bitmap.CompressFormat.PNG, 0, stream);
                    values.put(InventoryContract.InventoryEntry.COLUMN_INVENTORY_KEY_IMAGE,stream.toByteArray());
                } else {
                   //do what you want
                }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM