Skip to content

Commit d598d1d

Browse files
authored
MainActivity.kt の更新
1 parent a30e608 commit d598d1d

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

app/src/main/java/com/rabimi/javaskinchanger/MainActivity.kt

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ class MainActivity : Activity() {
5656
setupUI()
5757
checkLogin()
5858

59-
// ☆ 初期スキンを steve.png に強制
59+
// ★ 初期スキンは res/raw/steve.png
6060
loadDefaultSteveSkin()
6161
}
6262

63-
/** res/raw/steve.png のビットマップを読み込んで表示する */
63+
/** res/raw/steve.png を読み込む */
6464
private fun loadDefaultSteveSkin() {
6565
val input = resources.openRawResource(R.raw.steve)
6666
val bmp = BitmapFactory.decodeStream(input)
@@ -84,7 +84,10 @@ class MainActivity : Activity() {
8484
btnUpload.setOnClickListener { handleUpload() }
8585

8686
btnLibrary.setOnClickListener {
87-
AlertDialog.Builder(this).setMessage("未実装").setPositiveButton("OK", null).show()
87+
AlertDialog.Builder(this)
88+
.setMessage("未実装")
89+
.setPositiveButton("OK", null)
90+
.show()
8891
}
8992

9093
btnLogout.setOnClickListener {
@@ -112,7 +115,6 @@ class MainActivity : Activity() {
112115
startActivityForResult(Intent.createChooser(intent, "スキンを選択"), REQUEST_SKIN_PICK)
113116
}
114117

115-
/** 選択後即プレビュー */
116118
override fun onActivityResult(req: Int, res: Int, data: Intent?) {
117119
super.onActivityResult(req, res, data)
118120
if (req == REQUEST_SKIN_PICK && res == Activity.RESULT_OK) {
@@ -176,7 +178,7 @@ class MainActivity : Activity() {
176178
}
177179
}
178180

179-
/** アップロード本体 */
181+
/** Mojang API 正式対応のスキンアップロード */
180182
private suspend fun uploadSkin(
181183
token: String,
182184
bmp: Bitmap,
@@ -197,10 +199,12 @@ class MainActivity : Activity() {
197199

198200
val out = DataOutputStream(conn.outputStream)
199201

202+
// skinModel
200203
out.writeBytes("--$boundary\r\n")
201-
out.writeBytes("Content-Disposition: form-data; name=\"variant\"\r\n\r\n")
204+
out.writeBytes("Content-Disposition: form-data; name=\"skinModel\"\r\n\r\n")
202205
out.writeBytes("$model\r\n")
203206

207+
// file
204208
out.writeBytes("--$boundary\r\n")
205209
out.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"skin.png\"\r\n")
206210
out.writeBytes("Content-Type: image/png\r\n\r\n")
@@ -209,18 +213,22 @@ class MainActivity : Activity() {
209213
bmp.compress(Bitmap.CompressFormat.PNG, 100, pngBaos)
210214
val bytes = pngBaos.toByteArray()
211215

212-
val chunk = if (bytes.size >= 100) bytes.size / 100 else bytes.size
213-
if (chunk <= 0) {
214-
out.write(bytes)
216+
val chunk = (bytes.size / 100).coerceAtLeast(1)
217+
218+
var written = 0
219+
for (i in 0 until 100) {
220+
val start = i * chunk
221+
if (start >= bytes.size) break
222+
val end = ((i + 1) * chunk).coerceAtMost(bytes.size)
223+
out.write(bytes, start, end - start)
224+
written = end
225+
onProgress(i + 1)
226+
}
227+
228+
// 残り書き込み
229+
if (written < bytes.size) {
230+
out.write(bytes, written, bytes.size - written)
215231
onProgress(100)
216-
} else {
217-
for (i in 0 until 100) {
218-
val start = i * chunk
219-
val end = if (i == 99) bytes.size else (i + 1) * chunk
220-
if (start >= bytes.size) break
221-
out.write(bytes, start, end - start)
222-
onProgress(i + 1)
223-
}
224232
}
225233

226234
out.writeBytes("\r\n--$boundary--\r\n")

0 commit comments

Comments
 (0)