Before gradle update everything worked fine, but later on this error popped up. I have referred to the official documents and it provides the same code. Not accepting the ge
UploadTask.getDownloadUrl()
is deprecated. Use StorageReference.getDownloadUrl() instead.
For kotlin
val uploadTask = fileReference.putFile(Uri.fromFile(primaryFile), metadata)
and
uploadTask
.addOnProgressListener { taskSnapshot ->
}
.addOnPausedListener {
}
.addOnSuccessListener { taskSnapshot ->
}
.continueWithTask { task ->
if (!task.isSuccessful) {
throw task.exception!!
}
fileReference.downloadUrl
}
.addOnCompleteListener { task ->
if (task.isSuccessful) {
val downloadUrl: Uri = task.result
} else {
// Handle failures
}
}
.addOnFailureListener { e ->
}
val downloadUrl
is your URL that is uploaded.
As Doug pointed out, UploadTask.getDownloadUrl()
is deprecated, so use StorageReference.getDownloadUrl().
But StorageReference.getDownloadUrl() returns Task, which must be handled asynchronously, you cannot do Uri downloadUrl = photoRef.getDownloadUrl().getResult();
else you will get java.lang.IllegalStateException: Task is not yet complete
Therefore, handle it asynchronously like this
photoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Uri downloadUrl = uri;
Toast.makeText(getBaseContext(), "Upload success! URL - " + downloadUrl.toString() , Toast.LENGTH_SHORT).show();
}
});
If you have a 'image_uri' and put it to the firebase storage this code will help you.
private StorageReference storageReference= FirebaseStorage.getInstance().getReference();
final StorageReference ref = storageReference.child("picture.jpg");
ref.putFile(image_uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
final Uri downloadUrl = uri;
}
});
Try this code..
public class MainActivity extends AppCompatActivity {
String LOG_TAG = MainActivity.class.getSimpleName();
Button buttonUpload, buttonDownload;
RadioGroup radioGroup;
ImageView imageviewResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonUpload = findViewById(R.id.upload_button);
buttonDownload = findViewById(R.id.download_button);
radioGroup = findViewById(R.id.radio_group);
imageviewResult = findViewById(R.id.resultant_imageview);
buttonUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
uploadImage();
}
});
buttonDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
downloadImage();
}
});
}
private void uploadImage() {
// Start by getting our StorageReference
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference rootRef = storage.getReference();
StorageReference bearRef = rootRef.child("images/bear.jpg");
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading");
progressDialog.show();
// Get the data from the image as bytes
ImageView bearImage = getSelectedBearImage();
bearImage.setDrawingCacheEnabled(true);
bearImage.buildDrawingCache();
Bitmap bitmap = bearImage.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();
// Upload it to our reference
UploadTask uploadTask = bearRef.putBytes(data);
buttonDownload.setEnabled(false);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
progressDialog.dismiss();
Log.w(LOG_TAG, "Upload failed: " + exception.getMessage());
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
Uri downloadUrl = taskSnapshot.getDownloadUrl();
progressDialog.dismiss();
Log.d(LOG_TAG, "Download Url: " + downloadUrl);
buttonDownload.setEnabled(true);
}
});
}
private void downloadImage() {
// Start by getting a reference to the same location we uploaded to
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference rootRef = storage.getReference();
StorageReference bearRef = rootRef.child("images/bear.jpg");
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading");
progressDialog.show();
// Download our data with a max allocation of 1MB
final long ONE_MEGABYTE = 1024 * 1024;
bearRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
// Convert bytes to bitmap and call setImageBitmap
progressDialog.dismiss();
Log.d(LOG_TAG, "Download successful");
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
imageviewResult.setImageBitmap(bitmap);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
progressDialog.dismiss();
Log.w(LOG_TAG, "Download failed: " + exception.getMessage());
}
});
}
private ImageView getSelectedBearImage() {
switch (radioGroup.getCheckedRadioButtonId()) {
case R.id.radio1:
return findViewById(R.id.image_bear1);
case R.id.radio2:
return findViewById(R.id.image_bear2);
case R.id.radio3:
return findViewById(R.id.image_bear3);
case R.id.radio4:
return findViewById(R.id.image_bear4);
default:
return findViewById(R.id.image_bear1);
}
}
}
activity.xml..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.adruser.firebasestorageapp.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="96dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/image_bear1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="centerInside"
android:src="@drawable/a"/>
<ImageView
android:id="@+id/image_bear2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="centerInside"
android:src="@drawable/tiger"/>
<ImageView
android:layout_width="0dp"
android:id="@+id/image_bear3"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="centerInside"
android:src="@drawable/download"/>
<ImageView
android:layout_width="0dp"
android:id="@+id/image_bear4"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="centerInside"
android:src="@drawable/add"/>
</LinearLayout>
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radio1"
android:layout_width="0dp"
android:layout_weight="1"
android:checked="true"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/radio3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/radio4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</RadioGroup>
<TextView
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Downloaded Image"/>
<ImageView
android:id="@+id/resultant_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"/>
<Button
android:id="@+id/upload_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Upload"/>
<Button
android:id="@+id/download_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:layout_marginTop="8dp"
android:text="Download"/>
</LinearLayout>
and i hope you connetcted with firebase storage and give internet permission..
<uses-permission android:name="android.permission.INTERNET"/>
more information refer this link.. https://code.tutsplus.com/tutorials/image-upload-to-firebase-in-android-application--cms-29934
getDownloadUrl
no longer exists.
So the new method is:
final StorageReference ref = storageRef.child("images/mountains.jpg");
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();
} else {
// Handle failures
// ...
}
}
});
Firebase doc