你好每个人都在做一个发布应用程序都很好,但我想实现放置用户个人资料照片,它将在帖子片段中拯救,在用户片段像所有个人资料照片但它给了我这个错误, 尝试在fragement对话框中执行相同操作以上传照片但不起作用
例外:
03-25 14:22:12.567 22927-22927/com.android.octa.memetixs E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.octa.memetixs, PID: 22927
Theme: themes:{default=overlay:system, iconPack:system, fontPkg:system, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system}
java.lang.IllegalArgumentException: location must not be null or empty
at com.google.android.gms.common.internal.zzac.zzb(Unknown Source)
at com.google.firebase.storage.FirebaseStorage.getReference(Unknown Source)
at com.android.octa.memetixs.ui.fragment.UserFragment.onCreateView(UserFragment.java:67)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1984)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:626)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1268)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
at android.view.View.measure(View.java:18799)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18799)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:18799)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1081)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2899)
at android.view.View.measure(View.java:18799)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2108)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1224)
at android.view.ViewRootImpl.performTraversals(ViewR
片段用户: (当我实现上传照片的方法给我错误时)
public class UserFragment extends Fragment {
private View mRootView;
private ImageView mUserImage;
private TextView mUsername;
private ImageButton mChangePhotoUser, mLogOut;
private Uri mSelectedImageUri;
private User mUser;
private Post mPost;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private final static int RC_PHOTO_PICKER = 2;
public UserFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.fragment_user, container, false);
mAuth = FirebaseAuth.getInstance();
init();
StorageReference storageReference = FirebaseStorage.getInstance().getReference(mUser.getPhotUrl());
Glide.with(getActivity()).using(new FirebaseImageLoader()).load(storageReference).into(mUserImage);
return mRootView;
}
private void init(){
final String userId = FirebaseUtils.getUid();
mUserImage = (ImageView) mRootView.findViewById(R.id.imageViewUser_fragUser);
mUsername = (TextView) mRootView.findViewById(R.id.textViewUsername_fragUser);
mLogOut = (ImageButton) mRootView.findViewById(R.id.imageButtonExit_FragUser);
mChangePhotoUser = (ImageButton) mRootView.findViewById(R.id.buttonAddPhoto_fragUser);
mUser = new User();
mChangePhotoUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectImage();
}
});
if(mSelectedImageUri != null){
FirebaseUtils.getImagesUserRef().child(mSelectedImageUri.getLastPathSegment()).putFile(mSelectedImageUri).addOnSuccessListener(getActivity(), new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
String url = Constants.USER_IMAGE + "/" + mSelectedImageUri.getLastPathSegment();
mUser.setPhotUrl(url);
addToMyOtherList(userId);
}
});
}else{
addToMyOtherList(userId);
}
mLogOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
logOut();
}
});
}
private void logOut(){
mAuth.signOut();
}
private void addToMyOtherList(String imageId){
FirebaseUtils.getUserImageProfileRef().child(imageId).setValue(mUser);
FirebaseUtils.getMyUserProfileRef().child(imageId).setValue(true).addOnCompleteListener(getActivity(), new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// null
}
});
FirebaseUtils.addToMyOtherRecord(Constants.USER_IMAGE_KEY, imageId);
}
private void selectImage(){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), RC_PHOTO_PICKER);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_PHOTO_PICKER) {
if (resultCode == RESULT_OK) {
mSelectedImageUri = data.getData();
mUserImage.setImageURI(mSelectedImageUri);
}
}
}
}
我以此课程为例上传了用户照片
public class PostCreateDialog extends DialogFragment implements View.OnClickListener {
private static final int RC_PHOTO_PICKER = 1;
private View mRootView;
private Post mPost;
private ProgressDialog mProgressDialog;
private Uri mSelectedImageUri;
private ImageView mPostDisplay;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
mPost = new Post();
mProgressDialog = new ProgressDialog(getContext());
mRootView = getActivity().getLayoutInflater().inflate(R.layout.post_create_dialog, null);
mPostDisplay = (ImageView) mRootView.findViewById(R.id.post_dialog_display);
mRootView.findViewById(R.id.post_dialog_select_imageview).setOnClickListener(this);
mRootView.findViewById(R.id.post_dialog_send_imageview).setOnClickListener(this);
builder.setView(mRootView);
return builder.create();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.post_dialog_send_imageview:
sendPost();
break;
case R.id.post_dialog_select_imageview:
selectImage();
break;
}
}
private void selectImage(){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), RC_PHOTO_PICKER);
}
private void sendPost(){
mProgressDialog.setMessage("Sending post...");
mProgressDialog.setCancelable(false);
mProgressDialog.setIndeterminate(true);
mProgressDialog.show();
FirebaseUtils.getUserRef(FirebaseUtils.getCurrentUser().getEmail().replace(".", ","))
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
final String postUid = FirebaseUtils.getUid();
TextView postDialogTextView = (TextView) mRootView.findViewById(R.id.post_dialog_edittext);
String text = postDialogTextView.getText().toString();
mPost.setUser(user);
mPost.setNumComments(0);
mPost.setNumLikes(0);
mPost.setTimeCreated(System.currentTimeMillis());
mPost.setPostId(postUid);
mPost.setPostText(text);
if (mSelectedImageUri != null) {
FirebaseUtils.getImagesSRef()
.child(mSelectedImageUri.getLastPathSegment())
.putFile(mSelectedImageUri)
.addOnSuccessListener(getActivity(),
new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
String url = Constants.POST_IMAGES + "/" + mSelectedImageUri.getLastPathSegment();
//String downloadUrl = taskSnapshot.getDownloadUrl().toString();
mPost.setPostImageUrl(url);
addToMyPostList(postUid);
}
});
} else {
addToMyPostList(postUid);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
mProgressDialog.dismiss();
}
});
}
private void addToMyPostList(final String postUid){
FirebaseUtils.getPostRef().child(postUid)
.setValue(mPost);
FirebaseUtils.getMyPostRef().child(postUid).setValue(true)
.addOnCompleteListener(getActivity(), new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
mProgressDialog.dismiss();
dismiss();
}
});
FirebaseUtils.addToMyRecord(Constants.POSTS_KEY, postUid);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_PHOTO_PICKER) {
if (resultCode == RESULT_OK) {
mSelectedImageUri = data.getData();
mPostDisplay.setImageURI(mSelectedImageUri);
}
}
}
}
最后&#34; utils&#34;这里的类是参考文献
我创建了另一种方法&#34; addToMyotherRecord&#34; 添加用户prodile照片但不起作用
public class FirebaseUtils {
public static DatabaseReference getUserRef(String email) {
return FirebaseDatabase.getInstance()
.getReference(Constants.USERS_KEYS)
.child(email);
}
public static DatabaseReference getUserImageProfileRef(){
return FirebaseDatabase.getInstance().getReference(Constants.USER_IMAGE_KEY);
}
public static DatabaseReference getMyUserProfileRef(){
return FirebaseDatabase.getInstance().getReference(Constants.MY_USER_IMAGE).child(getCurrentUser().getEmail().replace(".",","));
}
public static DatabaseReference getPostRef() {
return FirebaseDatabase.getInstance()
.getReference(Constants.POSTS_KEY);
}
public static Query getPostQuery() {
return getPostRef().orderByChild(Constants.TIME_CREATED_KEY);
}
public static DatabaseReference getPostLikedRef() {
return FirebaseDatabase.getInstance()
.getReference(Constants.POSTS_LIKED_KEY);
}
public static DatabaseReference getPostLikedRef(String postId) {
return getPostLikedRef().child(getCurrentUser().getEmail()
.replace(".", ","))
.child(postId);
}
public static FirebaseUser getCurrentUser() {
return FirebaseAuth.getInstance().getCurrentUser();
}
public static String getUid() {
String path = FirebaseDatabase.getInstance().getReference().push().toString();
return path.substring(path.lastIndexOf("/") + 1);
}
public static StorageReference getImagesSRef() {
return FirebaseStorage.getInstance().getReference(Constants.POST_IMAGES);
}
public static StorageReference getImagesUserRef(){
return FirebaseStorage.getInstance().getReference(Constants.USER_IMAGE);
}
public static DatabaseReference getMyPostRef() {
return FirebaseDatabase.getInstance().getReference(Constants.MY_POSTS)
.child(getCurrentUser().getEmail().replace(".", ","));
}
public static DatabaseReference getCommentRef(String postId) {
return FirebaseDatabase.getInstance().getReference(Constants.COMMENTS_KEY)
.child(postId);
}
public static DatabaseReference getMyRecordRef() {
return FirebaseDatabase.getInstance().getReference(Constants.USER_RECORD)
.child(getCurrentUser().getEmail().replace(".", ","));
}
public static void addToMyOtherRecord(String node, final String id){
FirebaseUtils.getMyRecordRef().child(node).runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
ArrayList<String> myUserImageCollection;
if(mutableData.getValue() == null){
myUserImageCollection = new ArrayList<>(1);
myUserImageCollection.add(id);
mutableData.setValue(myUserImageCollection);
}else{
myUserImageCollection = (ArrayList<String>) mutableData.getValue();
myUserImageCollection.add(id);
mutableData.setValue(myUserImageCollection);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
}
});
}
public static void addToMyRecord(String node,final String id) {
FirebaseUtils.getMyRecordRef().child(node).runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
ArrayList<String> myPostCollection;
if (mutableData.getValue() == null) {
myPostCollection = new ArrayList<>(1);
myPostCollection.add(id);
mutableData.setValue(myPostCollection);
} else {
myPostCollection = (ArrayList<String>) mutableData.getValue();
myPostCollection.add(id);
mutableData.setValue(myPostCollection);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
}
});
}
}
和常量
public class Constants {
public static final String POSTS_KEY = "posts";
public static final String TIME_CREATED_KEY = "timeCreated";
public static final String NUM_LIKES_KEY = "numLikes";
public static final String POSTS_LIKED_KEY = "posts_liked";
public static final String EXTRA_POST_ID = "postId";
public static final String POST_IMAGES = "post_images";
public static final String USER_IMAGE = "user_imagee";
public static final String MY_USER_IMAGE = "my_profileeImages";
public static final String USER_IMAGE_KEY = "userImage_key"; // se agrega al record
public static final String MY_POSTS = "my_posts";
public static final String EXTRA_POST = "post";
public static final String COMMENTS_KEY = "comments";
public static final String USER_RECORD = "user_record";
public static final String NUM_SHARES_KEY = "numShares";
public static String USERS_KEYS = "users";
}