J'essaie de télécharger des images à l'aide de Firebase Storage. J'ai écrit le code suivant
MainActivity.Java
public class MainActivity extends AppCompatActivity {
ArrayList<Car> carListSuper;
RecyclerView rvCarList;
ProgressBar progressbar;
private CarListAdapter carListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
carListSuper = new ArrayList<>();
Firebase myFirebaseRef = new Firebase("https://carbar-7dae7.firebaseio.com/");
rvCarList = (RecyclerView) findViewById(R.id.rvCarList);
rvCarList.setItemAnimator(new FeedItemAnimator());
progressbar = (ProgressBar) findViewById(R.id.progressCarLoad);
carListAdapter = new CarListAdapter(carListSuper,MainActivity.this);
RecyclerView.LayoutManager linearLayoutManager = new LinearLayoutManager(this);
rvCarList.setLayoutManager(linearLayoutManager);
rvCarList.setAdapter(carListAdapter);
myFirebaseRef.child("carList").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
Log.i("saz",snapshot.getValue().toString());
progressbar.setVisibility(View.GONE);
carListSuper.clear();
//String jsonString = snapshot.getValue().toString();
try {
for (int i=0; i<snapshot.getChildrenCount();i++) {
//Car carList = gson.fromJson(snapshot.child(i+""), Car.class);
DataSnapshot snap = snapshot.child(i+"");
carListSuper.add(FirebaseParser.carParser(snap));
}
carListAdapter.updatelist(carListSuper);
Log.i("saz",snapshot.getValue().toString());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override public void onCancelled(FirebaseError error) { }
});
}
CarListAdapter.Java
public class CarListAdapter<T> extends RecyclerView.Adapter<CarListAdapter.ViewHolder> {
static int i = 0;
private static Context m_context;
List<Car> carList = null;
public static final int VIEW_TYPE_DEFAULT = 1;
public static final int VIEW_TYPE_LOADER = 2;
// Provide a suitable constructor (depends on the kind of dataset)
public CarListAdapter(List<Car> carList, Context context) {
Log.i("saz", "dealerCircularCalls: " + this.carList);
this.carList = carList;
m_context = context;
}
// Create new views (invoked by the layout manager)
@Override
public CarListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.car_item, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
public void updatelist(ArrayList<Car> list) {
ArrayList tempList = new ArrayList();
tempList.addAll(list);
Log.i("saz", "Size on notifyupdate: " + list.size());
carList.clear();
carList.addAll(tempList);
notifyDataSetChanged();
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
Log.i("saz", "position rv: " + position);
/*if (position % 2 != 0) {
holder.cv.setCardBackgroundColor(m_context.getResources().getColor(R.color.lighter_gray));
} else {
holder.cv.setCardBackgroundColor(m_context.getResources().getColor(R.color.white));
}*/
/*CarBarDb db = new CarBarDb(m_context,
CarBarDb.DATABASE_NAME, null, CarBarDb.DATABASE_VERSION);*/
Car car = carList.get(position);
holder.provider.setText("Zoomcar");
holder.carType.setText(car.getCarType());
holder.carName.setText(car.getCarName());
holder.tarrif.setText(car.getPrice_per_hour_weekday());
if (m_context!=null) {
FirebaseStorage storage = FirebaseStorage.getInstance(FirebaseApp.initializeApp(m_context));
StorageReference storageRef = storage.getReferenceFromUrl("gs://carbar-7dae7.appspot.com/");
storageRef.child("CarImages/figo.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Picasso.with(m_context).load(uri)
.error(R.drawable.no_image)
.into(holder.imCarImage);
}
});
}
//db.close();
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
Log.i("saz", "carList.size(): " + carList.size());
return carList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
CardView cv;
TextView provider;
TextView carName;
TextView carType;
TextView tarrif;
ImageButton imCarImage;
public ViewHolder(final View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cvCarItem);
provider = (TextView) cv.findViewById(R.id.tvProvider);
carName = (TextView) cv.findViewById(R.id.tvCarName);
carType = (TextView) cv.findViewById(R.id.tvCarType);
tarrif = (TextView) cv.findViewById(R.id.tvTarrif);
imCarImage = (ImageButton) cv.findViewById(R.id.imgCarImage);
}
}
Pourtant, je reçois l'erreur suivante.
Java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.saz.firebasetest. Make sure to call FirebaseApp.initializeApp(Context) first.
Et en activité principale, j’initialise cet adaptateur en tant que
carListAdapter = new CarListAdapter(carListSuper,MainActivity.this);
J'ai cherché sur ce forum mais je n'ai pas obtenu quelle est la question exacte. S'il vous plaît aider. Lignes de journal:
FATAL EXCEPTION: main
Process: com.saz.firebasetest, PID: 6991
Java.lang.IllegalArgumentException: Null is not a valid value for the FirebaseApp.
at com.google.Android.gms.common.internal.zzac.zzb(Unknown Source)
at com.google.firebase.storage.FirebaseStorage.getInstance(Unknown Source)
at com.saz.firebasetest.controller.CarListAdapter.onBindViewHolder(CarListAdapter.Java:86)
at com.saz.firebasetest.controller.CarListAdapter.onBindViewHolder(CarListAdapter.Java:29)
at Android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.Java:5768)
at Android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.Java:5801)
at Android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.Java:5037)
at Android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.Java:4913)
at Android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.Java:2029)
at Android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.Java:1414)
at Android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.Java:1377)
at Android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.Java:578)
at Android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.Java:3260)
at Android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.Java:3069)
at Android.support.v7.widget.RecyclerView.onLayout(RecyclerView.Java:3518)
at Android.view.View.layout(View.Java:16975)
at Android.view.ViewGroup.layout(ViewGroup.Java:5579)
at Android.widget.FrameLayout.layoutChildren(FrameLayout.Java:396)
at Android.widget.FrameLayout.onLayout(FrameLayout.Java:333)
at Android.view.View.layout(View.Java:16975)
at Android.view.ViewGroup.layout(ViewGroup.Java:5579)
at Android.widget.FrameLayout.layoutChildren(FrameLayout.Java:396)
at Android.widget.FrameLayout.onLayout(FrameLayout.Java:333)
at Android.view.View.layout(View.Java:16975)
at Android.view.ViewGroup.layout(ViewGroup.Java:5579)
at Android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.Java:433)
at Android.view.View.layout(View.Java:16975)
at Android.view.ViewGroup.layout(ViewGroup.Java:5579)
at Android.widget.FrameLayout.layoutChildren(FrameLayout.Java:396)
at Android.widget.FrameLayout.onLayout(FrameLayout.Java:333)
at Android.view.View.layout(View.Java:16975)
at Android.view.ViewGroup.layout(ViewGroup.Java:5579)
at Android.widget.LinearLayout.setChildFrame(LinearLayout.Java:2001)
at Android.widget.LinearLayout.layoutVertical(LinearLayout.Java:1844)
at Android.widget.LinearLayout.onLayout(LinearLayout.Java:1753)
at Android.view.View.layout(View.Java:16975)
at Android.view.ViewGroup.layout(ViewGroup.Java:5579)
at Android.widget.FrameLayout.layoutChildren(FrameLayout.Java:396)
at Android.widget.FrameLayout.onLayout(FrameLayout.Java:333)
at com.Android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.Java:2755)
at Android.view.View.layout(View.Java:16975)
at Android.view.ViewGroup.layout(ViewGroup.Java:5579)
at Android.view.ViewRootImpl.performLayout(ViewRootImpl.Java:2552)
at Android.view.ViewRootImpl.performTraversals(ViewRootImpl.Java:2255)
at Android.view.ViewRootImpl.doTraversal(ViewRootImpl.Java:1321)
at Android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.Java:6737)
at Android.view.Choreographer$CallbackRecord.run(Choreographer.Java:894)
at Android.view.Choreographer.doCallbacks(Choreographer.Java:696)
at Android.view.Choreographer.doFrame(Choreographer.Java:631)
at Android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.Java:880)
at Android.os.Handler.handleCallback(Handler.Java:815)
at Android.os.Handler.dispatchMessage(Handler.Java:104)
at Android.os.Looper.loop(Looper.Java:207)
at Android.app.ActivityThread.main(ActivityThread.Java:5776)
at Java.lang.reflect.Method.invoke(Native Method)
at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:789)
at com.Android.internal.os.ZygoteInit.main(ZygoteInit.
--------- beginning of system
Vous mélangez des versions de SDK Firebase.
Cela provient d'un SDK Firebase 2.x:
Firebase myFirebaseRef = new Firebase("https://carbar-7dae7.firebaseio.com/");
Mais Firebase Storage n’est inclus que dans Firebase 9.x.
Vous devrez passer à une version unique du SDK Firebase. Étant donné que vous voulez utiliser le stockage Firebase, ce sera la version 9.x.
Si vous suivez les instructions sur firebase.google.com , vous pouvez voir que les dépendances pour la base de données et le stockage sont:
dependencies {
// ...
compile 'com.google.firebase:firebase-database:9.6.1'
compile 'com.google.firebase:firebase-storage:9.6.1'
}
Que vous utilisiez exactement 9.6.1 importe peu, mais les versions doivent être identiques, alors que vous incluez maintenant probablement la base de données via compile 'com.firebase:firebase-client-Android:2.5.2+'
.
On dirait que vous faites ce qui suit:
carListAdapter = new CarListAdapter(carListSuper,MainActivity.this);
Cela devrait être le suivant: Dans votre activité
Firebase.setAndroidContext(this);
Firebase myFirebaseRef = new Firebase("https://carbar-7dae7.firebaseio.com/");
carListAdapter = new CarListAdapter(carListSuper,this);
et comme suggestion je ne ferais pas
FirebaseStorage storage = FirebaseStorage.getInstance(FirebaseApp.initializeApp(m_context));
StorageReference storageRef = storage.getReferenceFromUrl("gs://carbar-7dae7.appspot.com/");
dans la méthode onBindViewHolder. Cela ferait dans le constructeur et en faire une variable de classe.
Ajoutez la ligne ci-dessous dans le constructeur de votre adaptateur pour l'initialiser, pas dans la méthode bindviewholder.
FirebaseStorage storage = FirebaseStorage.getInstance(FirebaseApp.initializeApp(m_context));