gpt4 book ai didi

php - kotlin 通过改造上传图片

转载 作者:行者123 更新时间:2023-12-05 00:05:21 25 4
gpt4 key购买 nike

我想从图库中选择一张图片并通过改造将其上传到服务器。但是当我上传图片时返回的答案不成功,原因是:(没有那个文件或目录)我认为我的服务器代码也有问题,请检查并帮助我这是我的代码:

我的 api 服务:

@Multipart
@POST("upload.php")
fun uploadImage(@Part filePart:MultipartBody.Part):Call<Message>

fragment 代码:

    image.setOnClickListener {
var intent = Intent(Intent.ACTION_PICK)
intent.type = "image/*"
startActivityForResult(intent, 1001)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 1001 && resultCode == Activity.RESULT_OK) {
image.setImageURI(data!!.data)
var file = File(data!!.data!!.path)
var requestBody = RequestBody.create(MediaType.parse("image/jpeg"), file)
var filePart = MultipartBody.Part.createFormData("upload_file", file.name, requestBody)
viewModel.uploadImage(filePart).observe(this, Observer {
Toast.makeText(context, it.message, Toast.LENGTH_LONG).show()
})

}
}

和服务器代码:

define ('SITE_ROOT', realpath(dirname(__FILE__)));
//if(isset($_FILES['upload_file'])) {

$target_dir = "/uploads/";
$target_file = $target_dir . microtime().rand(99999,999999).basename($_FILES["upload_file"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["upload_file"]["tmp_name"]);
if($check !== false) {
$uploadOk = 1;
} else {
echo '{"status":"error","message":"فایل معتبر نیست"}';
$uploadOk = 0;
}
}


// Check file size
if ($_FILES["upload_file"]["size"] > 500000) {
echo '{"status":"error","message":"حجم عکس نباید بیشتر از 500 کیلوبایت باشد"}';
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
) {
echo '{"status":"error","message":"فرمت عکس اشتباه است"}';
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo '{"status":"error","message":"متاسفانه عکس آپلود نشد"}';
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["upload_file"]["tmp_name"], SITE_ROOT.$target_file)) {
echo '{"status":"success","message":"عکس با موفقیت آپلود شد"}';
} else {
echo '{"status":"error","message":"مشکل در آپلود فایل"}';
}
//}
}

最佳答案

几个月前我遇到过同样的问题。不确定,但较新版本的 Android 不会为您提供所选图像的路径。尝试将您选择的图像作为 ByteArray 发送,看看它是否适合您。这是您可以遵循的 fragment 。

将“image”替换为您作为表单数据传递的参数值。还让 filePartImage 为 MultipartBody.Part 类型。然后在您的 api 调用中将此 filePartImage var 作为参数发送。希望它对你有用!

if (requestCode == UPLOAD_IMAGE && resultCode == RESULT_OK) {
if (data != null) {

val bitmap = MediaStore.Images.Media.getBitmap(this.contentResolver, data.data)
product_iv.setImageBitmap(bitmap)

val imageType = contentResolver.getType(data.data!!)

val extension = imageType!!.substring(imageType.indexOf("/") + 1)

data.data!!.let {
application.contentResolver.openInputStream(it)?.use { inputStream ->
filePartImage = MultipartBody.Part.createFormData(
"image",
"image.$extension",
inputStream.readBytes().toRequestBody("*/*".toMediaType())
)
}
}
showMessage(this, "" + getString(R.string.image_uploaded))
} else {
showMessage(this, "" + getString(R.string.image_failed))
}
}

关于php - kotlin 通过改造上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59949509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com