【问题标题】:Android Firebase Storage Keep Uploading When Activity Is DestroyedActivity 被销毁时,Android Firebase 存储会继续上传
【发布时间】:2018-04-12 02:27:22
【问题描述】:

当有人使用我的 Android 应用程序将视频上​​传到 Firebase 存储时,我希望即使活动被破坏也能上传视频。我尝试使用 asynctask,但它似乎不起作用。这是异步任务代码:

public class UploadAttachment extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        userRef.child("currentSeason").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                if (dataSnapshot.exists()) {

                    final String attachCurrentSeason = dataSnapshot.getValue().toString();

                    seasonRef.child("id").addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {

                            String path = "users/" + mAuth.getUid() + "/" + attachCurrentSeason + "/game" + String.valueOf(Integer.parseInt(dataSnapshot.getValue().toString()) - 1);
                            StorageReference storageReference = storage.getReference(path);

                            storageReference.putBytes(byteArray);

                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });

                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        return null;
    }
}

我读过很多东西说 Asynctask 在 Activity 被销毁时仍然会运行,但它对我不起作用。 asynctask中不允许使用firebase吗?

感谢任何帮助。提前致谢!

【问题讨论】:

  • 您没有将any listeners(完成、成功或失败)附加到putBytes() 返回的UploadTask。您如何确定上传已成功开始但未完成?
  • @BobSnyder 我知道它不起作用,因为 firebase 存储不会将视频显示为文件
  • 我建议向putBytes() 添加一个完成和/或进度侦听器,这样您至少可以获得一些上传开始的迹象。由于安全规则或其他问题,它可能会立即失败。

标签: android firebase android-asynctask firebase-storage


【解决方案1】:

由于视频发送可能非常繁重,我建议您使用后台服务,您可以在official Android documentation 上找到相关信息。

可以这样创建:(最好定义一个单独的 UploadVideoService.java 类)

public class UploadVideoService extends IntentService {
    public UploadVideoService() {
        super("UploadVideoService");
    }

    @Override
    protected void onHandleIntent(Intent workIntent) {
        String path = workIntent.getStringExtra("path");
        StorageReference storageReference = storage.getReference(path);
        storageReference.putBytes(workIntent.getByteArrayExtra("data"));
    }
}

在您的 Activity 中,启动将所有数据传递给它的服务:

//...
userRef.child("currentSeason").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            final String attachCurrentSeason = dataSnapshot.getValue().toString();
            seasonRef.child("id").addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    String path = "users/" + mAuth.getUid() + "/" + attachCurrentSeason + "/game" + String.valueOf(Integer.parseInt(dataSnapshot.getValue().toString()) - 1);

                    // here you call the service, replace Activity with your Activity name
                    Intent mServiceIntent = new Intent(Activity.this, UploadVideoService.class);
                    mServiceIntent.putExtra("path", path);
                    mServiceIntent.putExtra("data", byteArray);
                    startService(mServiceIntent);

                }
                @Override
                public void onCancelled(DatabaseError databaseError) {}
            });
        }
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {}
});
//...

您还必须在清单中定义服务,这样:

<service android:name=".UploadVideoService" android:exported="false"/>

上传完成后,您可以在通知栏中放一条通知,例如告诉用户工作已完成。

【讨论】:

  • 谢谢,但它说字节数组太大了,我的视频只有 4 MB
  • 如果视频保存在内存中,那么您可以将 Uri 作为字符串传递给服务
  • 谢谢我试过这个,当我在应用程序上时文件上传,但是当我在上传时离开应用程序时,它没有完成。另外,附注:我不确定这是否会影响任何事情,但是在我启动 Intent Service 之后,我正在更改活动。我怀疑这会影响什么,但以防万一。最后,一个意图服务是否只允许在他的应用程序被销毁时启动?因为我是在应用程序运行时启动我的
  • 您可以记录 IntentService 上发生的事情,例如检查 StorageReference 是否正确创建。 IntentService 在后台运行,因此打开或关闭应用程序应该没有任何区别。我唯一能想到的可能是,随着应用程序被破坏,您可能会失去对 Firebase 的引用,因为上传存在问题。因此检查 StorageReference 并查看 logcat 以查看是否有错误(如果 IntentService 中有错误,则无法在可见弹出窗口中崩溃)
  • 感谢您的帮助,但我无法使用 Intent 服务。但是,我确实尝试了常规服务,并且对我有用。
猜你喜欢
  • 1970-01-01
  • 2016-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多