![](/img/trans.png)
[英]Upload converted (from Layout) and not saved in internal storage, image to Firebase. In other words, upload <LinearLayout> as image. Android Studio
[英]Download image from HTTPS and upload to firebase storage in Android Studio
我需要将文件的缩略图从 web 上传到 firebase 存储。 URL 总是像
使用 HTTPS,我在执行此操作时遇到了很多麻烦
为了上传到 firebase 存储,我可以使用 bitmap、inputStream、URL 或本地文件
我尝试过许多不同的库,Picasso,glide,......但我总是遇到同样的文件未找到错误。 从我在这里读到的,我认为这是因为它是 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
我终于发现了导致此错误的原因。 所以基本上我试图在上传文件后立即上传缩略图,但如果我做了完全相同的操作,但上传后一段时间效果很好。
显然,您必须在上传后等待一段时间,以便缩略图的链接有效,所以现在我使用完全相同的代码通过 glide 上传,但在使用它之前,我等待 10 秒,它工作得很好!
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();
}
如果我首先理解正确,那么您在加载图像时会遇到问题。 我不确定您指定的 url 是否可以在没有 API 的情况下加载(我无法在浏览器中打开它)
您是否尝试过任何其他图片/网址?
检查错误所在的日志...
您可以使用 Glide 加载 bitmap(工作代码):
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 文件
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.