[英]Unable to resolve symbol storageRef in firebase
I am using fire base to store the images in that i am getting a error in storage Ref unable to resolve the symbol please help.我正在使用 fire base 来存储图像,因为我在存储中遇到错误 Ref 无法解析符号,请帮忙。
public class Sample extends Activity {
public static final String GridViewDemo_ImagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pencilrulerlearner/";
StorageReference riversRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File targetDirector = new File(GridViewDemo_ImagePath);
File[] files = targetDirector.listFiles();
for (File file1 : files) {
Uri file= Uri.fromFile(file1);
storageRef.child("images/" + file.getLastPathSegment());
UploadTask uploadTask = riversRef.putFile(file);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
Uri downloadUrl = taskSnapshot.getDownloadUrl();
}
});
}
}
}
Even i changed the storageRef to riversRef where i am getting the following error即使我将 storageRef 更改为riversRef ,我也收到以下错误
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.storage.StorageReference com.google.firebase.storage.StorageReference.child(java.lang.String)' on a null object reference
at nidhinkumar.firebaseexample.Sample.onCreate(Sample.java:31)
at android.app.Activity.performCreate(Activity.java:6092)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
You're getting this error because storageRef
variable hasn't been initialized.您收到此错误是因为
storageRef
变量尚未初始化。 Any attempt to invoke a method on a null
reference will lead to java.lang.NullPointerException
.任何对
null
引用调用方法的尝试都将导致java.lang.NullPointerException
。
In your case you should first get a reference to the FirebaseStorage
instance:在您的情况下,您应该首先获得对
FirebaseStorage
实例的引用:
FirebaseStorage storage = FirebaseStorage.getInstance();
Then, you can get a reference to the storage you want, by getReferenceFromUrl
method.然后,您可以通过
getReferenceFromUrl
方法获取对所需存储的引用。
StorageReference storageRef = storage.getReferenceFromUrl("Url to storage");
Hereinafter storageRef
is initilized and can be used.以下
storageRef
被storageRef
并可以使用。
The variable storageRef
is not initialized.变量
storageRef
未初始化。
First, create a FirebaseStorage
variable:首先,创建一个
FirebaseStorage
变量:
storage = FirebaseStorage.getInstance();
Then initialize the StorageReference
type variable storageRef
like this:然后像这样初始化
StorageReference
类型变量storageRef
:
storageRef = storage.getReference();
StorageReference storageReference = FirebaseStorage.getInstance().getReference(); StorageReference storageReference = FirebaseStorage.getInstance().getReference();
do jus a simple thing import this file做一个简单的事情导入这个文件
import com.google.firebase.storage.StorageReference
; import com.google.firebase.storage.StorageReference
;
Your Welcome :)欢迎您:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.