我正在尝试将图像位图转换为base64string,以将其发布到服务器,但无法正常工作。它加载了这么长时间然后失败了。
这是我的代码onActivityResult
@Override
public void onActivityResult(int requenstCode, int resultCode, Intent data) {
super.onActivityResult(requenstCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requenstCode == REQUEST_CAMERA) {
selectedImageBitmap = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(selectedImageBitmap);
} else if (requenstCode == SELECT_FILE) {
Uri selectedImageUri = data.getData();
try {
selectedImageBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImageUri);
} catch (IOException e) {
e.printStackTrace();
}
imageView.setImageBitmap(selectedImageBitmap);
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
selectedImageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] byteArrayImage = byteArrayOutputStream.toByteArray();
encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
}
}
请帮助如何使其工作
答案 0 :(得分:0)
请勿使用base64字符串上传图片。压缩图片并将图片上传到服务器上
这是一个简单的php脚本
::selection{
color: #f2f2f2;
background: #404040;
text-shadow: none;
}
::-moz-selection {
color: #f2f2f2;
background: #404040;
text-shadow: none;
}
为FileUpload创建界面
<?php
$target_dir = "img/";
$target_dir = $target_dir .basename($_FILES["file"]["name"]);
$response = array();
// Check if image file is a actual image or fake image
if (isset($_FILES["file"]))
{
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_dir))
{
$success = true;
$message = "Successfully Uploaded";
}
else
{
$success = false;
$message = "Error while uploading ". $target_dir;
}
}
else
{
$success = false;
$message = "Required Field Missing";
}
$response["success"] = $success;
$response["message"] = $message;
echo json_encode($response);
?>
在Android上,使用翻新将压缩的图像上传到服务器
public interface FileUploadService {
@Multipart
@POST("upload.php")
Call<ResponseBody> upload(
@Part MultipartBody.Part file
);
@GET("images/{imageName}")
Call<okhttp3.ResponseBody> getImage(@Path("imageName") String imageName);
}