gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 08:22:32 25 4
gpt4 key购买 nike

您好,我正在尝试创建一个个人资料屏幕,用户可以在其中上传自己的个人资料图片并将 ImageView 更改为他们上传的内容。但是在使用这个 Upload profile image for a user Firebase作为引用,我在开始将图像上传到数据库时遇到了错误。

我的代码

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);

}
});

}
}

最佳答案

改变这个:

  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);

}

为此:

  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);

}

downloadUrl 保存为您的 firebase 数据库中的字符串

关于java - 安卓 Firebase java.lang.StackOverflowError : stack size 8MB error when user is uploading an image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48970955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com