Created
January 16, 2018 10:57
-
-
Save sahilpatel14/520b55ed437df1f56beb25077a0e1c78 to your computer and use it in GitHub Desktop.
Trying to save couchbase Blob having a JPG file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rapidbizapps.sample | |
import android.graphics.BitmapFactory | |
import android.media.ThumbnailUtils | |
import android.support.v7.app.AppCompatActivity | |
import android.os.Bundle | |
import android.util.Log | |
import kotlinx.android.synthetic.main.activity_test_couchbase.* | |
import android.graphics.Bitmap | |
import com.couchbase.lite.Blob | |
import com.couchbase.lite.MutableDocument | |
import com.rapidbizapps.data_engine.couchbase.CbHelper | |
import java.io.ByteArrayInputStream | |
import java.io.ByteArrayOutputStream | |
class TestCouchbaseActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_test_couchbase) | |
addImageToCouchbase() | |
} | |
fun addImageToCouchbase() { | |
val db = CbHelper.getInstance(this).db | |
try { | |
// val inStream = assets.open("samri.jpg") | |
val inStream = assets.open("splash.png") | |
val imageBitmap = BitmapFactory.decodeStream(inStream) | |
val thumbnail = ThumbnailUtils.extractThumbnail(imageBitmap, 150, 150) | |
inStream.close() | |
// Displaying all sorts of images here | |
// ivPicture.setImageBitmap(thumbnail) | |
// save as attachment to Document . | |
val out = ByteArrayOutputStream() | |
thumbnail.compress(Bitmap.CompressFormat.JPEG, 50, out) | |
thumbnail.compress(Bitmap.CompressFormat.PNG, 50, out) | |
val inp = ByteArrayInputStream(out.toByteArray()) | |
// val blob = Blob("image/jpg", inp) | |
val blob = Blob("image/png", inp) | |
val inDocument = MutableDocument() | |
inDocument.setBlob("image", blob) | |
val outDocument = db.save(inDocument) | |
val outBlob = outDocument.getBlob("image") | |
if(outBlob.content == null) { | |
Log.d(TAG, "content is null") | |
} | |
val outBitmap = BitmapFactory.decodeStream(outBlob.contentStream) | |
ivPicture.setImageBitmap(outBitmap) | |
} | |
catch (e : Exception) { | |
Log.e(TAG,"Error while loading image. ",e) | |
} | |
} | |
companion object { | |
val TAG = TestCouchbaseActivity::class.java.simpleName | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment