Android studio Volley发布base64图像错误[Volley:[232] NetworkUtility.shouldRetryException:“url”的意外响应代码400 ]
我想上传图像并将其编码为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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您因无效的 base64 而面临此错误,
尝试用这种方式转换为base64,然后
在onActivityResult内的
请求中发送它,将图像uri发送到此函数,此函数将您的图像转换为base64,无论您从图库中选择哪种类型的图像
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
send Image uri to this function , this function convert your image to base64 , dose not matter which type of image you select from gallery