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

时间:2016-09-15 16:34:15

标签: java android sqlite android-contentprovider

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

现在相机拍摄的图像在imageView上曝光,它不存储在表格中。

EditorActivity.Java

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) {

}
}

1 个答案:

答案 0 :(得分:0)

    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
                }