I need to upload the thumbnails of my files from the web to firebase storage. The URL is always like
with the HTTPS and I'm having a lot of trouble doing this
In order to upload to firebase storage, I can use a bitmap, inputStream, URL, or a local File
I have tried with many different libraries, Picasso, glide,... but I always get the same error of file not found. From what I read here, I think it is because it is an HTTPS
How can I do this?
Thanks!
EDIT:
I tried using the glide library as sugested, but didnt work with all urls, this is my code:
// the thmbnail link is combined with the fileId
String thumbnailLink = "https://drive.google.com/thumbnail?authuser=0&sz=w320&id=" + databasePost.getFileId();
Log.i("SecondActivity", "thumbnailLink: "+ thumbnailLink );
String thumbnailId = databasePost.getFileId() + ".png";
Glide.with(this)
.asBitmap()
.load(thumbnailLink)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap image, @Nullable Transition<? super Bitmap> transition) {
Log.i("SecondActivity", "loadBitmap: resource ready " );
String thumbnailId = databasePost.getFileId() + ".png";
StorageReference postRef = storage.child("thumbnails").child(thumbnailId);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();
UploadTask uploadTask = postRef.putBytes(data);
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Log.i("SecondActivity", "saveThumbnailToStorage: success");
databasePost.setThumbnailId(thumbnailId);
// saveDataToDatabase(databasePost);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i("SecondActivity", "saveThumbnailToStorage: error: " + e.getLocalizedMessage());
}
});
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {
super.onLoadFailed(errorDrawable);
Log.i("SecondActivity", "loadBitmap: error: " + errorDrawable);
}
});
This is the error I get:
2021-01-22 17:24:39.500 8486-8486/com.curso.testebottomnavigationviewdriveapi W/Glide: Load failed for https://drive.google.com/thumbnail?authuser=0&sz=w320&id=14fwtU1NKPaeuSmagklCHRLJwxRCnSgjial7MoySK2-k with size [-2147483648x-2147483648]
class com.bumptech.glide.load.engine.GlideException: Failed to load resource
There was 1 cause:
java.io.FileNotFoundException(https://lh3.googleusercontent.com/d/14fwtU1NKPaeuSmagklCHRLJwxRCnSgjial7MoySK2-k=w320)
call GlideException#logRootCauses(String) for more detail
Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Fetching data failed, class java.io.InputStream, REMOTE
There was 1 cause:
java.io.FileNotFoundException(https://lh3.googleusercontent.com/d/14fwtU1NKPaeuSmagklCHRLJwxRCnSgjial7MoySK2-k=w320)
call GlideException#logRootCauses(String) for more detail
Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Fetch failed
There was 1 cause:
java.io.FileNotFoundException(https://lh3.googleusercontent.com/d/14fwtU1NKPaeuSmagklCHRLJwxRCnSgjial7MoySK2-k=w320)
call GlideException#logRootCauses(String) for more detail
Cause (1 of 1): class java.io.FileNotFoundException: https://lh3.googleusercontent.com/d/14fwtU1NKPaeuSmagklCHRLJwxRCnSgjial7MoySK2-k=w320
2021-01-22 17:24:39.501 8486-8486/com.curso.testebottomnavigationviewdriveapi I/Glide: Root cause (1 of 1)
java.io.FileNotFoundException: https://lh3.googleusercontent.com/d/14fwtU1NKPaeuSmagklCHRLJwxRCnSgjial7MoySK2-k=w320
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:259)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:211)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:30)
at com.bumptech.glide.load.data.HttpUrlFetcher.loadDataWithRedirects(HttpUrlFetcher.java:102)
at com.bumptech.glide.load.data.HttpUrlFetcher.loadDataWithRedirects(HttpUrlFetcher.java:118)
at com.bumptech.glide.load.data.HttpUrlFetcher.loadData(HttpUrlFetcher.java:56)
at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.loadData(MultiModelLoader.java:100)
at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.startNextOrFail(MultiModelLoader.java:164)
at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.onLoadFailed(MultiModelLoader.java:154)
at com.bumptech.glide.load.data.HttpUrlFetcher.loadData(HttpUrlFetcher.java:62)
at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.loadData(MultiModelLoader.java:100)
at com.bumptech.glide.load.engine.SourceGenerator.startNextLoad(SourceGenerator.java:70)
at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:63)
at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:310)
at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:279)
at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
at com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.run(GlideExecutor.java:393)
2021-01-22 17:24:39.501 8486-8486/com.curso.testebottomnavigationviewdriveapi I/SecondActivity: loadBitmap: error: null
EDIT 2: I tried adding android:networkSecurityConfig="@xml/network_security_config" android:usesCleartextTraffic="true">
this is the network security:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">drive.google.com</domain>
</ domain-config>
</network-security-config>
I have android 10 and glide version 4.11
I finally discovered what was causing this error. So basically I was trying to upload the thumbnail right after uploading the file but if I did the exact same but some time after uploading it worked fine.
Apparently you have to wait sometime after uplaoding so that the link t the thumbnail is valid so now I'm using the exact same code to upload with glide but before using it I wait 10 seconds and it works perfectly!
try {
TimeUnit.SECONDS.sleep(10);
String thumbnailLink = "https://drive.google.com/thumbnail?authuser=0&sz=w320&id=" + databasePost.getFileId();
Log.i("SecondActivity", "thumbnailLink: "+ thumbnailLink );
String thumbnailId = databasePost.getFileId() + ".png";
Glide.with(this)
.asBitmap()
.load(thumbnailLink)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap image, @Nullable Transition<? super Bitmap> transition) {
Log.i("SecondActivity", "loadBitmap: resource ready " );
String thumbnailId = databasePost.getFileId() + ".png";
StorageReference postRef = storage.child("thumbnails").child(thumbnailId);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();
UploadTask uploadTask = postRef.putBytes(data);
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Log.i("SecondActivity", "saveThumbnailToStorage: success");
databasePost.setThumbnailId(thumbnailId);
// saveDataToDatabase(databasePost);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i("SecondActivity", "saveThumbnailToStorage: error: " + e.getLocalizedMessage());
}
});
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {
super.onLoadFailed(errorDrawable);
Log.i("SecondActivity", "loadBitmap: error: " + errorDrawable);
}
});
} catch (InterruptedException e) {
Log.i("UploadPostActivity", "failure sleep: "+e.getMessage());
e.printStackTrace();
}
If I understood correctly first of all you have a problem loading the image. I'm not sure that the url you specified can be loaded without API (it is not possible to open it in the browser for me)
Have you tried any other image/url?
Check in the log where the error is...
You can load bitmap with Glide (working code):
private void loadBitmapByGlide(String imageUrl, String imageName) {
Glide.with(this)
.asBitmap()
.load(imageUrl)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap image, @Nullable Transition<? super Bitmap> transition) {
//TODO upload image to firebase storage or something else
//String imagePath = savePNG(resource, imageName);
Toast.makeText(getApplicationContext(), "Image loaded: " + image.getWidth() + "-" + image.getHeight(), Toast.LENGTH_SHORT).show();
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
}
gradle file
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.