【发布时间】:2021-04-27 06:53:20
【问题描述】:
我需要将我的文件缩略图从网络上传到 Firebase 存储。 URL总是像
使用 HTTPS,我在执行此操作时遇到了很多麻烦
为了上传到 Firebase 存储,我可以使用位图、inputStream、URL 或本地文件
我尝试了许多不同的库,毕加索,滑翔,...但我总是得到相同的文件未找到错误。从我在这里读到的,我认为这是因为它是 HTTPS
我该怎么做?
谢谢!
编辑:
我尝试按照建议使用 glide 库,但不适用于所有 url,这是我的代码:
// 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);
}
});
这是我得到的错误:
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
编辑 2: 我尝试添加 android:networkSecurityConfig="@xml/network_security_config" android:usesCleartextTraffic="true">
这是网络安全:
<?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>
我有 android 10 和 glide 4.11 版
【问题讨论】:
-
“我总是遇到同样的文件未找到错误”你能显示minimal code that reproduces the problem that you're asking for our help on吗?这样更有可能有人可以提供帮助。
-
@FrankvanPuffelen 我按照建议尝试使用 Glide 库并用错误更新了问题
标签: java android firebase android-studio https