[当我尝试将照片上传到Firebase存储时,Android应用程序崩溃

问题描述 投票:0回答:1

用户选择照片,在按钮上单击该应用程序应将其上传到Firebase存储中

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_post);
 storage=FirebaseStorage.getInstance();
    mRef=storage.getReference();


    final Uri selectedImg = getIntent().getData();
    mImageView = (ImageView) findViewById(R.id.post_img_last);
    mImageView.setImageURI(selectedImg);
    uploadB = (Button) findViewById(R.id.upload);
    uploadB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         StorageReference reference=mRef.child("posts");
            reference.putFile(selectedImg);

        }
    });

}
}

当我单击按钮时,应用程序崩溃。

这是build.gradle

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
//noinspection GradleCompatible
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile  'com.google.firebase:firebase-auth:10.0.1'
compile 'com.firebaseui:firebase-ui-auth:1.2.0'
compile 'com.google.firebase:firebase-storage:10.0.1'
testCompile 'junit:junit:4.12'

}套用外挂程式:'com.google.gms.google-services'

这是错误日志

06-08 18:12:14.192 32236-32236/com.example.fcb.test A/FirebaseApp: Firebase API initialization failure.
                                                               java.lang.reflect.InvocationTargetException
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                                   at com.google.firebase.FirebaseApp.zza(Unknown Source)
                                                                   at com.google.firebase.FirebaseApp.initializeApp(Unknown Source)
                                                                   at com.google.firebase.FirebaseApp.initializeApp(Unknown Source)
                                                                   at com.google.firebase.FirebaseApp.initializeApp(Unknown Source)
                                                                   at com.google.firebase.provider.FirebaseInitProvider.onCreate(Unknown Source)
                                                                   at android.content.ContentProvider.attachInfo(ContentProvider.java:1761)
                                                                   at android.content.ContentProvider.attachInfo(ContentProvider.java:1730)
                                                                   at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source)
                                                                   at android.app.ActivityThread.installProvider(ActivityThread.java:5748)
                                                                   at android.app.ActivityThread.installContentProviders(ActivityThread.java:5314)
                                                                   at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5254)
                                                                   at android.app.ActivityThread.access$1500(ActivityThread.java:178)
                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1609)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                   at android.os.Looper.loop(Looper.java:214)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:6102)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
android firebase google-play-services image-uploading firebase-storage
1个回答
0
投票

根据错误Firebase API初始化]中的指定失败。显然,您需要在使用前初始化Firebase。

您需要创建一个称为BaseActivity的Activity,它应该看起来像这样。

import androidx.annotation.Nullable;
import com.google.firebase.FirebaseApp;

public class BaseActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FirebaseApp.initializeApp(this);
  }
}

此后,您需要在清单文件的标记内添加android:name属性,它应该看起来像这样

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    //this u need to add
    android:name=".BaseActivity"

    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
© www.soinside.com 2019 - 2024. All rights reserved.