'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap $ CompressFormat,int,java.io.OutputStream)'对空对象引用

时间:2018-04-23 00:31:41

标签: java android android-studio retrofit2

我正在尝试使用Retrofit2上传图片,但我收到以下错误:

  

java.lang.NullPointerException:尝试调用虚方法   “布尔   android.graphics.Bitmap.compress(android.graphics.Bitmap $ CompressFormat,   int,java.io.OutputStream)'在空对象引用

更新:我设法修复错误,但我不确定如何将ImageView转换为Bitmap并压缩它。我目前正在使用Picasso将图像加载到ImageView中。

我的图像toString方法:

     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);
}

我的API客户端:

        public class ApiClient {

             public static final String BASE_URL = ("http://10.5.94.217/lara/");
             private static Retrofit retrofit = null;


             public static Retrofit getClient() {
                 if (retrofit==null) {
                     retrofit = new Retrofit.Builder()
                             .baseUrl(BASE_URL)
                             .addConverterFactory(GsonConverterFactory.create())
                             .build();
                 }
                 return retrofit;
             }
         }

我的图片上传方法:

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文件:

 <?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); }   ?>

任何帮助都将受到高度赞赏。

0 个答案:

没有答案