taskSnapshot.getDownloadUrl() is deprecated
Until now, the way to get the url from file on Storage in Firebase, I used to do this
taskSnapshot.getDownloadUrl
, but nowadays is deprecated, which method I should use?
android firebase firebase-storage
add a comment |
Until now, the way to get the url from file on Storage in Firebase, I used to do this
taskSnapshot.getDownloadUrl
, but nowadays is deprecated, which method I should use?
android firebase firebase-storage
1
firebase.google.com/docs/reference/android/com/google/firebase/…
– Nilesh Rathod
May 22 '18 at 12:38
add a comment |
Until now, the way to get the url from file on Storage in Firebase, I used to do this
taskSnapshot.getDownloadUrl
, but nowadays is deprecated, which method I should use?
android firebase firebase-storage
Until now, the way to get the url from file on Storage in Firebase, I used to do this
taskSnapshot.getDownloadUrl
, but nowadays is deprecated, which method I should use?
android firebase firebase-storage
android firebase firebase-storage
edited May 22 '18 at 20:15
Liliana J
asked May 22 '18 at 12:32
Liliana JLiliana J
158211
158211
1
firebase.google.com/docs/reference/android/com/google/firebase/…
– Nilesh Rathod
May 22 '18 at 12:38
add a comment |
1
firebase.google.com/docs/reference/android/com/google/firebase/…
– Nilesh Rathod
May 22 '18 at 12:38
1
1
firebase.google.com/docs/reference/android/com/google/firebase/…
– Nilesh Rathod
May 22 '18 at 12:38
firebase.google.com/docs/reference/android/com/google/firebase/…
– Nilesh Rathod
May 22 '18 at 12:38
add a comment |
7 Answers
7
active
oldest
votes
As Doug says, you will need to run it inside a Task
Here is a hint of how you need to implement it
final StorageReference ref = storageRef.child("your_REF");
uploadTask = ref.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();
String downloadURL = downloadUri.toString();
} else {
// Handle failures
// ...
}
}
});
For more information about how to implement it, you can check this github question that was opened 7 days after I answered this https://github.com/udacity/and-nd-firebase/issues/41
It works, thanks so much!!
– Liliana J
May 22 '18 at 20:12
Glad it worked happy coding
– Gastón Saillén
May 22 '18 at 21:47
1
What is the type ofuploadTask
? I am stuck. The code does not execute for me.
– gamofe
May 29 '18 at 14:53
Except the storage reference has to be final. How to accomplish this without it being final?
– Robert Goodrick
Jul 1 '18 at 22:09
declare it globally
– Gastón Saillén
Jul 1 '18 at 22:10
|
show 3 more comments
You can use StorageReference.getDownloadUrl(). Please note that it returns a Task, so you will have to remember to treat it asynchronously like you do any other Task.
What about StorageReference.getDownloadUrl().toString()? It seems to work by returning the url
– Florian Walther
Jul 5 '18 at 17:47
@FlorianWalther It doesn't return a URL. Take a good look at the string - it doesn't even begin with "http". It returns an internalized string representation of the task object, which is entirely not helpful here.
– Doug Stevenson
Jul 5 '18 at 18:13
Weird because I tested it in my app and it worked as before
– Florian Walther
Jul 6 '18 at 9:51
You're right, sorry
– Florian Walther
Jul 6 '18 at 9:57
add a comment |
This code work for me.
You can try.
package br.com.amptec.firebaseapp;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import java.io.ByteArrayOutputStream;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
private DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
private FirebaseAuth auth = FirebaseAuth.getInstance();
private Button btnUpload;
private ImageView imgPhoto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnUpload = findViewById(R.id.btnUpload);
imgPhoto = findViewById(R.id.imgPhoto);
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imgPhoto.setDrawingCacheEnabled(true);
imgPhoto.buildDrawingCache();
Bitmap bitmap = imgPhoto.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte imageBytes = baos.toByteArray();
String fileName = UUID.randomUUID().toString();
StorageReference storageReference = FirebaseStorage.getInstance().getReference();
StorageReference images = storageReference.child("images");
StorageReference imageRef = images.child(fileName + ".jpeg");
UploadTask uploadTask = imageRef.putBytes(imageBytes);
uploadTask.addOnFailureListener(MainActivity.this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "Upload Error: " +
e.getMessage(), Toast.LENGTH_LONG).show();
}
}).addOnSuccessListener(MainActivity.this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//Uri url = taskSnapshot.getDownloadUrl();
Task<Uri> uri = taskSnapshot.getStorage().getDownloadUrl();
while(!uri.isComplete());
Uri url = uri.getResult();
Toast.makeText(MainActivity.this, "Upload Success, download URL " +
url.toString(), Toast.LENGTH_LONG).show();
Log.i("FBApp1 URL ", url.toString());
}
});
}
});
}
}
This is neither an acceptable question nor even article. pls if you have a problem mention it. otherwise, express the question as a question and answer procedure. for example, you ask something and you answer to your own question
– Mohamad Armoon
Dec 30 '18 at 3:44
Usually it's better to explain a solution instead of just posting some rows of anonymous code. You can read How do I write a good answer, and also Explaining entirely code-based answers
– Anh Pham
Dec 30 '18 at 4:04
add a comment |
//Create an instance of StorageReference first (here in this code snippet, it is storageRef)
StorageReference filepath = storageRef.child("images.jpg");
//If file exist in storage this works.
filepath.getDownloadUrl().addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
String downloadUrl = task.getResult().toString();
// downloadurl will be the resulted answer
}
});
add a comment |
Add the code below:
Task<Uri> downUrl=taskSnapshot.getMetadata().getReference().getDownloadUrl();
Log.i("url:",downUrl.getResult().toString());
it doesn't return a url
– JAMSHAID
Aug 3 '18 at 4:38
add a comment |
You wont get the download url of image now using
ImageUrl = taskSnapshot.getDownloadUrl().toString();
this method is deprecated.
Instead you can use the below method
uniqueId = UUID.randomUUID().toString();
ur_firebase_reference = storageReference.child("user_photos/" + uniqueId);
Uri file = Uri.fromFile(new File(mphotofile.getAbsolutePath()));
UploadTask uploadTask = ur_firebase_reference.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 ur_firebase_reference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
System.out.println("Upload " + downloadUri);
Toast.makeText(mActivity, "Successfully uploaded", Toast.LENGTH_SHORT).show();
if (downloadUri != null) {
String photoStringLink = downloadUri.toString(); //YOU WILL GET THE DOWNLOAD URL HERE !!!!
System.out.println("Upload " + photoStringLink);
}
} else {
// Handle failures
// ...
}
}
});
You can track the progress of uploading by adding Progress listeners,
uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
System.out.println("Upload is " + progress + "% done");
Toast.makeText(mContext, "Upload is " + progress + "% done", Toast.LENGTH_SHORT).show();
}
}).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
@Override
public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
System.out.println("Upload is paused");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Handle successful uploads on complete
// ...
}
});
add a comment |
Just use a Task instead of ref.putFile(uriImage)
.addOnSuccessListener(new OnSuccessListener()
Nowadays, firebase references suggest using Uploadtask objects
I've done it like this:
UploadTask uploadTask;
uploadTask = storageReferenceProfilePic.putFile(uriProfileImage );
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 storageReferenceProfilePic.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
progressBarImageUploading.setVisibility(View.GONE);
Uri downloadUri = task.getResult();
profileImageUrl = downloadUri.toString();
ins.setText(profileImageUrl);
} else {
// Handle failures
// ...
}
}
});
Notice these lines in the above code:
Uri downloadUri = task.getResult();
profileImageUrl = downloadUri.toString();
Now profileImageUrl contains something like "http://adressofimage" which is the url to acess the image
Now you're free to use the String profileImageUrl however you wish. For e.g., load the url into an ImageView using Glide or Fresco libraries.
add a comment |
protected by Community♦ Feb 21 at 16:38
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
As Doug says, you will need to run it inside a Task
Here is a hint of how you need to implement it
final StorageReference ref = storageRef.child("your_REF");
uploadTask = ref.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();
String downloadURL = downloadUri.toString();
} else {
// Handle failures
// ...
}
}
});
For more information about how to implement it, you can check this github question that was opened 7 days after I answered this https://github.com/udacity/and-nd-firebase/issues/41
It works, thanks so much!!
– Liliana J
May 22 '18 at 20:12
Glad it worked happy coding
– Gastón Saillén
May 22 '18 at 21:47
1
What is the type ofuploadTask
? I am stuck. The code does not execute for me.
– gamofe
May 29 '18 at 14:53
Except the storage reference has to be final. How to accomplish this without it being final?
– Robert Goodrick
Jul 1 '18 at 22:09
declare it globally
– Gastón Saillén
Jul 1 '18 at 22:10
|
show 3 more comments
As Doug says, you will need to run it inside a Task
Here is a hint of how you need to implement it
final StorageReference ref = storageRef.child("your_REF");
uploadTask = ref.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();
String downloadURL = downloadUri.toString();
} else {
// Handle failures
// ...
}
}
});
For more information about how to implement it, you can check this github question that was opened 7 days after I answered this https://github.com/udacity/and-nd-firebase/issues/41
It works, thanks so much!!
– Liliana J
May 22 '18 at 20:12
Glad it worked happy coding
– Gastón Saillén
May 22 '18 at 21:47
1
What is the type ofuploadTask
? I am stuck. The code does not execute for me.
– gamofe
May 29 '18 at 14:53
Except the storage reference has to be final. How to accomplish this without it being final?
– Robert Goodrick
Jul 1 '18 at 22:09
declare it globally
– Gastón Saillén
Jul 1 '18 at 22:10
|
show 3 more comments
As Doug says, you will need to run it inside a Task
Here is a hint of how you need to implement it
final StorageReference ref = storageRef.child("your_REF");
uploadTask = ref.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();
String downloadURL = downloadUri.toString();
} else {
// Handle failures
// ...
}
}
});
For more information about how to implement it, you can check this github question that was opened 7 days after I answered this https://github.com/udacity/and-nd-firebase/issues/41
As Doug says, you will need to run it inside a Task
Here is a hint of how you need to implement it
final StorageReference ref = storageRef.child("your_REF");
uploadTask = ref.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();
String downloadURL = downloadUri.toString();
} else {
// Handle failures
// ...
}
}
});
For more information about how to implement it, you can check this github question that was opened 7 days after I answered this https://github.com/udacity/and-nd-firebase/issues/41
edited Oct 31 '18 at 19:22
answered May 22 '18 at 13:11
Gastón SaillénGastón Saillén
3,67341233
3,67341233
It works, thanks so much!!
– Liliana J
May 22 '18 at 20:12
Glad it worked happy coding
– Gastón Saillén
May 22 '18 at 21:47
1
What is the type ofuploadTask
? I am stuck. The code does not execute for me.
– gamofe
May 29 '18 at 14:53
Except the storage reference has to be final. How to accomplish this without it being final?
– Robert Goodrick
Jul 1 '18 at 22:09
declare it globally
– Gastón Saillén
Jul 1 '18 at 22:10
|
show 3 more comments
It works, thanks so much!!
– Liliana J
May 22 '18 at 20:12
Glad it worked happy coding
– Gastón Saillén
May 22 '18 at 21:47
1
What is the type ofuploadTask
? I am stuck. The code does not execute for me.
– gamofe
May 29 '18 at 14:53
Except the storage reference has to be final. How to accomplish this without it being final?
– Robert Goodrick
Jul 1 '18 at 22:09
declare it globally
– Gastón Saillén
Jul 1 '18 at 22:10
It works, thanks so much!!
– Liliana J
May 22 '18 at 20:12
It works, thanks so much!!
– Liliana J
May 22 '18 at 20:12
Glad it worked happy coding
– Gastón Saillén
May 22 '18 at 21:47
Glad it worked happy coding
– Gastón Saillén
May 22 '18 at 21:47
1
1
What is the type of
uploadTask
? I am stuck. The code does not execute for me.– gamofe
May 29 '18 at 14:53
What is the type of
uploadTask
? I am stuck. The code does not execute for me.– gamofe
May 29 '18 at 14:53
Except the storage reference has to be final. How to accomplish this without it being final?
– Robert Goodrick
Jul 1 '18 at 22:09
Except the storage reference has to be final. How to accomplish this without it being final?
– Robert Goodrick
Jul 1 '18 at 22:09
declare it globally
– Gastón Saillén
Jul 1 '18 at 22:10
declare it globally
– Gastón Saillén
Jul 1 '18 at 22:10
|
show 3 more comments
You can use StorageReference.getDownloadUrl(). Please note that it returns a Task, so you will have to remember to treat it asynchronously like you do any other Task.
What about StorageReference.getDownloadUrl().toString()? It seems to work by returning the url
– Florian Walther
Jul 5 '18 at 17:47
@FlorianWalther It doesn't return a URL. Take a good look at the string - it doesn't even begin with "http". It returns an internalized string representation of the task object, which is entirely not helpful here.
– Doug Stevenson
Jul 5 '18 at 18:13
Weird because I tested it in my app and it worked as before
– Florian Walther
Jul 6 '18 at 9:51
You're right, sorry
– Florian Walther
Jul 6 '18 at 9:57
add a comment |
You can use StorageReference.getDownloadUrl(). Please note that it returns a Task, so you will have to remember to treat it asynchronously like you do any other Task.
What about StorageReference.getDownloadUrl().toString()? It seems to work by returning the url
– Florian Walther
Jul 5 '18 at 17:47
@FlorianWalther It doesn't return a URL. Take a good look at the string - it doesn't even begin with "http". It returns an internalized string representation of the task object, which is entirely not helpful here.
– Doug Stevenson
Jul 5 '18 at 18:13
Weird because I tested it in my app and it worked as before
– Florian Walther
Jul 6 '18 at 9:51
You're right, sorry
– Florian Walther
Jul 6 '18 at 9:57
add a comment |
You can use StorageReference.getDownloadUrl(). Please note that it returns a Task, so you will have to remember to treat it asynchronously like you do any other Task.
You can use StorageReference.getDownloadUrl(). Please note that it returns a Task, so you will have to remember to treat it asynchronously like you do any other Task.
answered May 22 '18 at 12:45
Doug StevensonDoug Stevenson
77.9k991111
77.9k991111
What about StorageReference.getDownloadUrl().toString()? It seems to work by returning the url
– Florian Walther
Jul 5 '18 at 17:47
@FlorianWalther It doesn't return a URL. Take a good look at the string - it doesn't even begin with "http". It returns an internalized string representation of the task object, which is entirely not helpful here.
– Doug Stevenson
Jul 5 '18 at 18:13
Weird because I tested it in my app and it worked as before
– Florian Walther
Jul 6 '18 at 9:51
You're right, sorry
– Florian Walther
Jul 6 '18 at 9:57
add a comment |
What about StorageReference.getDownloadUrl().toString()? It seems to work by returning the url
– Florian Walther
Jul 5 '18 at 17:47
@FlorianWalther It doesn't return a URL. Take a good look at the string - it doesn't even begin with "http". It returns an internalized string representation of the task object, which is entirely not helpful here.
– Doug Stevenson
Jul 5 '18 at 18:13
Weird because I tested it in my app and it worked as before
– Florian Walther
Jul 6 '18 at 9:51
You're right, sorry
– Florian Walther
Jul 6 '18 at 9:57
What about StorageReference.getDownloadUrl().toString()? It seems to work by returning the url
– Florian Walther
Jul 5 '18 at 17:47
What about StorageReference.getDownloadUrl().toString()? It seems to work by returning the url
– Florian Walther
Jul 5 '18 at 17:47
@FlorianWalther It doesn't return a URL. Take a good look at the string - it doesn't even begin with "http". It returns an internalized string representation of the task object, which is entirely not helpful here.
– Doug Stevenson
Jul 5 '18 at 18:13
@FlorianWalther It doesn't return a URL. Take a good look at the string - it doesn't even begin with "http". It returns an internalized string representation of the task object, which is entirely not helpful here.
– Doug Stevenson
Jul 5 '18 at 18:13
Weird because I tested it in my app and it worked as before
– Florian Walther
Jul 6 '18 at 9:51
Weird because I tested it in my app and it worked as before
– Florian Walther
Jul 6 '18 at 9:51
You're right, sorry
– Florian Walther
Jul 6 '18 at 9:57
You're right, sorry
– Florian Walther
Jul 6 '18 at 9:57
add a comment |
This code work for me.
You can try.
package br.com.amptec.firebaseapp;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import java.io.ByteArrayOutputStream;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
private DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
private FirebaseAuth auth = FirebaseAuth.getInstance();
private Button btnUpload;
private ImageView imgPhoto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnUpload = findViewById(R.id.btnUpload);
imgPhoto = findViewById(R.id.imgPhoto);
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imgPhoto.setDrawingCacheEnabled(true);
imgPhoto.buildDrawingCache();
Bitmap bitmap = imgPhoto.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte imageBytes = baos.toByteArray();
String fileName = UUID.randomUUID().toString();
StorageReference storageReference = FirebaseStorage.getInstance().getReference();
StorageReference images = storageReference.child("images");
StorageReference imageRef = images.child(fileName + ".jpeg");
UploadTask uploadTask = imageRef.putBytes(imageBytes);
uploadTask.addOnFailureListener(MainActivity.this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "Upload Error: " +
e.getMessage(), Toast.LENGTH_LONG).show();
}
}).addOnSuccessListener(MainActivity.this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//Uri url = taskSnapshot.getDownloadUrl();
Task<Uri> uri = taskSnapshot.getStorage().getDownloadUrl();
while(!uri.isComplete());
Uri url = uri.getResult();
Toast.makeText(MainActivity.this, "Upload Success, download URL " +
url.toString(), Toast.LENGTH_LONG).show();
Log.i("FBApp1 URL ", url.toString());
}
});
}
});
}
}
This is neither an acceptable question nor even article. pls if you have a problem mention it. otherwise, express the question as a question and answer procedure. for example, you ask something and you answer to your own question
– Mohamad Armoon
Dec 30 '18 at 3:44
Usually it's better to explain a solution instead of just posting some rows of anonymous code. You can read How do I write a good answer, and also Explaining entirely code-based answers
– Anh Pham
Dec 30 '18 at 4:04
add a comment |
This code work for me.
You can try.
package br.com.amptec.firebaseapp;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import java.io.ByteArrayOutputStream;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
private DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
private FirebaseAuth auth = FirebaseAuth.getInstance();
private Button btnUpload;
private ImageView imgPhoto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnUpload = findViewById(R.id.btnUpload);
imgPhoto = findViewById(R.id.imgPhoto);
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imgPhoto.setDrawingCacheEnabled(true);
imgPhoto.buildDrawingCache();
Bitmap bitmap = imgPhoto.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte imageBytes = baos.toByteArray();
String fileName = UUID.randomUUID().toString();
StorageReference storageReference = FirebaseStorage.getInstance().getReference();
StorageReference images = storageReference.child("images");
StorageReference imageRef = images.child(fileName + ".jpeg");
UploadTask uploadTask = imageRef.putBytes(imageBytes);
uploadTask.addOnFailureListener(MainActivity.this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "Upload Error: " +
e.getMessage(), Toast.LENGTH_LONG).show();
}
}).addOnSuccessListener(MainActivity.this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//Uri url = taskSnapshot.getDownloadUrl();
Task<Uri> uri = taskSnapshot.getStorage().getDownloadUrl();
while(!uri.isComplete());
Uri url = uri.getResult();
Toast.makeText(MainActivity.this, "Upload Success, download URL " +
url.toString(), Toast.LENGTH_LONG).show();
Log.i("FBApp1 URL ", url.toString());
}
});
}
});
}
}
This is neither an acceptable question nor even article. pls if you have a problem mention it. otherwise, express the question as a question and answer procedure. for example, you ask something and you answer to your own question
– Mohamad Armoon
Dec 30 '18 at 3:44
Usually it's better to explain a solution instead of just posting some rows of anonymous code. You can read How do I write a good answer, and also Explaining entirely code-based answers
– Anh Pham
Dec 30 '18 at 4:04
add a comment |
This code work for me.
You can try.
package br.com.amptec.firebaseapp;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import java.io.ByteArrayOutputStream;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
private DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
private FirebaseAuth auth = FirebaseAuth.getInstance();
private Button btnUpload;
private ImageView imgPhoto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnUpload = findViewById(R.id.btnUpload);
imgPhoto = findViewById(R.id.imgPhoto);
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imgPhoto.setDrawingCacheEnabled(true);
imgPhoto.buildDrawingCache();
Bitmap bitmap = imgPhoto.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte imageBytes = baos.toByteArray();
String fileName = UUID.randomUUID().toString();
StorageReference storageReference = FirebaseStorage.getInstance().getReference();
StorageReference images = storageReference.child("images");
StorageReference imageRef = images.child(fileName + ".jpeg");
UploadTask uploadTask = imageRef.putBytes(imageBytes);
uploadTask.addOnFailureListener(MainActivity.this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "Upload Error: " +
e.getMessage(), Toast.LENGTH_LONG).show();
}
}).addOnSuccessListener(MainActivity.this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//Uri url = taskSnapshot.getDownloadUrl();
Task<Uri> uri = taskSnapshot.getStorage().getDownloadUrl();
while(!uri.isComplete());
Uri url = uri.getResult();
Toast.makeText(MainActivity.this, "Upload Success, download URL " +
url.toString(), Toast.LENGTH_LONG).show();
Log.i("FBApp1 URL ", url.toString());
}
});
}
});
}
}
This code work for me.
You can try.
package br.com.amptec.firebaseapp;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import java.io.ByteArrayOutputStream;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
private DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
private FirebaseAuth auth = FirebaseAuth.getInstance();
private Button btnUpload;
private ImageView imgPhoto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnUpload = findViewById(R.id.btnUpload);
imgPhoto = findViewById(R.id.imgPhoto);
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imgPhoto.setDrawingCacheEnabled(true);
imgPhoto.buildDrawingCache();
Bitmap bitmap = imgPhoto.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte imageBytes = baos.toByteArray();
String fileName = UUID.randomUUID().toString();
StorageReference storageReference = FirebaseStorage.getInstance().getReference();
StorageReference images = storageReference.child("images");
StorageReference imageRef = images.child(fileName + ".jpeg");
UploadTask uploadTask = imageRef.putBytes(imageBytes);
uploadTask.addOnFailureListener(MainActivity.this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "Upload Error: " +
e.getMessage(), Toast.LENGTH_LONG).show();
}
}).addOnSuccessListener(MainActivity.this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//Uri url = taskSnapshot.getDownloadUrl();
Task<Uri> uri = taskSnapshot.getStorage().getDownloadUrl();
while(!uri.isComplete());
Uri url = uri.getResult();
Toast.makeText(MainActivity.this, "Upload Success, download URL " +
url.toString(), Toast.LENGTH_LONG).show();
Log.i("FBApp1 URL ", url.toString());
}
});
}
});
}
}
answered Dec 30 '18 at 3:06
Adriano PereiraAdriano Pereira
211
211
This is neither an acceptable question nor even article. pls if you have a problem mention it. otherwise, express the question as a question and answer procedure. for example, you ask something and you answer to your own question
– Mohamad Armoon
Dec 30 '18 at 3:44
Usually it's better to explain a solution instead of just posting some rows of anonymous code. You can read How do I write a good answer, and also Explaining entirely code-based answers
– Anh Pham
Dec 30 '18 at 4:04
add a comment |
This is neither an acceptable question nor even article. pls if you have a problem mention it. otherwise, express the question as a question and answer procedure. for example, you ask something and you answer to your own question
– Mohamad Armoon
Dec 30 '18 at 3:44
Usually it's better to explain a solution instead of just posting some rows of anonymous code. You can read How do I write a good answer, and also Explaining entirely code-based answers
– Anh Pham
Dec 30 '18 at 4:04
This is neither an acceptable question nor even article. pls if you have a problem mention it. otherwise, express the question as a question and answer procedure. for example, you ask something and you answer to your own question
– Mohamad Armoon
Dec 30 '18 at 3:44
This is neither an acceptable question nor even article. pls if you have a problem mention it. otherwise, express the question as a question and answer procedure. for example, you ask something and you answer to your own question
– Mohamad Armoon
Dec 30 '18 at 3:44
Usually it's better to explain a solution instead of just posting some rows of anonymous code. You can read How do I write a good answer, and also Explaining entirely code-based answers
– Anh Pham
Dec 30 '18 at 4:04
Usually it's better to explain a solution instead of just posting some rows of anonymous code. You can read How do I write a good answer, and also Explaining entirely code-based answers
– Anh Pham
Dec 30 '18 at 4:04
add a comment |
//Create an instance of StorageReference first (here in this code snippet, it is storageRef)
StorageReference filepath = storageRef.child("images.jpg");
//If file exist in storage this works.
filepath.getDownloadUrl().addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
String downloadUrl = task.getResult().toString();
// downloadurl will be the resulted answer
}
});
add a comment |
//Create an instance of StorageReference first (here in this code snippet, it is storageRef)
StorageReference filepath = storageRef.child("images.jpg");
//If file exist in storage this works.
filepath.getDownloadUrl().addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
String downloadUrl = task.getResult().toString();
// downloadurl will be the resulted answer
}
});
add a comment |
//Create an instance of StorageReference first (here in this code snippet, it is storageRef)
StorageReference filepath = storageRef.child("images.jpg");
//If file exist in storage this works.
filepath.getDownloadUrl().addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
String downloadUrl = task.getResult().toString();
// downloadurl will be the resulted answer
}
});
//Create an instance of StorageReference first (here in this code snippet, it is storageRef)
StorageReference filepath = storageRef.child("images.jpg");
//If file exist in storage this works.
filepath.getDownloadUrl().addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
String downloadUrl = task.getResult().toString();
// downloadurl will be the resulted answer
}
});
answered Aug 10 '18 at 7:07
AnanthuAnanthu
1425
1425
add a comment |
add a comment |
Add the code below:
Task<Uri> downUrl=taskSnapshot.getMetadata().getReference().getDownloadUrl();
Log.i("url:",downUrl.getResult().toString());
it doesn't return a url
– JAMSHAID
Aug 3 '18 at 4:38
add a comment |
Add the code below:
Task<Uri> downUrl=taskSnapshot.getMetadata().getReference().getDownloadUrl();
Log.i("url:",downUrl.getResult().toString());
it doesn't return a url
– JAMSHAID
Aug 3 '18 at 4:38
add a comment |
Add the code below:
Task<Uri> downUrl=taskSnapshot.getMetadata().getReference().getDownloadUrl();
Log.i("url:",downUrl.getResult().toString());
Add the code below:
Task<Uri> downUrl=taskSnapshot.getMetadata().getReference().getDownloadUrl();
Log.i("url:",downUrl.getResult().toString());
edited Aug 2 '18 at 9:11
SirPeople
2,192827
2,192827
answered Aug 2 '18 at 8:20
QamberQamber
213
213
it doesn't return a url
– JAMSHAID
Aug 3 '18 at 4:38
add a comment |
it doesn't return a url
– JAMSHAID
Aug 3 '18 at 4:38
it doesn't return a url
– JAMSHAID
Aug 3 '18 at 4:38
it doesn't return a url
– JAMSHAID
Aug 3 '18 at 4:38
add a comment |
You wont get the download url of image now using
ImageUrl = taskSnapshot.getDownloadUrl().toString();
this method is deprecated.
Instead you can use the below method
uniqueId = UUID.randomUUID().toString();
ur_firebase_reference = storageReference.child("user_photos/" + uniqueId);
Uri file = Uri.fromFile(new File(mphotofile.getAbsolutePath()));
UploadTask uploadTask = ur_firebase_reference.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 ur_firebase_reference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
System.out.println("Upload " + downloadUri);
Toast.makeText(mActivity, "Successfully uploaded", Toast.LENGTH_SHORT).show();
if (downloadUri != null) {
String photoStringLink = downloadUri.toString(); //YOU WILL GET THE DOWNLOAD URL HERE !!!!
System.out.println("Upload " + photoStringLink);
}
} else {
// Handle failures
// ...
}
}
});
You can track the progress of uploading by adding Progress listeners,
uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
System.out.println("Upload is " + progress + "% done");
Toast.makeText(mContext, "Upload is " + progress + "% done", Toast.LENGTH_SHORT).show();
}
}).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
@Override
public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
System.out.println("Upload is paused");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Handle successful uploads on complete
// ...
}
});
add a comment |
You wont get the download url of image now using
ImageUrl = taskSnapshot.getDownloadUrl().toString();
this method is deprecated.
Instead you can use the below method
uniqueId = UUID.randomUUID().toString();
ur_firebase_reference = storageReference.child("user_photos/" + uniqueId);
Uri file = Uri.fromFile(new File(mphotofile.getAbsolutePath()));
UploadTask uploadTask = ur_firebase_reference.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 ur_firebase_reference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
System.out.println("Upload " + downloadUri);
Toast.makeText(mActivity, "Successfully uploaded", Toast.LENGTH_SHORT).show();
if (downloadUri != null) {
String photoStringLink = downloadUri.toString(); //YOU WILL GET THE DOWNLOAD URL HERE !!!!
System.out.println("Upload " + photoStringLink);
}
} else {
// Handle failures
// ...
}
}
});
You can track the progress of uploading by adding Progress listeners,
uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
System.out.println("Upload is " + progress + "% done");
Toast.makeText(mContext, "Upload is " + progress + "% done", Toast.LENGTH_SHORT).show();
}
}).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
@Override
public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
System.out.println("Upload is paused");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Handle successful uploads on complete
// ...
}
});
add a comment |
You wont get the download url of image now using
ImageUrl = taskSnapshot.getDownloadUrl().toString();
this method is deprecated.
Instead you can use the below method
uniqueId = UUID.randomUUID().toString();
ur_firebase_reference = storageReference.child("user_photos/" + uniqueId);
Uri file = Uri.fromFile(new File(mphotofile.getAbsolutePath()));
UploadTask uploadTask = ur_firebase_reference.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 ur_firebase_reference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
System.out.println("Upload " + downloadUri);
Toast.makeText(mActivity, "Successfully uploaded", Toast.LENGTH_SHORT).show();
if (downloadUri != null) {
String photoStringLink = downloadUri.toString(); //YOU WILL GET THE DOWNLOAD URL HERE !!!!
System.out.println("Upload " + photoStringLink);
}
} else {
// Handle failures
// ...
}
}
});
You can track the progress of uploading by adding Progress listeners,
uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
System.out.println("Upload is " + progress + "% done");
Toast.makeText(mContext, "Upload is " + progress + "% done", Toast.LENGTH_SHORT).show();
}
}).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
@Override
public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
System.out.println("Upload is paused");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Handle successful uploads on complete
// ...
}
});
You wont get the download url of image now using
ImageUrl = taskSnapshot.getDownloadUrl().toString();
this method is deprecated.
Instead you can use the below method
uniqueId = UUID.randomUUID().toString();
ur_firebase_reference = storageReference.child("user_photos/" + uniqueId);
Uri file = Uri.fromFile(new File(mphotofile.getAbsolutePath()));
UploadTask uploadTask = ur_firebase_reference.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 ur_firebase_reference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
System.out.println("Upload " + downloadUri);
Toast.makeText(mActivity, "Successfully uploaded", Toast.LENGTH_SHORT).show();
if (downloadUri != null) {
String photoStringLink = downloadUri.toString(); //YOU WILL GET THE DOWNLOAD URL HERE !!!!
System.out.println("Upload " + photoStringLink);
}
} else {
// Handle failures
// ...
}
}
});
You can track the progress of uploading by adding Progress listeners,
uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
System.out.println("Upload is " + progress + "% done");
Toast.makeText(mContext, "Upload is " + progress + "% done", Toast.LENGTH_SHORT).show();
}
}).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
@Override
public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
System.out.println("Upload is paused");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Handle successful uploads on complete
// ...
}
});
answered Sep 20 '18 at 12:04
MIDHUN CEASARMIDHUN CEASAR
417
417
add a comment |
add a comment |
Just use a Task instead of ref.putFile(uriImage)
.addOnSuccessListener(new OnSuccessListener()
Nowadays, firebase references suggest using Uploadtask objects
I've done it like this:
UploadTask uploadTask;
uploadTask = storageReferenceProfilePic.putFile(uriProfileImage );
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 storageReferenceProfilePic.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
progressBarImageUploading.setVisibility(View.GONE);
Uri downloadUri = task.getResult();
profileImageUrl = downloadUri.toString();
ins.setText(profileImageUrl);
} else {
// Handle failures
// ...
}
}
});
Notice these lines in the above code:
Uri downloadUri = task.getResult();
profileImageUrl = downloadUri.toString();
Now profileImageUrl contains something like "http://adressofimage" which is the url to acess the image
Now you're free to use the String profileImageUrl however you wish. For e.g., load the url into an ImageView using Glide or Fresco libraries.
add a comment |
Just use a Task instead of ref.putFile(uriImage)
.addOnSuccessListener(new OnSuccessListener()
Nowadays, firebase references suggest using Uploadtask objects
I've done it like this:
UploadTask uploadTask;
uploadTask = storageReferenceProfilePic.putFile(uriProfileImage );
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 storageReferenceProfilePic.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
progressBarImageUploading.setVisibility(View.GONE);
Uri downloadUri = task.getResult();
profileImageUrl = downloadUri.toString();
ins.setText(profileImageUrl);
} else {
// Handle failures
// ...
}
}
});
Notice these lines in the above code:
Uri downloadUri = task.getResult();
profileImageUrl = downloadUri.toString();
Now profileImageUrl contains something like "http://adressofimage" which is the url to acess the image
Now you're free to use the String profileImageUrl however you wish. For e.g., load the url into an ImageView using Glide or Fresco libraries.
add a comment |
Just use a Task instead of ref.putFile(uriImage)
.addOnSuccessListener(new OnSuccessListener()
Nowadays, firebase references suggest using Uploadtask objects
I've done it like this:
UploadTask uploadTask;
uploadTask = storageReferenceProfilePic.putFile(uriProfileImage );
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 storageReferenceProfilePic.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
progressBarImageUploading.setVisibility(View.GONE);
Uri downloadUri = task.getResult();
profileImageUrl = downloadUri.toString();
ins.setText(profileImageUrl);
} else {
// Handle failures
// ...
}
}
});
Notice these lines in the above code:
Uri downloadUri = task.getResult();
profileImageUrl = downloadUri.toString();
Now profileImageUrl contains something like "http://adressofimage" which is the url to acess the image
Now you're free to use the String profileImageUrl however you wish. For e.g., load the url into an ImageView using Glide or Fresco libraries.
Just use a Task instead of ref.putFile(uriImage)
.addOnSuccessListener(new OnSuccessListener()
Nowadays, firebase references suggest using Uploadtask objects
I've done it like this:
UploadTask uploadTask;
uploadTask = storageReferenceProfilePic.putFile(uriProfileImage );
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 storageReferenceProfilePic.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
progressBarImageUploading.setVisibility(View.GONE);
Uri downloadUri = task.getResult();
profileImageUrl = downloadUri.toString();
ins.setText(profileImageUrl);
} else {
// Handle failures
// ...
}
}
});
Notice these lines in the above code:
Uri downloadUri = task.getResult();
profileImageUrl = downloadUri.toString();
Now profileImageUrl contains something like "http://adressofimage" which is the url to acess the image
Now you're free to use the String profileImageUrl however you wish. For e.g., load the url into an ImageView using Glide or Fresco libraries.
answered Dec 25 '18 at 20:05
Sougata GhoshSougata Ghosh
12
12
add a comment |
add a comment |
protected by Community♦ Feb 21 at 16:38
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
1
firebase.google.com/docs/reference/android/com/google/firebase/…
– Nilesh Rathod
May 22 '18 at 12:38