我正在使用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();
}
});
}
}
}
即使我将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)
答案 0 :(得分:2)
您收到此错误是因为storageRef
变量尚未初始化。任何在null
引用上调用方法的尝试都将导致java.lang.NullPointerException
。
在您的情况下,您应首先获得对FirebaseStorage
实例的引用:
FirebaseStorage storage = FirebaseStorage.getInstance();
然后,您可以通过getReferenceFromUrl
方法获得所需存储的引用。
StorageReference storageRef = storage.getReferenceFromUrl("Url to storage");
以下storageRef
已经启动并可以使用。
答案 1 :(得分:0)
导入该文件很简单
import com.google.firebase.storage.StorageReference
;
您的欢迎:)
答案 2 :(得分:0)
变量 "build": "react-scripts build && cp build/index.html build/404.html",
未初始化。
首先,创建一个storageRef
变量:
FirebaseStorage
然后像这样初始化storage = FirebaseStorage.getInstance();
类型变量StorageReference
:
storageRef