我正在尝试将图片上传到Firebase。如果我将图片从InputStream
加载到我的应用中,并使用putBytes()
上传,则可以使用,如果我尝试直接使用putStream()
上传图片,则会收到错误消息,流关闭:
11-09 14:16:39.126 26077-26586/com.finder E/StorageException: StorageException has occurred. An unknown error occurred, please check the HTTP result code and inner exception for server response. Code: -13000 HttpResult: 200 11-09 14:16:39.126 26077-26586/com.finder E/StorageException: closed java.io.IOException: closed at com.android.okhttp.okio.RealBufferedSource$1.read(RealBufferedSource.java:366) at com.google.firebase.storage.internal.AdaptiveStreamBuffer.fill(com.google.firebase:firebase-storage@@16.0.4:107) at com.google.firebase.storage.UploadTask.uploadChunk(com.google.firebase:firebase-storage@@16.0.4:396) at com.google.firebase.storage.UploadTask.run(com.google.firebase:firebase-storage@@16.0.4:205) at com.google.firebase.storage.StorageTask.lambda$getRunnable$7(com.google.firebase:firebase-storage@@16.0.4:1106) at com.google.firebase.storage.StorageTask$$Lambda$12.run(com.google.firebase:firebase-storage@@16.0.4) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818)
上传图片的方法:
static class UploadPicAsyncWrapperTask extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... strings) {
String picsFolder = strings[0];
String id = strings[1];
final String url = strings[2];
final String profilepicNodeName = strings[3];
final String picsNodeName = strings[4];
final StorageReference filePathRoot = FirebaseStorage.getInstance().getReference().child(picsFolder).child(FirebaseAuth.getInstance().getCurrentUser().getUid());
final StorageReference filePath = filePathRoot.child(id);
try (InputStream is = new URL(url).openStream()) { StorageMetadata.Builder().setContentType("image/jpg").setContentLanguage("en").build();
UploadTask uploadTask = filePath.putStream(is);
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
filePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
if (profilePicName == null) {
mUserDatabase.child(profilepicNodeName).setValue(uri.toString());
profilePicName = uri.toString();
} else {
mUserDatabase.child(picsNodeName).child(Helper.imageUrlToFirebasePath(uri.toString())).setValue(uri.toString());
}
--picsRemaining;
}
});
}
});
} catch (MalformedURLException e) {
Log.e(TAG, "BufferedInputStream DIIIIID throw MalformedURLException 2 " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "BufferedInputStream DIIIIID throw IOException 2 " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "BufferedInputStream DIIIIID throw Exception 2 " + e.getClass() + " " + e.getMessage());
}
return null;
}
}
我使用InputStream
还是BufferedInputStream
都没关系。
Resuming UploadTask results in E/StorageException: BufferedInputStream is closed说要使用Firebase的另一个版本,但是由于它是旧版本,所以我已经使用了。