I want to save image uri from taking camera or gallery into SQLite.
And I want to display the image which called uri from SQLite.
If I want to do this, someone said you have to save image uri into SQLite as byte, and
you can set image on imageView.
I understand the theory, but I am still getting stuck with my coding.
If it is true, I want to save formatted image into sdcard or somewhere.
someone said use BitmapFactory and decodeResource.
and call the uri from R.drawable.
However, I don't know how to save image into R.drawable folder.
Could you help me? I will give you some my coding.
I am fighting with saving image into SQLite and how to load it, and how to modify it during two weeks!
Sorry for really long coding. I don't know where I am now. Thank you.
fridgeDetails.java
populateFields();
private void populateFields() 
{
    if (mRowId != null)
    {
        Cursor data = mDbHelper.fetchItem(mRowId);
        startManagingCursor(data);
        //load image from sqlite
        byte[] blob = data.getBlob(data.getColumnIndexOrThrow(FridgeDbAdapter.KEY_IMAGE));
        mImageView.setImageBitmap(BitmapFactory.decodeByteArray(blob, 0, blob.length));
        nameEdit.setText(data.getString(data.getColumnIndexOrThrow(FridgeDbAdapter.KEY_NAME)));
        categoryEdit.setText(data.getString(data.getColumnIndexOrThrow(FridgeDbAdapter.KEY_CATEGORY)));
        expired_Date_Btn.setText(data.getString(data.getColumnIndexOrThrow(FridgeDbAdapter.KEY_EXPIRED_DATE)));
    }
    else{
    expired_Date_Btn.setText(
            new StringBuilder()
            .append(mDay).append("/")
            //month is 0 based. Then add 1
            .append(mMonth + 1).append("/")
            .append(mYear).append(" "));    
    }
}
    //create dialog for taking image
    ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item,items);
    AlertDialog.Builder builder  = new AlertDialog.Builder(this);
    builder.setTitle("Select Image");
    builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) 
        {
            if(item==0)
            {
                Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                try
                {
                    cameraIntent.putExtra("return-data", true);
                    startActivityForResult(cameraIntent, PICK_FROM_CAMERA); 
                }
                catch(ActivityNotFoundException e)
                {
                    e.printStackTrace();
                }                   
            }
            else
            {
                Intent galleryIntent = new Intent();
                galleryIntent.setType("image/*");
                galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
                //image chooser
                startActivityForResult(Intent.createChooser(galleryIntent, "Complete action using"), PICK_FROM_GALLERY);
            }
        }   
    });
confirmButton.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) {
            //set alarm with expiration date                
            am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            setOneTimeAlarm();
            Toast.makeText(fridgeDetails.this, "Alarm automatic set", Toast.LENGTH_SHORT).show();
            saveState();
            setResult(RESULT_OK);
            finish();
        }
    });
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);
     if (resultCode != RESULT_OK) return;
       switch (requestCode)
       {
        case PICK_FROM_CAMERA:
            Bundle extras = data.getExtras();
            Bitmap selectedImage = (Bitmap) extras.get("data");
            selectedImage = Bitmap.createScaledBitmap(selectedImage, 200, 200, false);
            mImageView.setImageBitmap(selectedImage);
            break;
        case PICK_FROM_GALLERY:
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath);
            Bitmap bt=Bitmap.createScaledBitmap(bitmap, 200, 200, false);
            mImageView.setImageBitmap(bt);
        break;
       }
}
protected void onSaveInstanceState(Bundle outState)
{
    super.onSaveInstanceState(outState);
    saveState();
}
@Override
protected void onPause()
{
    super.onPause();
    saveState();
}
@Override
protected void onResume()
{
    super.onResume();
    populateFields();
}
private void saveState() 
{
    String name = (String) nameEdit.getText().toString();
    String category = (String) categoryEdit.getText().toString();
    String expired_date = (String) expired_Date_Btn.getText().toString();
    byte[] image = ConvertDrawableToByteArray(mImageView.getDrawable());
    if(mRowId == null)
    {
        long id = mDbHelper.insertItem(category, name, expired_date, image);
        if(id>0)
        {
            mRowId = id;
        }           
    }
    else 
    {
        mDbHelper.updateItem(mRowId, category, name, expired_date, image);
    }   
}
public static byte[] ConvertDrawableToByteArray(Drawable drawableResource) {
    Bitmap imageBitmap = ((BitmapDrawable) drawableResource).getBitmap();
    ByteArrayOutputStream imageByteStream = new ByteArrayOutputStream();
    imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, imageByteStream);
    byte[] imageByteData = imageByteStream.toByteArray();
    return imageByteData;
}
 
     
     
    