androidandroid-imageviewandroid-imageandroid-webservice

How to send Media files like whatsapp in android


Am developing an app that enables one to send images,audio and video through the internet. the person receiving this media files must have installed my app in his device.

What i can do right now is compress the image to be sent. i don't know where to start since most of the online tutorials are using intents to do this but i don't want to trigger another app to carry out the sending. my app should be able to do all this by itself.

this is how am compressing the image

public class ImageCompression extends AsyncTask<String, Void, String> {

private Context context;
private static final float maxHeight = 1280.0f;
private static final float maxWidth = 1280.0f;


public ImageCompression(Context context){
    this.context=context;
}

@Override
protected String doInBackground(String... strings) {
    if(strings.length == 0 || strings[0] == null)
        return null;

    return compressImage(strings[0]);
}

protected void onPostExecute(String imagePath){
    // imagePath is path of new compressed image.
}

Solution

  • It was a long time ago. I was new in android development. The problem is easily solved using firebase. Just uploading a file in firebase and sending a FCM notification which contains image uri.

    Uri file = Uri.fromFile(new File("path/to/images/rivers.jpg"));
    StorageReference riversRef = storageRef.child("images/"+file.getLastPathSegment());
    UploadTask uploadTask = riversRef.putFile(file);
    
    Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
        @Override
        public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
            if (!task.isSuccessful()) {
                throw task.getException();
            }
    
            // Continue with the task to get the download URL
            return ref.getDownloadUrl();
        }
    }).addOnCompleteListener(new OnCompleteListener<Uri>() {
        @Override
        public void onComplete(@NonNull Task<Uri> task) {
            if (task.isSuccessful()) {
                Uri downloadUri = task.getResult();
                int id =1;
    FirebaseMessaging fm = FirebaseMessaging.getInstance();
    fm.send(new RemoteMessage.Builder(SENDER_ID + "@gcm.googleapis.com")
      .setMessageId("1")
      .addData("my_message", downloadUri.toString())
      .addData("my_action","SAY_HELLO")
      .build());
    
            } else {
                // Handle failures
                // ...
            }
        }
    });