This question already has answers here:
How to get URL from Firebase Storage getDownloadURL
(13 answers)
Closed 4 years ago.
So when I upload an image onto Firebase Storage, the getDownloadUrl() method generates a string such as: com.google.android.gms.tasks.zzu#275cc4
Now I'm trying to place the image uploaded into an ImageView:
ImageView img;
img = v.findViewById(R.id.imgView);
Glide.with(getApplicationContext()).load("com.google.android.gms.tasks.zzu#275cc4").into(img);
This doesn't seem to load the image. I tried with the full firebase url https://firebasestorage.googleapis.com/v0/..... and this worked, but I can't seem to find a method that generates this when I upload an image?
storageRef.child(path).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
Glide.with(getApplicationContext()).load(uri.toString()).into(img);
}
});
Hope this will help you.......Have a nice day
To solve this, you need to pass to the load() method the actual url of the photo. I'm affraid:
com.google.android.gms.tasks.zzu#275cc4
Is not a valid url for an image. So change the above url with one that is correct formated and includes: https://firebasestorage.googleapis.com/v0/....
To verify if the url is correct, just copy and paste that url in a browser and see if it opens an image. If it doesn't open, it means that you have provided an incorrect url.
Related
Is there a way in Firebase Storage to generate a download url pointing to nothing, in order to upload a file to that url later? something like that (in Kotlin):
fun generateItemPhotoUrl(id: String) =
storageRef.child("$Id/${generateUniqueName()}.${COMPRESS_FORMAT.name}").downloadUrl
This code returns a failed task...
I want this so my upload process can look like so:
// Case: old photo is null but new one is not - upload new photo to a new uri
generateItemPhotoUrl(itemId).continueWithTask { generateTask ->
if (generateTask.isSuccessful) {
val destUrl = generateTask.result.toString()
// Uploading may take time, so first update document to hold a uri, so consecutive
// calls will result in updating instead of uploading a new file
updateItemPhoto(itemId, destUrl).continueWithTask { updateTask ->
if (updateTask.isSuccessful)
uploadFileToDest(destUrl, newImage).continueWithTask { uploadTask ->
if (!uploadTask.isSuccessful) updateItemPhoto(itemId, null)
}
}
}
}
As explained in code, I need this to prevent the case of updating the item's photo twice in a row too fast for the first one to finish it's upload. I end up with 2 files - one of them is not referenced from anywhere. If I could do something like this, the second upload will go to my "update" case (instead of the "new photo" case presented here) - where the file will be switched correctly.
Is there a way in Firebase Storage to generate a download URL pointing to nothing, in order to upload a file to that URL later?
No, this is not possible. You cannot generate a Storage URL in advance and upload the file sometime later. You get the download URL only when the file is successfully uploaded on the Firebase servers. This is because the URL that comes from the UploadTask contains a token that is generated on the server, and it's apart of the URL. To get the entire download URL of an uploaded file, please see my answer from the following post:
How to get the download url from Firebase Storage?
The process of uploading the file is asynchronous, meaning that any code that needs that URL, needs to be inside the" onSuccess()" method, or be called from there. So there is no need to upload the file twice.
I'm developing some kind of android mail app and I get each mail attachments as an ArrayList of urls from a rest api and I want to use them in some kind of attachment section. I need to check the urls and pass image links to a preview adapter using glide api and show other urls (other file formats, like .pdf, .docx or ...) in another section as a download link.
Is there any way to know if the url is link to a image file or not before downloading it?
I know there are seemingly similar threads that are answered already but this is different in two ways.
First I want to to know if the url is link to image or not before downloading it.
Second I don't want to use static extension check. Because there are like tons of different extensions like .jpg, .png,... and they may change and I don't want to update my app with each change.
There is a way you can do it but I'm not sure its the best approach.
Code:
new Thread(new Runnable() { // if already doing the checking on network thread then no need to add this thread
#Override
public void run() {
try {
URLConnection connection = new URL("image url here").openConnection();
String contentType = connection.getHeaderField("Content-Type");
boolean image = contentType.startsWith("image/"); //true if image
Log.i("IS IMAGE", "" + image);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
Hope this helps!
You can provide additional fields,which can help you identify file format, in your rest API.
You can checkout response content-type. Checkout this answer:
https://stackoverflow.com/a/5802223
If you have the URI you could:
use this for the full path
and substring after the last "."
I am passing an image from a webpage using AJAX request. The image is passed as a Data URI to a Java function as shown below.
public Result upload() {
String dataUri = request().body().asText();
System.out.println(dataUri);
File file = dataUri;
return ok("File uploaded");
}
The problem I am having is I think I need to convert the data URI to a file object in order to pass it into "File file = ". And if I can do that, then the image will be uploaded to the server. Any takers?
Cropped and uploaded! I found another link on stackoverflow that showed me how to convert base64 image to file. I have successfully loaded the cropped file to a directory on the server. Thanks for your help!! The link which has the answer is here:
Convert base64 string to image
Here in my web-application under Images folder pdficon.png image is available I want to add this image using Image.getInstance() method but whaen I tring like this..
image = Image.getInstance("images/pdficon.png");
Here it showing Exception is
IOException :: C:\Users\Developpc\Downloads\wildfly-9.0.2.Final\bin\images\pdficon.png (The system cannot find the path specified)
So,what I need to do....
Signup for some free image hosting account and upload your image. After uploading your image you can get the URL of the image and put that URL for image.getInstanceMethod.
Image img = Image.getInstance(IMG_URL);
If you are using that image multiple times then create an string constant and use that.
I use Glide library in my Android project to update avatar from URL that comes in response from API.
The problem is that when I try to load different image (from different URL) to the same imageView it shows me an image that was downloaded first time. (URL forming works fine, I tried it in browser and it shows the needed image)
Here is my code:
EditProfileFragment.xml
public static final String IMAGE_BASE_URL = "http://myapi.com/img/";
String imageUrl = Const.IMAGE_BASE_URL + cb_getProfile.photo; //imageName.jpg
Glide.with(mContext)
.load(imageUrl)
.animate(R.anim.abc_fade_in)
.centerCrop()
.into(mImageView_photo);
Solved this problem by adding a StringSignature when loading image from the URL.
Source
Glide.with(mContext)
.load(imageUrl)
.animate(R.anim.abc_fade_in)
.signature(new StringSignature(String.valueOf(System.currentTimeMillis())))
.centerCrop()
.into(mImageView_photo)
;
Thanks to Yoav Sternberg.