Android studio Volley发布base64图像错误[Volley:[232] NetworkUtility.shouldRetryException:“url”的意外响应代码400 ]

发布于 2025-01-09 13:12:41 字数 2453 浏览 0 评论 0原文

我想上传图像并将其编码为base64字符串,将其放入json数据中并使用volley进行发布。

使用下面的代码,我可以发布 PNG 图像,并且可以将其保存在服务器端,但是如果我上传 jpeg 或其他类型的图像,则会出现错误 [ E/Volley: [232] NetworkUtility.shouldRetryException: 意外的响应代码 400 ]并且无法上传图片。 232有时会变成231。

我不知道为什么我可以上传png,但不能上传其他类型的图像。

抱歉我的英语不好。我很感谢你的帮助。谢谢。

// MainActivity.kt
class MainActivity : AppCompatActivity() {

    private var imageData: ByteArray? = null

    companion object{
        private val IMAGE_PICK_CODE = 999
        private lateinit var imageView: ImageView
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recipe_create)

            // Upload Image
            upload.setOnClickListener {
                imageView = icon
                launchGallery()
            }

            submit.setOnClickListener {
                val url = "https://www.hamary.net/recipe/api/create"
                val postData = JSONObject()
                postData.put("imageData", Base64.encodeToString(imageData, Base64.NO_WRAP))

                val getUserInfoRequest = JsonObjectRequest(
                    Request.Method.POST,
                    url,
                    postData,
                    { response ->
                        Log.v("response", response.toString())
                    },
                    { error ->
                        Log.e("volley_error", error.toString())
                    }
                )
                MySingleton.getInstance(this).addToRequestQueue(getUserInfoRequest)
            }
        }
    }

    private fun launchGallery() {
        val intent = Intent(Intent.ACTION_PICK)
        intent.type = "image/*"
        startActivityForResult(intent, IMAGE_PICK_CODE)
    }

    @Throws(IOException::class)
    private fun createImageData(uri: Uri)
    {
        val inputStream = contentResolver.openInputStream(uri)
        inputStream?.buffered()?.use{
            imageData = it.readBytes()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if(resultCode == Activity.RESULT_OK && requestCode == IMAGE_PICK_CODE)
        {
            val uri = data?.data
            if(uri != null){
                imageView.setImageURI(uri)
                createImageData(uri)
            }
        }
        super.onActivityResult(requestCode, resultCode, data)
    }
}

I want to upload image and encode it to base64 string, put it in the json data and post using volley.

Using code below, I can post PNG image and I can save it at server side, but if I upload jpeg or other type of image, I have error [ E/Volley: [232] NetworkUtility.shouldRetryException: Unexpected response code 400 for ] and can't upload image. 232 sometimes become 231.

I have no idea why I can upload png, but can't upload other type of image.

Sorry my english is not good. I'm appreciate for your help. Thank you.

// MainActivity.kt
class MainActivity : AppCompatActivity() {

    private var imageData: ByteArray? = null

    companion object{
        private val IMAGE_PICK_CODE = 999
        private lateinit var imageView: ImageView
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recipe_create)

            // Upload Image
            upload.setOnClickListener {
                imageView = icon
                launchGallery()
            }

            submit.setOnClickListener {
                val url = "https://www.hamary.net/recipe/api/create"
                val postData = JSONObject()
                postData.put("imageData", Base64.encodeToString(imageData, Base64.NO_WRAP))

                val getUserInfoRequest = JsonObjectRequest(
                    Request.Method.POST,
                    url,
                    postData,
                    { response ->
                        Log.v("response", response.toString())
                    },
                    { error ->
                        Log.e("volley_error", error.toString())
                    }
                )
                MySingleton.getInstance(this).addToRequestQueue(getUserInfoRequest)
            }
        }
    }

    private fun launchGallery() {
        val intent = Intent(Intent.ACTION_PICK)
        intent.type = "image/*"
        startActivityForResult(intent, IMAGE_PICK_CODE)
    }

    @Throws(IOException::class)
    private fun createImageData(uri: Uri)
    {
        val inputStream = contentResolver.openInputStream(uri)
        inputStream?.buffered()?.use{
            imageData = it.readBytes()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if(resultCode == Activity.RESULT_OK && requestCode == IMAGE_PICK_CODE)
        {
            val uri = data?.data
            if(uri != null){
                imageView.setImageURI(uri)
                createImageData(uri)
            }
        }
        super.onActivityResult(requestCode, resultCode, data)
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏末染殇 2025-01-16 13:12:41

我认为您因无效的 base64 而面临此错误,
尝试用这种方式转换为base64,然后

在onActivityResult内的

Uri uri = data.getData();
toBase64(uri)

请求中发送它,将图像uri发送到此函数,此函数将您的图像转换为base64,无论您从图库中选择哪种类型的图像


 public String toBase64(Uri contentURI) {
 Bitmap bitmap=MediaStore.Images.Media.getBitmap(getContentResolver(), contentURI);
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, 3508, 2480, false);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        resizedBitmap.compress(Bitmap.CompressFormat.PNG, 90, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        return Base64.encodeToString(byteArray, Base64.DEFAULT);
    }

I think you face this error because of invalid base64 ,
try this way to convert in base64 and then send it in request

inside the onActivityResult

Uri uri = data.getData();
toBase64(uri)

send Image uri to this function , this function convert your image to base64 , dose not matter which type of image you select from gallery


 public String toBase64(Uri contentURI) {
 Bitmap bitmap=MediaStore.Images.Media.getBitmap(getContentResolver(), contentURI);
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, 3508, 2480, false);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        resizedBitmap.compress(Bitmap.CompressFormat.PNG, 90, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        return Base64.encodeToString(byteArray, Base64.DEFAULT);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文