Android FireBase error when uploading picture - android

I am trying to upload a picture via a local file method.
UploadTask uploadTask = currentPicRef.putFile(file, metadata);
When the user pick's image from the gallery or taking a picture via the camera the picture is saved in external storage and I save the uri in shared preference.
I successfully loaded the image into an imageview using the setImageURI(uri) method, but When I call the firebase method and using the same uri (Uri file = Uri.fromFile(new File(fileName));)
I get the error
could not locate file for uploading:file:///content%3A/media/external/images/media/22943
but when I use log to check the local file I get
uri is content://media/external/images/media/22943
also worth to mention that when I used uri.parse() instead uri.fromFile() in firebase to upload from a local file it uploaded the metadata but not the photo itself.
Any ideas how to fix it?

Maybe it's a bit late, anyway:
could not locate file for uploading:file:///content%3A/media/external/images/media/22943
I think you may have a problem of url encoding (see %3A in the file uri).
Try to decode the uri Uri file = Uri.fromFile(new File(fileName)); before passing it to Firebase Storage Reference.
If it does not work, instead retrieving the image uri in this way Uri.fromFile(new File(fileName)) , you can try to save the local image path in the shared preferences (I'm supposing that when you get the image from gallery or camera, you can have also the local path), and use this to upload the image in the storage Firebase.
Hope can help.

fileName can be determined fileUri.toString(). This worked for me:
Uri uploadUri = Uri.fromFile(new File(fileUri.toString()));
Here's an example that works:
// [START upload_from_uri]
private void uploadFromUri(Uri fileUri) {
Uri uploadUri = Uri.fromFile(new File(fileUri.toString()));
Log.d(TAG, "uploadFromUri:src:" + fileUri.toString());
// [START get_child_ref]
// Get a reference to store file at photos/<FILENAME>.jpg
final StorageReference photoRef = mStorageRef.child("photos").child(uploadUri.getLastPathSegment());
// [END get_child_ref]
// Upload file to Firebase Storage
// [START_EXCLUDE]
showProgressDialog();
// [END_EXCLUDE]
Log.d(TAG, "uploadFromUri:dst:" + photoRef.getPath());
photoRef.putFile(uploadUri)
.addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Upload succeeded
Log.d(TAG, "uploadFromUri:onSuccess");
// Get the public download URL
mDownloadUrl = taskSnapshot.getMetadata().getDownloadUrl();
// [START_EXCLUDE]
hideProgressDialog();
updateUI(mAuth.getCurrentUser());
// [END_EXCLUDE]
}
})
.addOnFailureListener(this, new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Upload failed
Log.w(TAG, "uploadFromUri:onFailure", exception);
mDownloadUrl = null;
// [START_EXCLUDE]
hideProgressDialog();
Toast.makeText(MainActivity.this, "Error: upload failed",
Toast.LENGTH_SHORT).show();
updateUI(mAuth.getCurrentUser());
// [END_EXCLUDE]
}
});
}
// [END upload_from_uri]

Related

Why can't this retieve the download URL of an image on Firebase Storage?

I am trying to retrieve an image from firebase storage for an android app by getting the download url of the image I want to retrieve in the onSuccessListener of the putImage call I used to upload the image in the first place. I am saving the download URL in a field variable but the issue is that the field variable is not being updated. I used toast to print out the value of the url field variable after my attempt in setting it equal to the download URl of the image I want to retrieve, but it remains unchanged. I'm not sure why this is the case so I would appreciate any help in resolving this.
//field variable that s meant to hold download url for the image I want to retrieve
String url = "never changed"
...
final String imageKey = UUID.randomUUID().toString();
StorageReference profileref = storageReference.child("contactProfiles/" + imageKey);
profileref.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Task<Uri> task = taskSnapshot.getMetadata().getReference().getDownloadUrl();
task.addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
//setting url equal to url of the image I want to retrieve
url = uri.toString();
}
});
Toast.makeText(ContactCreation.this, "Successful Profile Picture Upload", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(ContactCreation.this, "Failed to Upload Profile Picture", Toast.LENGTH_SHORT).show();
}
});
//Seeing if the url changed or not
Toast.makeText(ContactCreation.this, url, Toast.LENGTH_SHORT).show();
The call to getDownloadURL makes a call to the server, and thus is executed asynchronously. This means that right now in your code, the Toast.makeText(ContactCreation.this, url runs before the url = uri.toString() ever runs, which explains why it logs no value.
The solution is always the same: any code that needs the value, needs to be inside the onSuccess that is called with the value, or it needs to be called from there.
So:
task.addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
url = uri.toString();
Toast.makeText(ContactCreation.this, url, Toast.LENGTH_SHORT).show();
}
});
If you're new to such asynchronous behavior, I recommend reading some more about it. For example with:
URL generated by Firebase Storage does not allow access to the file, which shows some chaining of Task listeners.
How to get the download url from Firebase Storage?
Hello, I have problem to upload my image to my firebase storage

Can't get actual download Url of an image uploaded in firebase storage [duplicate]

This question already has answers here:
How to get URL from Firebase Storage getDownloadURL
(13 answers)
Closed 2 years ago.
I'm trying to get the download url of the image uploaded to firebase database. But Task Uri imageURL = storageReference.getDownloadUrl(); doesn't gives the actual download URL of an image stored in the firebase storage i.e it gives this - com.google.android.gms.tasks.zzu#27da5837
getdownloadUrl() doesn't work in case of as it is deprecated:
Uri imageUrl = storagereference.getdownloadUrl(); //Gives error
please see the code here:
final StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("images").child(imageName);
final UploadTask uploadTask = storageReference.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle unsuccessful uploads
Toast.makeText(CreateSnapActivity.this, "Upload Failed", Toast.LENGTH_SHORT).show();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, etc.
//Task<Uri> imageURL = storageReference.getDownloadUrl();
//Log.i("URL", imageURL.toString()); // gives this url - com.google.android.gms.tasks.zzu#27da5837 which is not correct
Task<Uri> urlTask = storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
Log.i("Url", String.valueOf(uri));
imageUrl = uri.toString(); //Gives the correct url but it cannot store this uri value outside this function i.e for Intent shown below outside this func.
}
});
Toast.makeText(CreateSnapActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(CreateSnapActivity.this, ChooseUserActivity.class);
intent.putExtra("imageName", imageName);
intent.putExtra("imageURL", imageUrl);
intent.putExtra("message", captionEditText.getText().toString());
startActivity(intent);
}
});
Calling getDownloadUrl() starts an asynchronous operation, which may take some time to complete. Because of this, your main code continues to run while the download URL is being retrieved to prevent blocking the user. Then when the download URL is available, your onSuccess gets called. Because of this, by the time your intent.putExtra("imageURL", imageUrl) now runs, the imageUrl = uri.toString() hasn't run yet.
To see that this indeed true, I recommend putting some logging in the code to just show the flow, or running it in a debugger and setting breakpoints on the lines I indicated above.
To fix it, any code that needs the download URL, needs to be inside the onSuccess, or be called from there. This applies not just here, but to all code that runs asynchronously, which includes most modern cloud APIs. So I recommend spending some time now studying this behavior, so that you're more comfortable with it going forward.
In your code:
final StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("images").child(imageName);
final UploadTask uploadTask = storageReference.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Toast.makeText(CreateSnapActivity.this, "Upload Failed", Toast.LENGTH_SHORT).show();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {
Task<Uri> urlTask = storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
imageUrl = uri.toString();
Toast.makeText(CreateSnapActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(CreateSnapActivity.this, ChooseUserActivity.class);
intent.putExtra("imageName", imageName);
intent.putExtra("imageURL", imageUrl);
intent.putExtra("message", captionEditText.getText().toString());
startActivity(intent);
}
});
}
});
Also see:
URL generated by Firebase Storage does not allow access to the file
How to get download url for firebase storage and update firestore document?
imageURL is a task.You are trying to print a task. not task's result. please try this code:
Task<Uri> imageURL = storageReference.getDownloadUrl();
Log.i("URL", imageURL.toString());
Log.i("URL", imageURL.result.toString());

How to choose specified File to upload to Firebase Storage?

I would like to upload my SQLite database File to Firebase Storage. My problem is that I can only find tutorials where the user has to choose the file manually, but I would like to send a fixed file, so you can't send other data to Firebase storage.
How can I do that?
Thank you.
First get the reference to the firebase storage
// Create a storage reference from our app
StorageReference storageRef = storage.getReference();
Then to upload the file do the following:
Uri file = Uri.fromFile("sqlite_file");
StorageReference filesRef = storageRef.child("files");
uploadTask = filesRef.putFile(file);
// Register observers to listen for when the download is done or if it fails
uploadTask.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) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, etc.
// ...
}
});

Firebase Storage error: error getting token

I am building an android app which allows users to upload pictures to firebase storage. I am still in development mode so I set up my storage rules to public. When the user selects a picture to upload, the file is not uploaded but the download URL is returned. Logcat shows the following error
E/StorageUtil: error getting token java.util.concurrent.ExecutionException: com.google.firebase.internal.api.FirebaseNoSignedInUserException: Please sign in before trying to get a token.
And here is my storage rules
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write;
}
}
}
And my android java code:
private void uploadPic() {
StorageReference mStorageRef = FirebaseStorage.getInstance().getReference();
Uri fileUrl = Uri.fromFile(new File(filePath));
String fileExt = MimeTypeMap.getFileExtensionFromUrl(fileUrl.toString());
final String fileName = UUID.randomUUID().toString()+"."+fileExt;
StorageReference profilePicsRef = mStorageRef.child("profile_pics/"+fileName);
profilePicsRef.putFile(fileUrl)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Get a URL to the uploaded content
Uri downloadUrl = taskSnapshot.getDownloadUrl();
Log.d("DOWNLOAD_URL", downloadUrl.toString());
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle unsuccessful uploads
// ...
Toast.makeText(getApplicationContext(), "Error: "+exception.toString(), Toast.LENGTH_LONG).show();
}
});
}
Any help will be greatly appreciated.
UPDATE
I have narrowed down the problem: I have two folders in storage, I can upload to the "Videos" folder, but not to the "profile_pics" folder or any other folder. Why is this happening?
For some reason unknown to mere mortals, the gods have refused upload to any folder that starts with "profile". I had to create another folder "users_profile_pic". Three days wasted!

How to get all files from firebase storage?

I have uploaded some files into Firebase directory and I want to list them and download one by one.There is no API/Documentation fo this.Could you help me ?
As of July 2019, version 18.1.0 of the Cloud Storage SDK now supports listing all objects from a bucket. You simply need to call listAll() in a StorageReference:
StorageReference storageRef = FirebaseStorage.getInstance().getReference();
// Now we get the references of these images
storageRef.listAll().addOnSuccessListener(new OnSuccessListener<ListResult>() {
#Override
public void onSuccess(ListResult result) {
for(StorageReference fileRef : result.getItems()) {
// TODO: Download the file using its reference (fileRef)
}
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(Exception exception) {
// Handle any errors
}
});
If you want to download these files, you can use one of the options shown in the Docs.
Please note that in order to use this method, you must opt-in to version 2 of Security Rules, which can be done by making rules_version = '2'; the first line of your security rules:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
first , you should get the file download Url to retrieve it, the easiest way is to upload a file and generate the download Url in your database, so after that you just go and retrieve each file from the storage like this :
private void downloadFile() {
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReferenceFromUrl("<your_bucket>");
StorageReference islandRef = storageRef.child("file.txt");
File rootPath = new File(Environment.getExternalStorageDirectory(), "file_name");
if(!rootPath.exists()) {
rootPath.mkdirs();
}
final File localFile = new File(rootPath,"imageName.txt");
islandRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Log.e("firebase ",";local tem file created created " +localFile.toString());
// updateDb(timestamp,localFile.toString(),position);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Log.e("firebase ",";local tem file not created created " +exception.toString());
}
});
}
I personally use this method, whenever you upload a file, save its download URL in your Firebase Database, if you are uploading multiple files then save it in an array. There is no method for Firebase Storage android to download and upload multiple files in one go.
Whenever you want to download files access your firebase database for those URL's.

Categories

Resources