Hello everyone I have a problem that I have tried to solve for 3 days without stopping, but I can not solve it and this is despairing, it is with the StorageReference,
When I call the getReference()
method it gives me a "location must not be empty or null" but the point is if I am giving the storage reference, and when trying to use with getReferenceFromUrl()
, run, you can get the photo with Glide or with picasso but to return to my activity I get a null pointer exception, that I can not use it. Is this
"java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String android.net.Uri.getLastPathSegment ()' on a null object
reference"
The photos that I put in the method send arrive with success but when wanting to rescue them to use them in an IMageView with picasso or glide gives me that problem
I tried all the possible ways but it does not work
This is the class that gives me problems. The exception problem is in the populate viewHolder in the StorageRefence line and I put the file in the sendComment
method
private void initCommentSection() {
RecyclerView commentRecyclerView = (RecyclerView) findViewById(R.id.comment_recyclerview);
commentRecyclerView.setLayoutManager(new LinearLayoutManager(CommentActivity.this));
here in the populateVIewHolder is the problem, in the storage reference, in the getLastPathSegment()
FirebaseRecyclerAdapter<Comment, CommentHolder> commentAdapter =
new FirebaseRecyclerAdapter<Comment, CommentHolder>(
Comment.class,
R.layout.row_comment,
CommentHolder.class,
FirebaseUtils.getCommentRef(mPost.getPostId())) {
@Override
protected void populateViewHolder(final CommentHolder viewHolder, Comment model, int position) {
viewHolder.setUsername(mUser.getUsername());
viewHolder.setComment(model.getComment());
viewHolder.setTime(DateUtils.getRelativeTimeSpanString(model.getTimeCreated()));
StorageReference sdsd = FirebaseStorage.getInstance().getReferenceFromUrl(FirebaseUtils.getCommentsImagesRef().child(mSelectedImageUri.getLastPathSegment()).toString());
sdsd.getDownloadUrl().addOnSuccessListener(CommentActivity.this, new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Picasso.with(CommentActivity.this).load(uri).into(viewHolder.imageBetaComm);
}
});
if(mSelectedImageUri == null){
StorageReference storageReference = FirebaseStorage.getInstance().getReferenceFromUrl("https://firebasestorage.googleapis.com/v0/b/memetics-e9fac.appspot.com/o/bite_carpet%2Fbite.png?alt=media&token=36b90aed-5448-4493-836a-ad5554848820");
storageReference.getDownloadUrl().addOnSuccessListener(CommentActivity.this, new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Picasso.with(CommentActivity.this).load(uri).into(viewHolder.imageBetaComm);
}
});
}
}
};
commentRecyclerView.setAdapter(commentAdapter);
}
ans here sends the photo to the storage
private void sendComment() {
final ProgressDialog progressDialog = new ProgressDialog(CommentActivity.this);
progressDialog.setMessage("Sending comment..");
progressDialog.setCancelable(true);
progressDialog.setIndeterminate(true);
progressDialog.show();
final String uid = FirebaseUtils.getUid();
final String strComment = mCommentEditTextView.getText().toString();
FirebaseUtils.getUserRef(FirebaseUtils.getCurrentUser().getEmail().replace(".", ","))
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
if(mSelectedImageUri != null){
mComent.setUser(user);
mComent.setCommentId(uid);
mComent.setComment(strComment);
mComent.setTimeCreated(System.currentTimeMillis());
FirebaseUtils.getCommentsImagesRef().child(mSelectedImageUri.getLastPathSegment()).putFile(mSelectedImageUri);
}else{
mComent.setUser(user);
mComent.setCommentId(uid);
mComent.setComment(strComment);
mComent.setTimeCreated(System.currentTimeMillis());
}
FirebaseUtils.getCommentRef(mPost.getPostId())
.child(uid)
.setValue(mComent);
FirebaseUtils.getMyCommentRef().child(uid).setValue(true);
FirebaseUtils.addToMyRecord(Constants.COMMENTS_KEY, uid);
FirebaseUtils.getPostRef().child(mPost.getPostId())
.child("numComments")
.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
long num = (long) mutableData.getValue();
mutableData.setValue(num + 1);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
progressDialog.dismiss();
FirebaseUtils.addToMyRecord(Constants.COMMENTS_KEY,uid);
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
progressDialog.dismiss();
}
});
}
答案 0 :(得分:0)
我找到了解决方案
if(mSelectedImageUri != null){
mComent.setUser(user);
mComent.setCommentId(uid);
mComent.setComment(strComment);
mComent.setTimeCreated(System.currentTimeMillis());
FirebaseUtils.getCommentsImagesRef().child(mSelectedImageUri.getLastPathSegment()).putFile(mSelectedImageUri).addOnSuccessListener(CommentActivity.this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
String url = Constants.COMMENT_IMAGES + "/" + mSelectedImageUri.getLastPathSegment();
mComent.setPhotoCommentUrl(url);
FirebaseUtils.getCommentRef(mPost.getPostId())
.child(uid)
.setValue(mComent);
FirebaseUtils.getMyCommentRef().child(uid).setValue(true);
FirebaseUtils.addToMyRecord(Constants.COMMENTS_KEY, uid);
}
});
}else{
mComent.setUser(user);
mComent.setCommentId(uid);
mComent.setComment(strComment);
mComent.setTimeCreated(System.currentTimeMillis());
String urlnull = Constants.PHOTO_COMMENT_ROW + "/" + "bite.png";
mComent.setPhotoCommentUrl(urlnull);
FirebaseUtils.getCommentRef(mPost.getPostId())
.child(uid)
.setValue(mComent);
FirebaseUtils.getMyCommentRef().child(uid).setValue(true);
FirebaseUtils.addToMyRecord(Constants.COMMENTS_KEY, uid);
}