-3
new Runnable() {
           public void run() {
               try {
                   //Bitmap bitmap = Picasso.with(getActivity()).load(Uri.parse(mNewMember.getMemberImageUri())).resize(300, 300).get();
                   Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), Uri.parse(mNewMember.getMemberImageUri()));
                   ByteArrayOutputStream stream = new ByteArrayOutputStream();
                   bitmap.compress(Bitmap.CompressFormat.JPEG, 10, stream);
                   byte[] imageData = stream.toByteArray();
                   FirebaseStorage.getInstance().getReference()
                           .child("member images\\")
                           .child(mNewMember.getMemberId() + ".jpg").putBytes(imageData);
               }catch (IOException e){Log.d("db","machhar");}
       }
       }.run();

So currently it is uploading an image to firebase storeage but if I uncomment the commented line and comment the line below it, then the image does not get uploaded and no error gets generated i also tried attaching failure listener , it also does not get called. While both the lines returns a bitmap then why the first line(commented) does not work , and second line works. Also if I use the first line I have to run it one a background thread otherwise it produces error.

Please explain

Abhinav Chauhan
  • 1,304
  • 1
  • 7
  • 24
  • 1st line replace with this line **`Bitmap bitmap = Picasso.with(context).load(url).get()`** and try. – Ali Mar 16 '20 at 08:07
  • 1
    `Runnable#.run()` will run on same thread not on separate thread . Create a new thread and then call `thread.start()`.. – ADM Mar 16 '20 at 08:09
  • ADM you're right but before wrapping it inside the run method , there was an error now it there is no error "saying that i need to do it on separate thread, can you please explain why is that even when the thread is same – Abhinav Chauhan Mar 16 '20 at 08:12
  • @Ali i want to resize because i don't want to put big image in firebase storage – Abhinav Chauhan Mar 16 '20 at 08:17

1 Answers1

1

The commented line is Asynchronous and so the rest of your code will continue to execute immediately instead of waiting for it to complete.

You can use picasso with a completion callback in order to wait for the result.

Take a look at this answer

Ivan Wooll
  • 4,145
  • 3
  • 23
  • 34
  • your answer is very helpful can you also explain why that line needs a separate thread to run , so before i wrap it inside run() method there was an error basically saying that "i need to run it on separate thread" , but ADM commented that it is same thread then why now the error is not there. – Abhinav Chauhan Mar 16 '20 at 08:16
  • aslo please tell me which line should i use , i am asking this because the method MediaStore.Images.Media.getBitmap() is deprecated – Abhinav Chauhan Mar 16 '20 at 08:18
  • also please tell me how to use that listener , if i use the first line – Abhinav Chauhan Mar 16 '20 at 08:21