【发布时间】:2020-03-16 13:20:50
【问题描述】:
Hello So basically when clicking on the image view the dialog opens and i can choose a picture however when the picture is selected the app get's me disconnected from the account without uploading and updating the picture .
dialog = new SpotsDialog.Builder(getContext()).create();
storageReference=FirebaseStorage.getInstance().getReference("image_upload");
avatarIv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent =new Intent();
intent.setType("image/");
intent.setAction(intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Choisissez une image"),PICK_IMAGE_CODE);
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE_CODE ){
dialog.show();
UploadTask uploadTask =storageReference.putFile(data.getData());
Task<Uri>task= uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()){
Toast.makeText(getActivity(),"Erreur de téléchargement",Toast.LENGTH_SHORT).show();
}
return storageReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if(task.isSuccessful()){
String Url =task.getResult().toString().substring(0,task.getResult().toString().indexOf("&token"));
Log.d("DIRECTLINK",Url);
Picasso.get().load(Url).into(avatarIv);
dialog.dismiss();
}
}
});
}
}
通过在 if(requestCode == PICK_IMAGE_CODE ) 中使用断点 我得到了这个
Ps:这段代码是在一个片段中实现的(底部导航视图)
私有静态最终 int PICK_IMAGE_CODE = 1000;
编辑:这些是存储规则 允许读、写:if request.auth != null;
【问题讨论】:
标签: java firebase android-fragments firebase-storage android-studio-2.3