';boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat,int,java.io.OutputStream)和#x27;关于空对象引用
我正在尝试使用Reformation2上载图像,但出现以下错误: java.lang.NullPointerException:尝试调用虚拟方法 '布尔 android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int,java.io.OutputStream)“”在空对象引用上 更新:我设法修复了错误,但我不确定如何将ImageView转换为位图并压缩它。我目前正在使用毕加索将图像加载到ImageView中 My image toString方法:';boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat,int,java.io.OutputStream)和#x27;关于空对象引用,java,android,android-studio,retrofit2,Java,Android,Android Studio,Retrofit2,我正在尝试使用Reformation2上载图像,但出现以下错误: java.lang.NullPointerException:尝试调用虚拟方法 '布尔 android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int,java.io.OutputStream)“”在空对象引用上 更新:我设法修复了错误,但我不确定如何将ImageView转换为位图并压缩它。我目前正在使用毕加索将图像加载到ImageView中
private String imagetoString() {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
byte[] imgByte = byteArrayOutputStream.toByteArray();
return Base64.encodeToString(imgByte,Base64.DEFAULT);
}
我的API接口:
public interface ApiUpdateProfile {
@FormUrlEncoded
@POST("update_profile.php")
Call<ProfileUpdate uploadImage(@Field("Photo_Location") String Photo_Location, @Field("Photo_Title") String Photo_Title);
}
我的图像上载方法:
private void uploadImage()
{
String Photo_Location = imagetoString();
String Photo_Title = fullname.toString();
ApiUpdateProfile apiUpdateProfile = ApiClient.getClient().create(ApiUpdateProfile.class);
Call<ProfileUpdate call = apiUpdateProfile.uploadImage(Photo_Title, Photo_Location);
call.enqueue(new Callback<ProfileUpdate>() {
@Override
public void onResponse(Call<ProfileUpdate call, Response<ProfileUpdate response) {
ProfileUpdate profileUpdate = response.body();
Toast.makeText(ProfileUpdateActivity.this, "Server response " + profileUpdate.getResponse(), Toast.LENGTH_LONG);
}
@Override
public void onFailure(Call<ProfileUpdate call, Throwable t) {
Toast.makeText(ProfileUpdateActivity.this, "error", Toast.LENGTH_LONG);
}
});
}
private void uploadImage()
{
字符串Photo_Location=imagetoString();
字符串Photo_Title=fullname.toString();
ApiUpdateProfile ApiUpdateProfile=ApiClient.getClient().create(ApiUpdateProfile.class);
调用您的imagetoString
中的bitmap
对象来自何处?我遵循本教程:它似乎与他一起工作。我不确定我缺少了什么。它是他的MainActivity
的一个类变量,可能您没有设置它(因此在您的情况下它仍然是NULL
).回到该系列的第5部分,检查你是否遗漏了什么。
private void uploadImage()
{
String Photo_Location = imagetoString();
String Photo_Title = fullname.toString();
ApiUpdateProfile apiUpdateProfile = ApiClient.getClient().create(ApiUpdateProfile.class);
Call<ProfileUpdate call = apiUpdateProfile.uploadImage(Photo_Title, Photo_Location);
call.enqueue(new Callback<ProfileUpdate>() {
@Override
public void onResponse(Call<ProfileUpdate call, Response<ProfileUpdate response) {
ProfileUpdate profileUpdate = response.body();
Toast.makeText(ProfileUpdateActivity.this, "Server response " + profileUpdate.getResponse(), Toast.LENGTH_LONG);
}
@Override
public void onFailure(Call<ProfileUpdate call, Throwable t) {
Toast.makeText(ProfileUpdateActivity.this, "error", Toast.LENGTH_LONG);
}
});
}
<?php
require "connection.php";
if($con){ $title = $_POST['Photo_Title']; $image =
$_POST['Photo_Location'];
$upload_path = "images/$title.jpg";
$sql = "INSERT into employee_profile(Photo_Location, Photo_Title)
values('$title', '$upload_path');";
if(mysqli_query($con, $sql)) { file_put_contents($upload_path,
base64_decode($image)); echo json_encode(array('response'=>"Image
uploaded")); } else{ echo json_encode(array('response'=>"Image
upload failed.")); }
mysqli_close($con); } ?>