我是Android Studio的新手。我的代码有问题,因为我在关注YouTube教程,所以我不知道它是什么 在Android plus Firebase中创建用户的说明。问题出在Firebase上。
我的代码:
public class Profile extends AppCompatActivity {
private static final int CHOOSE_IMAGE = 101;
EditText editText;
ImageView imageView;
String profileimageurl;
Uri uriProfileImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile2);
editText = (EditText) findViewById(R.id.nome);
imageView = (ImageView) findViewById(R.id.foto);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showImageChooser();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CHOOSE_IMAGE && data != null && data.getData() != null) {
uriProfileImage = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uriProfileImage);
imageView.setImageBitmap(bitmap);
uploadImageToFirebaseStorage();
} catch(IOException e) {
e.printStackTrace();
}
}
}
private void uploadImageToFirebaseStorage() {
StorageReference profileImageRef = FirebaseStorage.getInstance().getReference("profilepics/" + System.currentTimeMillis() + ".jpg");
if (uriProfileImage != null) {
profileImageRef.putFile(uriProfileImage)
.addOnSuccessListener(new OnSuccessListener < UploadTask.TaskSnapshot > () {@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
profileimageurl = taskSnapshot.getDownloadUrl().toString();
}
}).addOnFailureListener(new OnFailureListener() {@Override
public void onFailure(@NonNull Exception e) {
}
});
}
}
private void showImageChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Escohe a imagem de perfil"), CHOOSE_IMAGE);
}
}
答案 0 :(得分:1)
该方法已被弃用。 使用storageReference获取下载网址。
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
profileImageRef.addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(final Uri uri) {
String downloadUri = uri;
} })
您现在可以使用downloadUri字符串加载图像。