-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImageAdapter.kt
165 lines (138 loc) · 7.56 KB
/
ImageAdapter.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.CheckBox
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.example.securityapps.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File
@Suppress("NAME_SHADOWING")
class ImageAdapter(private val imagePathList: List<String>) :
RecyclerView.Adapter<ImageAdapter.ViewHolder>() {
private var checkedImages: MutableList<String>? = mutableListOf()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.image_item, parent, false)
return ViewHolder(view)
}
@SuppressLint("NotifyDataSetChanged")
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val imagePath = imagePathList[position]
holder.imageView.setImageURI(Uri.parse(imagePath))
holder.root_layout?.setOnClickListener {
holder.checkBox.isChecked = !holder.checkBox.isChecked
}
holder.checkBox.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
try {
if (checkedImages == null) {
checkedImages = mutableListOf()
}
checkedImages!!.add(imagePath)
if (checkedImages?.isNotEmpty()!!) {
val activity = holder.itemView.context as AppCompatActivity
val transferButton: Button = activity.findViewById(R.id.airdrop_button)
transferButton.visibility = View.VISIBLE
transferButton.setOnClickListener {
// Implement your logic to transfer the selected images here
CoroutineScope(Dispatchers.IO).launch {
try {
val checkedImages = getCheckedImages()
for (imagePath in checkedImages) {
val imageUri = Uri.parse(imagePath)
val imageStream = holder.itemView.context.contentResolver.openInputStream(imageUri)
if (imageStream != null) {
// Decode the bitmap from the input stream
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
BitmapFactory.decodeStream(imageStream, null, options)
imageStream.close()
val width = options.outWidth
val height = options.outHeight
// Calculate the sample size to scale down the bitmap to 250x250
var sampleSize = 1
while (width / (2 * sampleSize) >= 250 && height / (2 * sampleSize) >= 250) {
sampleSize *= 2
}
// Decode the bitmap again with the calculated sample size
val imageStream2 =
holder.itemView.context.contentResolver.openInputStream(imageUri)
val scaledBitmapOptions = BitmapFactory.Options()
scaledBitmapOptions.inSampleSize = sampleSize
val scaledBitmap =
BitmapFactory.decodeStream(imageStream2, null, scaledBitmapOptions)
// Create a new file with the scaled bitmap
val imageFile = File(holder.itemView.context.cacheDir, "${imageUri.lastPathSegment.toString()}.jpg")
imageFile.outputStream().use { out ->
scaledBitmap?.compress(Bitmap.CompressFormat.PNG, 100, out)
}
// Upload the file to the server
val requestBody = imageFile.asRequestBody("image/*".toMediaTypeOrNull())
val formData = MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("uploaded-file", imageFile.name, requestBody)
.build()
val request = Request.Builder()
.url("http://192.168.1.11:5000/upload_images")
.post(formData)
.build()
val client = OkHttpClient()
val response = client.newCall(request).execute()
if (!response.isSuccessful) {
Log.d("this", "Failed to upload file. HTTP error code: ${response.code}")
throw Exception("Failed to upload file. HTTP error code: ${response.code}")
} else {
Log.d("THIS", "File uploaded successfully")
response.close()
}
} else {
Log.d("THIS", "Failed to open image stream")
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}
override fun getItemCount(): Int {
return imagePathList.size
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun setOnClickListener(any: Any) {
}
val imageView: ImageView = itemView.findViewById(R.id.image_view)
val checkBox: CheckBox = itemView.findViewById(R.id.checkbox)
val root_layout: View? = itemView.findViewById(R.id.image_card_views)
}
fun getCheckedImages(): List<String> {
return checkedImages!!
}
fun clearCheckedImages() {
checkedImages?.clear()
}
fun isChecked(): Boolean {
return checkedImages?.isNotEmpty()!!
}
}