0

I'm using this method in a fragment to compress an image, if I'm not mistaken and then upload it to google firebase server:

Bitmap thumb_bitmap = new Compressor(this.getActivity())
                    .setMaxWidth(200)
                    .setMaxHeight(200)
                    .setQuality(75)
                    .compressToBitmap(thumb_filePath);

I end up getting the following error from Android Studio:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference

This is the rest of the code for the method:

private void uploadImage() {

        if (filePath[0] != null && filePath[1] != null) {

            final ProgressDialog progressDialog = new ProgressDialog(getActivity());
            //progressDialog.setTitle("Uploading...");
            //progressDialog.show();

            for ( Uri path : filePath ) {

                id_or_proof += 1;

                File thumb_filePath = new File(path.getPath());

                Log.d ( "THUMB FILE PATH", path.getPath() );

                String current_user_id = mCurrentUser.getUid();

                Bitmap thumb_bitmap = new Compressor(this.getActivity())
                        .setMaxWidth(200)
                        .setMaxHeight(200)
                        .setQuality(75)
                        .compressToBitmap(thumb_filePath);

                Log.d (  "BITMAP", String.valueOf(thumb_bitmap));

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                thumb_bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                final byte[] thumb_byte = baos.toByteArray();

                StorageReference filepath = mImageStorage.child("profile_images").child(current_user_id + ".jpg");
                final StorageReference thumb_filepath = mImageStorage.child("profile_images").child("thumbs").child(current_user_id + ".jpg");


                filepath.putFile(path).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {

                        if(task.isSuccessful()){

                            final String download_url = task.getResult().getDownloadUrl().toString();

                            UploadTask uploadTask = thumb_filepath.putBytes(thumb_byte);
                            uploadTask.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                                @Override
                                public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> thumb_task) {

                                    String thumb_downloadUrl = thumb_task.getResult().getDownloadUrl().toString();

                                    if(thumb_task.isSuccessful()){

                                        if ( id_or_proof == 1 ) {
                                            Map update_hashMap = new HashMap();
                                            update_hashMap.put("id_image", download_url);
                                            update_hashMap.put("thumb_id_image", thumb_downloadUrl);

                                            mUserDatabase.updateChildren(update_hashMap).addOnCompleteListener(new OnCompleteListener<Void>() {
                                                @Override
                                                public void onComplete(@NonNull Task<Void> task) {

                                                    if (task.isSuccessful()) {

                                                        mProgressDialog.dismiss();
                                                        Toast.makeText(getActivity(), "Success Uploading.", Toast.LENGTH_LONG).show();

                                                    }

                                                }
                                            });
                                        }


                                        if ( id_or_proof == 2 ) {
                                            Map update_hashMap = new HashMap();
                                            update_hashMap.put("proof_image", download_url);
                                            update_hashMap.put("thumb_proof_image", thumb_downloadUrl);

                                            mUserDatabase.updateChildren(update_hashMap).addOnCompleteListener(new OnCompleteListener<Void>() {
                                                @Override
                                                public void onComplete(@NonNull Task<Void> task) {

                                                    if (task.isSuccessful()) {

                                                        mProgressDialog.dismiss();
                                                        Toast.makeText(getActivity(), "Success Uploading.", Toast.LENGTH_LONG).show();

                                                    }

                                                }
                                            });
                                        }

                                    } else {

                                        Toast.makeText(getActivity(), "Error in uploading thumbnail.", Toast.LENGTH_LONG).show();
                                        mProgressDialog.dismiss();

                                    }


                                }
                            });



                        } else {

                            Toast.makeText(getActivity(), "Error in uploading.", Toast.LENGTH_LONG).show();
                            mProgressDialog.dismiss();

                        }

                    }
                });

            }// end of for

        }// end of if ()

    } // end of uploadImage()

I need help trying to figure out why I'm getting the error that I'm getting and also how I can fix it.

Much Appreciated.

Here's the logcat:

03-14 15:36:57.383 2780-2780/in.tvac.akshaye.lapitchat E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /document/60 (No such file or directory) 03-14 15:36:57.383 2780-2780/in.tvac.akshaye.lapitchat E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /document/60 (No such file or directory) 03-14 15:36:57.383 2780-2780/in.tvac.akshaye.lapitchat D/BITMAP: null 03-14 15:36:57.383 2780-2780/in.tvac.akshaye.lapitchat D/AndroidRuntime: Shutting down VM 03-14 15:36:57.384 2780-2780/in.tvac.akshaye.lapitchat E/AndroidRuntime: FATAL EXCEPTION: main Process: in.tvac.akshaye.lapitchat, PID: 2780 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference at in.tvac.akshaye.lapitchat.PersonalInforamtionFragment.uploadImage(PersonalInforamtionFragment.java:649) at in.tvac.akshaye.lapitchat.PersonalInforamtionFragment.access$1900(PersonalInforamtionFragment.java:57) at in.tvac.akshaye.lapitchat.PersonalInforamtionFragment$8.onComplete(PersonalInforamtionFragment.java:767) at com.google.android.gms.tasks.zzc$1.run(Unknown Source:23) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

V. Doe
  • 56
  • 7

2 Answers2

2

It seems you are passing the wrong Uri to the new File, so it is not finding your file.

Get from External Storage.

If you are trying to get the file from the External Storage you have to add this permission:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Then create the path like this:

String filePath = Environment.getExternalStorageDirectory() + "/document/60";
Uri path = Uri.parse(filePath);

Get from app storage

If you are trying to get the file from App Storage, just build your Uri like this:

String filePath = getActivity().getFilesDir() + "/document/60";
Uri path = Uri.parse(filePath);
0

Use correct context

Bitmap thumb_bitmap = new Compressor(this.getActivity())
                        .setMaxWidth(200)
                        .setMaxHeight(200)
                        .setQuality(75)
                        .compressToBitmap(thumb_filePath);

Replace it with this

Bitmap thumb_bitmap = new Compressor(getActivity())
                            .setMaxWidth(200)
                            .setMaxHeight(200)
                            .setQuality(75)
                            .compressToBitmap(thumb_filePath);

Also the path should be correct as well

Quick learner
  • 10,632
  • 4
  • 45
  • 55