Get Byte Array From Image in ImageView Android : <p>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:</p>

<pre><code>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)
</code></pre>

<p>My code so far:</p>

<pre><code>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();
            }
        }
    });
</code></pre>

<p>Method to upload image via retrofit, works well:</p>

<pre><code>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&lt;ImageResponse&gt; imageCall = anInterface.upLoadImage(partBody);
    imageCall.enqueue(new Callback&lt;ImageResponse&gt;() {
        @Override
        public void onResponse(Call&lt;ImageResponse&gt; call, retrofit2.Response&lt;ImageResponse&gt; response) {
            Log.d(TAG, "Response body is:\t" + response.body().toString());
        }

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

}
</code></pre>

<p>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</p>
