简体   繁体   中英

Android Firebase java.lang.StackOverflowError: stack size 8MB error when user is uploading an image

Hello I'm trying to create a profile screen where the user can upload their own profile image and change the ImageView into something that they've uploaded. But upon using this Upload profile image for a user Firebase as a reference I encountered an error when I'm starting to upload the images to the database.

My Code

public class userMain extends Fragment {
private TextView userfirstName,usersecondName,userNumber,userGender,userEmail,userDate;
private Button btnChoose, btnUpload;
private ImageView imageView;
private Uri filePath;
private FirebaseStorage storage;
private StorageReference storageReference;

private final int PICK_IMAGE_REQUEST = 71;


public userMain() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view =  inflater.inflate(R.layout.fragment_user_main, container, false);
    btnUpload = (Button) view.findViewById(R.id.savePhoto);
    imageView = (ImageView) view.findViewById(R.id.imageView3);
    userfirstName = (TextView) view.findViewById(R.id.firstnametv);
    usersecondName = (TextView) view.findViewById(R.id.secondnametv);
    userNumber = (TextView) view.findViewById(R.id.numbertv);
    userGender = (TextView) view.findViewById(R.id.gendertv);
    userEmail = (TextView) view.findViewById(R.id.emailtv);
    userDate = (TextView) view.findViewById(R.id.datetv);
    FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
    storage = FirebaseStorage.getInstance();
    storageReference = storage.getReference();

    String useruid=user.getUid();
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Accounts").child("Users").child(useruid);

    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            chooseImage();
        }
    });
    btnUpload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            uploadImage();
        }
    });
    ref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String firstname = dataSnapshot.child("first").getValue(String.class);
            String secondname = dataSnapshot.child("second").getValue(String.class);
            String number = dataSnapshot.child("number").getValue(String.class);
            String gender = dataSnapshot.child("gender").getValue(String.class);
            String email = dataSnapshot.child("email").getValue(String.class);
            String date = dataSnapshot.child("date").getValue(String.class);

            userfirstName.setText(firstname);
            usersecondName.setText(secondname);
            userNumber.setText(number);
            userGender.setText(gender);
            userEmail.setText(email);
            userDate.setText(date);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });






    return view;
}

private void uploadImage() {
    if(filePath != null)
    {
        final ProgressDialog progressDialog = new ProgressDialog(getActivity());
        progressDialog.setTitle("Uploading...");
        progressDialog.show();

        StorageReference ref = storageReference.child("images/"+ UUID.randomUUID().toString());
        ref.putFile(filePath)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        progressDialog.dismiss();
                        Toast.makeText(getContext(), "Uploaded", Toast.LENGTH_SHORT).show();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        progressDialog.dismiss();
                        Toast.makeText(getContext(), "Failed "+e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                })
                .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                        double progress = (100.0*taskSnapshot.getBytesTransferred()/taskSnapshot
                                .getTotalByteCount());
                        progressDialog.setMessage("Uploaded "+(int)progress+"%");
                    }
                });
    }
}

private void chooseImage() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
    String useruid=user.getUid();
    final DatabaseReference img = FirebaseDatabase.getInstance().getReference().child("Accounts").child("Users").child(useruid);
    if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
            && data != null && data.getData() != null )
    {
        filePath = data.getData();
        StorageReference filepath= storageReference.child("Images").child(filePath.getLastPathSegment());
        filepath.putFile(filePath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                Uri downloadUrl = taskSnapshot.getDownloadUrl();
                img.child("image").setValue(downloadUrl);

            }
        });

    }
}

}

Change this:

  StorageReference filepath= storageReference.child("Images").child(filePath.getLastPathSegment());
    filepath.putFile(filePath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            Uri downloadUrl = taskSnapshot.getDownloadUrl();
            img.child("image").setValue(downloadUrl);

        }

to this:

  StorageReference filepath= storageReference.child("Images").child(filePath.getLastPathSegment());
    filepath.putFile(filePath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            String downloadUrl = taskSnapshot.getDownloadUrl().toString()
            img.child("image").setValue(downloadUrl);

        }

saving the downloadUrl as string in your firebase database

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM