I am receiving a byteArray of my image over a socket from a localserver. the byteArray is represented as RGB. I'm converting it to bitmap. I checked that the bitmap is not empty. but when I use ImageView.setImageBitmap(bitmap) then my android app crashes. I've already tried a lot of things and I can't find a solution yet. help me please
class Cam : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Log.d("Cam activity", "onCreate")
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_cam)
val button_back : Button = findViewById(R.id.button_back)!!
var textView : TextView = findViewById(R.id.textView)!!
var imageView: ImageView = findViewById(R.id.Image_update)!!
Thread {
Thread.sleep(1000)
var byteArray = ByteArray(76032)
Connection.inBytesCam?.read(byteArray)
var nrOfPixels = byteArray.size / 3 // Three bytes per pixel.
var pixels = IntArray(nrOfPixels)
for (i in 0 until nrOfPixels){
var r = byteArray.get(3*i).toInt()
var g = byteArray.get(3*i + 1).toInt()
var b = byteArray.get(3*i + 2).toInt()
pixels.set(i, Color.rgb(r, g, b))
}
var bitmap = Bitmap.createBitmap(pixels, 176, 144, Bitmap.Config.ARGB_8888)
imageView.setImageBitmap(bitmap)
}.start()
button_back.setOnClickListener {
Thread {
runOnUiThread {
val intent = Intent(this#Cam, ControlActivity::class.java)
startActivity(intent)
}
}.start()
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/purple_200"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="-103dp">
<Button
android:id="#+id/button_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/purple_700"
android:fontFamily="monospace"
android:text="Back"
android:textAlignment="center"
android:textSize="34sp"
app:iconTint="#color/purple_700"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.862" />
<TextView
android:id="#+id/textView"
android:layout_width="246dp"
android:layout_height="135dp"
android:layout_marginBottom="52dp"
android:text="LOG"
android:textAlignment="textStart"
app:layout_constraintBottom_toTopOf="#+id/button_back"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="#+id/Image_update"
android:layout_width="176dp"
android:layout_height="144dp"
android:layout_marginTop="100dp"
app:layout_constraintBottom_toTopOf="#+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="#drawable/camera_recording" />
</androidx.constraintlayout.widget.ConstraintLayout>
i use DataInputStream/DataOutputStream to transfer ImageBytes from localserver to client on android.
i cant find anything that solve my problem
in general, I need to have an image update in a loop, like video player. but I can't even update one image :(
Use Glide library as below
add dependency;
implementation 'com.github.bumptech.glide:glide:4.11.0'
use in your activity etc.
Glide.with(context)
.load(bitmap)
.into(imageView)
Related
I am trying to capture image within the rectangle as shown in image, using CameraX.
The way I am going on about this is, by having the ratio of Screen to Caputred image calculated and then mapping the rectangle visible in camera to another rectangle(equivalent to the captured image) and then cropping the bitmap. The code is as follows.
fragment_scan_doc.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/clRoot"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/gdl_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="28dp" />
<TextView
android:id="#+id/tvTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_25dp"
android:text="#string/app_name"
android:textColor="#color/black"
android:textSize="#dimen/text_size_24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="#id/gdl_right"
app:layout_constraintStart_toStartOf="#id/gdl_left"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvDesc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_16dp"
android:text="#string/place_doc"
android:textColor="#color/black"
app:layout_constraintEnd_toEndOf="#+id/gdl_right"
app:layout_constraintStart_toStartOf="#+id/gdl_left"
app:layout_constraintTop_toBottomOf="#+id/tvTitle" />
<androidx.camera.view.PreviewView
android:id="#+id/previewView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="#dimen/dimen_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/tvDesc" />
<TextView
android:id="#+id/tvCamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
android:background="#drawable/bg_round_white_solid_yellow_stripe"
android:gravity="center"
android:paddingStart="#dimen/dimen_25dp"
android:paddingTop="#dimen/dimen_10dp"
android:paddingEnd="#dimen/dimen_25dp"
android:paddingBottom="#dimen/dimen_10dp"
android:text="Front"
android:textColor="#color/black"
app:layout_constraintEnd_toEndOf="#id/gdl_right"
app:layout_constraintStart_toStartOf="#id/gdl_left"
app:layout_constraintTop_toTopOf="#+id/previewView" />
<View
android:id="#+id/border_view"
android:layout_width="325dp"
android:layout_height="202.5010436dp"
android:background="#drawable/bg_round_grey_stripe"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#id/gdl_right"
app:layout_constraintStart_toStartOf="#id/gdl_left"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvPoweredBy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="Powered by"
android:textColor="#color/white"
app:layout_constraintEnd_toEndOf="#id/gdl_right"
app:layout_constraintStart_toStartOf="#id/gdl_left"
app:layout_constraintTop_toBottomOf="#+id/border_view" />
<View
android:id="#+id/vTakePhoto"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginBottom="20dp"
android:background="#drawable/bg_round_white_solid_stripe"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
<ImageView
android:id="#+id/ivFlash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:background="#drawable/bg_circle_white_solid"
android:padding="10dp"
android:src="#drawable/ic_baseline_flash_off_24"
app:layout_constraintBottom_toBottomOf="#+id/vTakePhoto"
app:layout_constraintStart_toEndOf="#+id/vTakePhoto"
app:layout_constraintTop_toTopOf="#+id/vTakePhoto" />
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="50dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/gdl_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="28dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is the place where I get Image from camerax and I submit it to the cropImage() function
private fun takePhoto() {
imageCapture?.takePicture(ContextCompat.getMainExecutor(requireContext()),
object : ImageCapture.OnImageCapturedCallback() {
#SuppressLint("UnsafeOptInUsageError")
override fun onCaptureSuccess(imageProxy: ImageProxy) {
val image = imageProxy.image?.let { image ->
InputImage.fromMediaImage(
image,
imageProxy.imageInfo.rotationDegrees
)
}
cropImage(imageProxy)
}
override fun onError(exception: ImageCaptureException) {
exception.printStackTrace()
}
})
}
This is the cropImage() function
private fun cropImage(imageProxy: ImageProxy) {
val ori = imageProxy.convertImageProxyToBitmap().rotate(90f)
val rect = Rect()
mViewBinding.borderView.getGlobalVisibleRect(rect)
val screenWidth = mViewBinding.root.width
val screenHeight = mViewBinding.root.height
val ratioW = screenWidth.toFloat() / ori.width // I am trying to get the ratio of screen to image width
val ratioH = screenHeight.toFloat() / ori.height
val x1 = rect.left / ratioW
val y1 = rect.top / ratioH
val x1W = rect.right / ratioW
val x1H = rect.bottom / ratioH
val rect1 = Rect(x1.toInt(), y1.toInt(), x1W.toInt(), x1H.toInt())
val cropedBm = Bitmap.createBitmap(
ori,
rect1.left,
rect1.top,
rect1.right - rect1.left,
rect1.bottom - rect1.top
)
processNewBitmap(cropedBm) // this is where I display the cropped bitmap and it appears like in the image below
}
The image is cropped a bit below the rectangle frame.
I am not quite sure what I am doing wrong. Any help or direction would be highly appreciated. Thanks
your ImageProxy has a bitmap you want to crop with a view overlay that does not same width or height. You must calculate with scale ratio.
You can refer to this link:
https://www.codeproject.com/Articles/1276135/Crop-Image-from-Camera-on-Android
I am creating a Gallery application and i have a Tab that shows all the videos of a folder. The layout must show a preview of the video and the duration inside a textview upon the preview.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp">
<ImageView
android:id="#+id/video_preview"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_margin="4dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/logo"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/video_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:layout_marginBottom="4dp"
android:text="00:00"
android:background="#drawable/primary_background"
android:paddingHorizontal="4dp"
android:paddingVertical="2dp"
android:textColor="#color/colorWhite"
android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
The only thing that i have from the data is the AttachmentItem which holds the path of the Video.
class VideoViewHolder internal constructor(itemView: View) : GalleryItemVewHolder(itemView) {
private val videoPreview: ImageView = itemView.findViewById(R.id.video_preview)
private val selectedBox: AppCompatCheckBox = itemView.findViewById(R.id.selectedBox)
private val videoDuration: TextView = itemView.findViewById(R.id.video_duration)
fun bind(
video: FileGalleryUi.VideoItem,
videoCallback: RecyclerCallback.GalleryCallback,
notifyCallback: (file: FileGalleryUi, pos: Int) -> Unit
) {
val originalFile = video.attachmentItem.toFile(itemView.context)
Glide.with(itemView.context)
.load(originalFile)
.error(ContextCompat.getDrawable(itemView.context, R.drawable.ic_gallery))
.into(videoPreview)
// Below line makes the recycler to lag when it scrolls. If i comment out the scrolling become very smooth as expected.
tryOrNull {
MediaMetadataRetriever().apply {
setDataSource(originalFile.absolutePath)
}.also { mmr ->
videoDuration.text = DateUtils.millisToDuration(
mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)?.toLong()
)
videoDuration.visibility = View.VISIBLE
}
} ?: run {
videoDuration.visibility = View.GONE
}
}
companion object {
fun from(parent: ViewGroup): VideoViewHolder =
VideoViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.video_gallery_item, parent, false)
)
}
}
Its obvious to me that if i comment out the Duration setup block (from the tryOrNull) the recycler scrolls very smooth and if i keep it uncommented then it lags when scroll.
Is there a better and more performant way to get the duration without block the UI?
since yesterday I have tried to change the content of an imageView into a fragment but beautiful the button and it does nothing.
I would appreciate any help
Method used in the fragment:
override fun onClick(v: View?) {
when (v?.id) {
R.id.nextButton -> {
// val rnds = (0..99999).random()
val quest1 = "https://My-web-get-image-url.com/"
// var randomGetNumber = quest1 + rnds
Picasso.get().load(quest1).memoryPolicy(MemoryPolicy.NO_CACHE).into(memeRandomView)
Picasso.get().setLoggingEnabled(true)
}
else -> {
}
}
}
}
My layaout
<ImageView
android:id="#+id/memeRandomView"
android:layout_width="385dp"
android:layout_height="452dp"
android:layout_marginStart="32dp"
android:layout_marginTop="28dp"
android:layout_marginEnd="32dp"
android:src="#drawable/logo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.578"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/nextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/memeRandomView"
app:layout_constraintVertical_bias="0.434" />
After you get a reference for your views(nextButton,memeRandomView) using findViewById(),viewBinding,kotlin-ext , you can set a click like that and use picasso in the click listener:
nextButton.setOnClickListener{
Picasso.get()
.load(url)
.centerCrop()
.into(memeRandomView)
}
My click listener are not working even if I check with a Toast inside of them, it use to work but after I made a couple of change in my viewModel it stop working, I can't figure out what went wrong. This happen in my detail activity only, but work on the recyclerview that call this detail activity via intent. I'm using Viewmodel, Livedata, databinding and Room. The recyclerview and the detail view are using the same viewmodel.
This is the code of my Detail activity:
class BuyDetailActivity : AppCompatActivity() {
private lateinit var sharedViewModel: BuySharedViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lateinit var buy: Buy
sharedViewModel = ViewModelProviders.of(this).get(BuySharedViewModel::class.java)
val position = intent.getIntExtra("position", 0)
sharedViewModel.allBuys.observe(this, Observer<List<Buy>> { buys ->
buy = buys[position]
val binding: com.example.drake.kunuk.databinding.ActivityBuyDetailBinding =
DataBindingUtil.setContentView(this, com.example.drake.kunuk.R.layout.activity_buy_detail)
binding.buy = buy
val agentNumber = buy.agentNumber
bnvContactAgent.setOnNavigationItemSelectedListener { item ->
when (item.itemId) {
com.example.drake.kunuk.R.id.action_call -> {
val callNumberUri = Uri.parse("tel:$agentNumber")
val callIntent = Intent(Intent.ACTION_DIAL, callNumberUri)
startActivity(callIntent)
}
com.example.drake.kunuk.R.id.action_sms -> {
val smsNumberUri = Uri.parse("sms:$agentNumber")
val smsIntent = Intent(Intent.ACTION_SENDTO, smsNumberUri)
startActivity(smsIntent)
}
com.example.drake.kunuk.R.id.action_email -> {
val uriText = "mailto:xxxxxxxx#gmail.com" +
"?subject=" + Uri.encode("I'm interested in $agentNumber") +
"&body=" + Uri.encode("Hello, ")
val uri = Uri.parse(uriText)
val sendIntent = Intent(Intent.ACTION_SENDTO)
sendIntent.data = uri
startActivity(Intent.createChooser(sendIntent, "Send email"))
}
}
false
}
// set animation duration via code, but preferable in your layout files by using the animation_duration attribute
expandableTextView.setAnimationDuration(750L)
// set interpolators for both expanding and collapsing animations
expandableTextView.setInterpolator(OvershootInterpolator())
// or set them separately
expandableTextView.expandInterpolator = OvershootInterpolator()
expandableTextView.collapseInterpolator = OvershootInterpolator()
// toggle the ExpandableTextView
buttonToggle.setOnClickListener {
buttonToggle.setText(if (expandableTextView.isExpanded) com.example.drake.kunuk.R.string.more else com.example.drake.kunuk.R.string.less)
expandableTextView.toggle()
}
// but, you can also do the checks yourself
buttonToggle.setOnClickListener {
if (expandableTextView.isExpanded) {
expandableTextView.collapse()
buttonToggle.setText(com.example.drake.kunuk.R.string.more)
} else {
expandableTextView.expand()
buttonToggle.setText(com.example.drake.kunuk.R.string.less)
}
}
//Open photoView activity when clicked
ivHouseDetail.setOnClickListener {
applicationContext
.startActivity(
Intent(
applicationContext,
ViewPagerActivity::class.java
)
.putExtra("imageList", buy.propertyImage)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
)
}
})
}
}
Here's my SharedViewmodel, use by both the fragment calling the detail activity and the detail activity.
class BuySharedViewModel(application: Application) : AndroidViewModel(application) {
private val repository: BuyRepository
var allBuys: LiveData<List<Buy>>
init {
val buyDao = KunukRoomDatabase.getDatabase(application, viewModelScope).buyDao()
val buyRemote = BuyRemote()
repository = BuyRepository.getInstance(buyDao , buyRemote)
//Use async because it return a result
viewModelScope.async { getAllBuys() }
allBuys = buyDao.loadAllBuys()
}
private suspend fun getAllBuys() {
repository.getBuys()
}
}
And finally this is the xml of the detail activity:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable name="buy" type="com.example.drake.kunuk.data.model.Buy"/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_height="match_parent" android:layout_width="match_parent"
>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bnvContactAgent"
android:layout_width="match_parent"
android:background="#color/colorPrimary"
app:itemIconTint="#color/colorSecondary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:labelVisibilityMode="unlabeled"
app:menu="#menu/bottom_nav_contact_agent"
android:layout_height="wrap_content"
app:layout_constraintHorizontal_bias="1.0"/>
<ScrollView
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_height="0dp" android:layout_width="0dp"
app:layout_constraintBottom_toTopOf="#+id/bnvContactAgent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".ui.buy.BuyDetailActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="230dp"
tools:srcCompat="#tools:sample/backgrounds/scenic"
android:id="#+id/ivHouseDetail"
android:scaleType="centerCrop"
android:contentDescription="#string/house"
app:imageUrl="#{buy.propertyImage}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
app:formatToUSD="#{buy.price}"
android:id="#+id/tvPriceDetail"
android:textStyle="bold"
android:textColor="#color/colorPrimaryText"
android:textSize="20sp"
tools:ignore="HardcodedText"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/ivHouseDetail" android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent"/>
<TextView
android:text="#{buy.address}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvAddressDetail"
android:textColor="#color/colorSecondaryText"
app:layout_constraintTop_toBottomOf="#+id/tvPriceDetail"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"/>
<TextView
android:text="#{Integer.toString(buy.numberOfRoom)}"
android:maxLength="3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvBedroom"
android:layout_marginTop="12dp"
android:textStyle="bold"
android:textColor="#color/colorPrimaryText"
app:layout_constraintTop_toBottomOf="#+id/tvAddressDetail"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" app:srcCompat="#drawable/ic_bed"
android:id="#+id/ivBedroom"
android:contentDescription="#string/bedroom_icon"
app:layout_constraintStart_toEndOf="#+id/tvBedroom"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="#+id/tvAddressDetail"/>
<TextView
android:text="#{Integer.toString(buy.numberOfBath)}"
android:maxLength="3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvBathroom"
android:textStyle="bold"
android:textColor="#color/colorPrimaryText"
app:layout_constraintStart_toEndOf="#+id/ivBedroom"
android:layout_marginStart="20dp" app:layout_constraintTop_toTopOf="#+id/ivBedroom"
android:layout_marginTop="4dp"/>
<ImageView
android:layout_width="24dp"
android:layout_height="24dp" app:srcCompat="#drawable/ic_bathtub"
android:id="#+id/imageView2"
android:contentDescription="#string/bathroom_icon"
app:layout_constraintStart_toEndOf="#+id/tvBathroom"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="#+id/tvAddressDetail"/>
<TextView
android:text="#{Integer.toString(buy.numberOfCar)}"
android:maxLength="3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvGarage"
android:textStyle="bold"
android:textColor="#color/colorPrimaryText"
app:layout_constraintStart_toEndOf="#+id/imageView2"
android:layout_marginStart="20dp" app:layout_constraintTop_toTopOf="#+id/imageView2"
android:layout_marginTop="4dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" app:srcCompat="#drawable/ic_garage"
android:id="#+id/imageView3"
android:contentDescription="#string/garage_icon"
app:layout_constraintStart_toEndOf="#+id/tvGarage"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="#+id/tvAddressDetail"/>
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/listDivider"
tools:layout_editor_absoluteY="281dp" tools:layout_editor_absoluteX="8dp"
app:layout_constraintBottom_toTopOf="#+id/tvDescTitle"
android:layout_marginBottom="4dp"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvDescTitle"
android:text="#string/description"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#color/colorSecondaryText"
app:layout_constraintTop_toBottomOf="#+id/imageView3"
app:layout_constraintBottom_toTopOf="#id/expandableTextView"
app:layout_constraintStart_toStartOf="parent"/>
<at.blogc.android.views.ExpandableTextView
android:text="#{buy.propertyDesc}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/expandableTextView"
android:textColor="#color/colorSecondaryText"
android:maxLines="5"
android:ellipsize="end"
app:animation_duration="750"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintTop_toBottomOf="#+id/tvDescTitle"
app:layout_constraintEnd_toEndOf="parent"/>
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonToggle"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:text="#string/more"
app:goneUnless="#{true}"
app:layout_constraintTop_toBottomOf="#+id/expandableTextView"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp"/>
<TextView
app:photoCounter="#{buy.propertyImage}"
android:background="#99000000"
android:elevation="4dp"
android:padding="4dp"
android:textColor="#ffafffff"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/ivHouseDetail"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" tools:layout_editor_absoluteY="189dp"
tools:layout_editor_absoluteX="323dp" android:id="#+id/tvPhotoCounter"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
This code is my repository class, where I use the coroutines.
class BuyRepository (private val buyDao: BuyDao, private val buyRemote: BuyRemote) {
private val job = SupervisorJob()
private val scope = CoroutineScope(Dispatchers.Default + job)
companion object {
//For singleton instantiation
#Volatile private var instance: BuyRepository? = null
fun getInstance(buyDao: BuyDao, buyRemote: BuyRemote) =
instance ?: synchronized(this) {
instance ?: BuyRepository(buyDao, buyRemote)
.also { instance = it}
}
}
suspend fun getBuys(){
refresh()
}
private suspend fun refresh(){
val list = scope.async {buyRemote.loadBuys()}
list.await().forEach { buy -> insert(buy) }
}
//#WorkerThread
private fun insert(buy: Buy) {
buyDao.insertBuy(buy)
}
}
// toggle the ExpandableTextView
buttonToggle.setOnClickListener { // <- Set listener here
buttonToggle.setText(if (expandableTextView.isExpanded) com.example.drake.kunuk.R.string.more else com.example.drake.kunuk.R.string.less)
expandableTextView.toggle()
}
// but, you can also do the checks yourself
buttonToggle.setOnClickListener { // <- And overwritten here - seems bad
if (expandableTextView.isExpanded) {
expandableTextView.collapse()
buttonToggle.setText(com.example.drake.kunuk.R.string.more)
} else {
expandableTextView.expand()
buttonToggle.setText(com.example.drake.kunuk.R.string.less)
}
}
Looks to me like you're overwriting the click listener. So ... don't do that :)
I'm facing a problem using a Samsung J1 device here.
But the image taken is bigger than the preview one.
This is the preview that I got:
and this is the final bitmap result:
I'm calling camera view with this attributes:
<com.otaliastudios.cameraview.CameraView
android:id="#+id/camera"
app:cameraGesturePinch="zoom"
app:cameraFlash="auto"
app:cameraAutoFocusResetDelay="0"
app:cameraGestureTap="focusWithMarker"
android:keepScreenOn="true"
app:cameraPreview="glSurface"
app:cameraPictureSizeSmallest="true"
android:adjustViewBounds="true"
app:cameraGestureScrollHorizontal="exposureCorrection"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
I already tried to use cameraPictureSizeAspectRatio with 4:3 but no success with it.
Did I do something wrong?
i have same problem .
after save image from cameraview ( i use camerakit lib) , display file by convert it to bitmap in imageview .
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
cameraKitView = findViewById(R.id.camera)
val captureButton = findViewById<View>(R.id.button_capture) as Button
val imageView = findViewById<ImageView>(R.id.myImageView)
captureButton.setOnClickListener {
cameraKitView!!.captureImage { _, capturedImage ->
val savedPhoto = getOutputMediaFile()
try {
val outputStream = FileOutputStream(savedPhoto!!.path)
outputStream.write(capturedImage)
outputStream.close()
val myBitmap = BitmapFactory.decodeFile(savedPhoto.getAbsolutePath())
imageView.setImageBitmap(myBitmap)
} catch (e: java.io.IOException) {
e.printStackTrace()
}
}
}
}
and this is my layout :
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.camerakit.CameraKitView
android:id="#+id/camera"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:adjustViewBounds="true"/>
<Button
android:id="#+id/button_capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="#string/save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="#+id/myImageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:alpha="0.5"
android:adjustViewBounds="true"
android:background="#android:color/holo_blue_bright"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:contentDescription="#string/app_name" />
</android.support.constraint.ConstraintLayout>
but when show image and want to take another image, cameraview is not as scale as imageview
Solved :
just change scaleType to CentreCrop . that is it