Okhttp3, FileNotFoundException при модернизации POST

Это потому, что без f компилятор увидит это как double не float

0
задан alfalfa 19 January 2019 в 23:22
поделиться

1 ответ

Я сделал много тестов, поэтому я получил решение, как я покажу в коде ниже, но если вы знаете лучшее решение, покажите мне.

private fun postImage() {
    user_register_button.setOnClickListener {

        if (selectedImage != null) {
            var imageFile = File(baseContext.cacheDir, "${System.currentTimeMillis()}")
            imageFile.createNewFile()

            var arrayOutputStream = ByteArrayOutputStream()
            getBitmapFromUri(selectedImage).compress(Bitmap.CompressFormat.JPEG, 100, arrayOutputStream)
            var outputStream = FileOutputStream(imageFile)
            outputStream.write(arrayOutputStream.toByteArray())
            outputStream.flush()
            outputStream.close()


            var descriptionBody = RequestBody.create(MediaType.parse("image/*"), imageFile)
            val imagePart = MultipartBody.Part.createFormData("file", "${System.currentTimeMillis()}", descriptionBody)

            ImageRequest(getString(R.string.base_url_file_upload)).postImageFile(
                image = imagePart, delegate = object : Delegate<ResponseBody> {
                    override fun onSuccessFull(isSuccessFull: Boolean, response: ResponseBody?, message: String?) {
                        if (isSuccessFull) {
                            Log.i("ResponseBody", response.toString())
                        } else {
                            Log.i("ResponseError", message)
                        }
                    }

                    override fun onFailure(exception: Throwable) {
                        Log.i("Error", exception.message)
                        exception.printStackTrace()
                    }

                })

        }
    }
}

@Throws(IOException::class)
private fun getBitmapFromUri(uri: Uri): Bitmap {
    val parcelFileDescriptor = contentResolver.openFileDescriptor(uri, "r")
    val fileDescriptor = parcelFileDescriptor.fileDescriptor
    val image = BitmapFactory.decodeFileDescriptor(fileDescriptor)
    parcelFileDescriptor.close()
    return image
}
0
ответ дан Elias Meireles 19 January 2019 в 23:22
поделиться
Другие вопросы по тегам:

Похожие вопросы: