0

The following code solves OOM errors by loading big images to imageview by ensuring the application doesn't exceed the memory limit of the app and mobile. Here is a piece of code from my app. Unfortunately i am getting error E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException at line 285 bitmap1.compress(Bitmap.CompressFormat.JPEG, 80, baos); at line 320 final String imageView1 = getStringImage1(bitmap1); and at line 189ImageUploadToServerFunction();

Where could i be going wrong?. Thank you in advance.

  SelectImageGallery1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select Image1 From Gallery"), 1);

            }
        });

        SelectImageGallery2.setOnClickListener(new View.OnClickListener() {
              @Override
                public void onClick(View view) {
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent, "Select Image4 From Gallery"), 2);
                }
          });

        SelectImageGallery3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select Image3 From Gallery"), 3);

            }
        });

        SelectImageGallery4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select Image4 From Gallery"), 4);

            }
        });

        UploadImageServer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GetImageNameEditText1 = imageName1.getText().toString();
                GetImageNameEditText2 = imageName2.getText().toString();
                GetImageNameEditText3 = imageName3.getText().toString();
                GetImageNameEditText4 = imageName4.getText().toString();
               ImageUploadToServerFunction();
            }
        });

    }

    @Override
    protected void onActivityResult(int RC, int RQC, Intent I) {
        super.onActivityResult(RC, RQC, I);
        if (RC == 1 && RQC == RESULT_OK && I != null && I.getData() != null) {
            Uri uri = I.getData();
            RequestOptions options = new RequestOptions()
                    .format(DecodeFormat.PREFER_RGB_565)
                    .placeholder(R.drawable.ic_launcher_background)
                    .error(R.drawable.ic_launcher_background);

            Glide.with(this)
                    .setDefaultRequestOptions(options)
                    .load(uri)
                    .centerInside()
                    .into(new CustomTarget<Drawable>(512, 512) {
                        @Override
                        public void onResourceReady(@NonNull Drawable resource1, @Nullable Transition<? super Drawable> transition) {
                            imageView1.setImageDrawable(resource1);
                        }
                        @Override
                        public void onLoadCleared(@Nullable Drawable placeholder) {
                        }
                    });
        }
        if (RC == 2 && RQC == RESULT_OK && I != null && I.getData() != null) {
            Uri uri = I.getData();
            RequestOptions options = new RequestOptions()
                    .format(DecodeFormat.PREFER_RGB_565)
                    .placeholder(R.drawable.ic_launcher_background)
                    .error(R.drawable.ic_launcher_background);

            Glide.with(this)
                    .setDefaultRequestOptions(options)
                    .load(uri)
                    .centerInside()
                    .into(new CustomTarget<Drawable>(512, 512) {
                        @Override
                        public void onResourceReady(@NonNull Drawable resource2, @Nullable Transition<? super Drawable> transition) {
                            imageView2.setImageDrawable(resource2);
                        }
                        @Override
                        public void onLoadCleared(@Nullable Drawable placeholder) {
                        }
                    });
        }
        if (RC == 3 && RQC == RESULT_OK && I != null && I.getData() != null) {
            Uri uri = I.getData();
            RequestOptions options = new RequestOptions()
                    .format(DecodeFormat.PREFER_RGB_565)
                    .placeholder(R.drawable.ic_launcher_background)
                    .error(R.drawable.ic_launcher_background);

            Glide.with(this)
                    .setDefaultRequestOptions(options)
                    .load(uri)
                    .centerInside()
                    .into(new CustomTarget<Drawable>(512, 512) {
                        @Override
                        public void onResourceReady(@NonNull Drawable resource3, @Nullable Transition<? super Drawable> transition) {
                            imageView3.setImageDrawable(resource3);
                        }
                        @Override
                        public void onLoadCleared(@Nullable Drawable placeholder) {
                        }
                    });
        }
        if (RC == 4 && RQC == RESULT_OK && I != null && I.getData() != null) {
            Uri uri = I.getData();
            RequestOptions options = new RequestOptions()
                    .format(DecodeFormat.PREFER_RGB_565)
                    .placeholder(R.drawable.ic_launcher_background)
                    .error(R.drawable.ic_launcher_background);

            Glide.with(this)
                    .setDefaultRequestOptions(options)
                    .load(uri)
                    .centerInside()
                    .into(new CustomTarget<Drawable>(512, 512) {
                        @Override
                        public void onResourceReady(@NonNull Drawable resource4, @Nullable Transition<? super Drawable> transition) {
                            imageView4.setImageDrawable(resource4);
                        }
                        @Override
                        public void onLoadCleared(@Nullable Drawable placeholder) {
                        }
                    });
        }
    }
    public String getStringImage1(Bitmap bitmap1){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap1.compress(Bitmap.CompressFormat.JPEG, 80, baos);
        byte[] imageBytes = baos.toByteArray();
        String encodedImage1 = Base64.encodeToString(imageBytes, Base64.DEFAULT);
        return encodedImage1;
    }

    public String getStringImage2(Bitmap bitmap2){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap2.compress(Bitmap.CompressFormat.JPEG, 80, baos);
        byte[] imageBytes = baos.toByteArray();
        String encodedImage2 = Base64.encodeToString(imageBytes, Base64.DEFAULT);
        return encodedImage2;
    }
    public String getStringImage3(Bitmap bitmap3){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap3.compress(Bitmap.CompressFormat.JPEG, 80, baos);
        byte[] imageBytes = baos.toByteArray();
        String encodedImage3 = Base64.encodeToString(imageBytes, Base64.DEFAULT);
        return encodedImage3;
    }

    public String getStringImage4(Bitmap bitmap4){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap4.compress(Bitmap.CompressFormat.JPEG, 80, baos);
        byte[] imageBytes = baos.toByteArray();
        String encodedImage4 = Base64.encodeToString(imageBytes, Base64.DEFAULT);
        return encodedImage4;
    }


    public void ImageUploadToServerFunction(){
        final String imageName1 = GetImageNameEditText1.trim();
        final String imageName2 = GetImageNameEditText2.trim();
        final String imageName3 = GetImageNameEditText3.trim();
        final String imageName4 = GetImageNameEditText4.trim();
        final String imageView1 = getStringImage1(bitmap1);
        final String imageView2 = getStringImage2(bitmap2);
        final String imageView3 = getStringImage3(bitmap3);
        final String imageView4 = getStringImage4(bitmap4);

NyP
  • 499
  • 4
  • 18
  • What is the `bitmap1` variable in the *ImageUploadToServerFunction* method? Maybe null? – davidm Jan 15 '20 at 19:59
  • ^ this . where do you initialize bitmap1 also in the `public String getStringImage` why so many just make 1 and pass the different bitmaps to it and get the string you want. the code is no different – mavriksc Jan 15 '20 at 20:03
  • thanks all. how do i initalize it in my code above? – NyP Jan 15 '20 at 20:36

0 Answers0