我尝试使用Firebase存储下载文件,但即使我在onHandleIntent中调用它,我也会收到此错误。
@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
final String action = intent.getAction();
switch (action) {
case ACTION_DOWNLOAD:
ArrayList<Talk> talks = intent.getParcelableArrayListExtra(EXTRA_TALKS);
if (talks == null) return;
handleDownload(talks);
break;
}
}
}
这是我尝试在本地复制文件的地方。
private void handleDownload(ArrayList<Talk> talks) {
for (final Talk talk : talks) {
FirebaseStorage.getInstance()
.getReferenceFromUrl(talk.getUrl())
.getStream()
.addOnSuccessListener(new OnSuccessListener<StreamDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(StreamDownloadTask.TaskSnapshot taskSnapshot) {
try {
File file = new File(DEFAULT_TALKS_DIR, "name_" + UUID.randomUUID());
IOUtils.copyLarge(taskSnapshot.getStream(), new FileOutputStream(file));
talk.setPath(file.getAbsolutePath());
TalksDatabase.getInstance(context).insertTalk(talk);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
完整的例外:
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
at com.android.org.conscrypt.Platform.blockGuardOnNetwork(Platform.java:300)
at com.android.org.conscrypt.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:741)
at com.android.okhttp.okio.Okio$2.read(Okio.java:136)
at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
at com.android.okhttp.okio.RealBufferedSource.read(RealBufferedSource.java:50)
at com.android.okhttp.internal.http.HttpConnection$FixedLengthSource.read(HttpConnection.java:418)
at com.android.okhttp.okio.RealBufferedSource$1.read(RealBufferedSource.java:371)
at com.google.firebase.storage.StreamDownloadTask$zza.read(Unknown Source)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1792)
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1769)
at eu.long1.jwnotes.services.ReceiveService$4.onSuccess(ReceiveService.java:206)
at eu.long1.jwnotes.services.ReceiveService$4.onSuccess(ReceiveService.java:200)
at com.google.firebase.storage.StorageTask$1.zza(Unknown Source)
at com.google.firebase.storage.StorageTask$1.zzk(Unknown Source)
at com.google.firebase.storage.zze$2.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
更新 你们是对的,从主线程中调用了监听器。我刚刚使用@AlexShutov
找到了这个public zzbtz(@Nullable Executor var1) {
this.zzbFQ = var1;
if(this.zzbFQ == null) {
this.mHandler = new Handler(Looper.getMainLooper());
} else {
this.mHandler = null;
}
}
答案 0 :(得分:2)
Firebase在主线程上调用.onSuccess()回调方法,以方便用户使用。您尝试从流复制到文件,这涉及网络操作。尝试在后台制作副本。