-2

I have uploaded an image from my app to server and displayed that image in imageview. An error occurs when I want to re-upload the existing image in imageview to server. Shows this error:

ava.lang.NullPointerException: Attempt to get length of null array
                                                                           at com.billionusers.tingting.activities.MyPictureActivity$3.onClick(MyPictureActivity.java:145)
                                                                           at android.view.View.performClick(View.java:5690)
                                                                           at android.view.View$PerformClick.run(View.java:22693)
                                                                           at android.os.Handler.handleCallback(Handler.java:836)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:103)
                                                                           at android.os.Looper.loop(Looper.java:203)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6269)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)

My code so far:

uploadTV.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Drawable avatar_drawable = new BitmapDrawable(getResources(), decBitmap);

            if (userImage.getDrawable() != null){ //I'm trying to check if imageview has image or not
                Intent editProfileIntent = null;

                if (userImage.getDrawable() == avatar_drawable || imgBytes.length == 0) { // error occurs here. I'm trying to check if imageview's image is same as retrieved image

                    Bitmap imageViewBitmap = ((BitmapDrawable) userImage.getDrawable()).getBitmap();

                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    imageViewBitmap.compress(Bitmap.CompressFormat.PNG, 10, baos);
                    imgBytes = baos.toByteArray();

                    upLoadImageBytes(imgBytes);
                    editProfileIntent = new Intent(MyPictureActivity.this, EditProfileActivity.class);
                    startActivity(editProfileIntent);

                }

            } else {
                Snackbar.make(findViewById(android.R.id.content), "Select an Image From Camera or Gallery", Snackbar.LENGTH_LONG).show();
            }
        }
    });

Method to upload image via retrofit, works well:

private void upLoadImageBytes(byte[] imgBytes) {

    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request originalReq = chain.request();

            Request request = originalReq.newBuilder()
                    .addHeader("x-access-token", key_token)
                    .method(originalReq.method(), originalReq.body())
                    .build();

            return chain.proceed(request);
        }
    });

    OkHttpClient client = builder.build();
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Constants.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(client)
            .build();

    ImageService anInterface = retrofit.create(ImageService.class);

    RequestBody imageFileBody = RequestBody.create(MediaType.parse("image/*"), imgBytes);

    MultipartBody.Part partBody = MultipartBody.Part.createFormData("avatar", "avatar.jpg", imageFileBody);

    Call<ImageResponse> imageCall = anInterface.upLoadImage(partBody);
    imageCall.enqueue(new Callback<ImageResponse>() {
        @Override
        public void onResponse(Call<ImageResponse> call, retrofit2.Response<ImageResponse> response) {
            Log.d(TAG, "Response body is:\t" + response.body().toString());
        }

        @Override
        public void onFailure(Call<ImageResponse> call, Throwable t) {
            Log.d(TAG, "Response error message is:\t" + t.getMessage().toString());
        }
    });

}

PS: What I'm trying to do is to re-upload the image contained in my imageview when the button is clicked. Can someone say what's wrong pls? Thanks

Error404
  • 13
  • 3

1 Answers1

0

your 'imgBytes' array is null, just like the exception says, you can't check the length of a null array.

Try this:

if (userImage.getDrawable() == avatar_drawable || imgBytes == null || imgBytes.length == 0) {
   ....
}
marmor
  • 27,641
  • 11
  • 107
  • 150