我有一個活動,其中包含一個用於顯示用戶照片的ImageView。 用戶從手機圖庫中選擇一個圖像,當他完成後,圖像上傳到firebase存儲並返回到ImageView。個人資料圖片消失在背按按鈕
問題是:當用戶轉到另一個活動(或按下後退按鈕)並返回到UserProfile活動時,圖像不再存在於ImageView內部。
如何在UserProfile活動啓動時加載圖像?
這是我的用戶配置活動
public class UserProfile extends AppCompatActivity {
private static final int GALLERY_INTENT = 2 ;
private ImageButton userImage;
private Button save_changes;
private EditText user_name;
private StorageReference mStorage;
private Uri mImageUri ,downloadUri;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
mAuth = FirebaseAuth.getInstance();
userImage = (ImageButton) findViewById(R.id.profileImage);
save_changes = (Button) findViewById(R.id.savechanges);
user_name = (EditText) findViewById(R.id.user_name2);
mStorage = FirebaseStorage.getInstance().getReference();
userImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent galleryIntent = new Intent();
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, GALLERY_INTENT);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK){
Uri imageUri = data.getData();
CropImage.activity(imageUri)
.setGuidelines(CropImageView.Guidelines.ON).
setAspectRatio(1,1)
.start(this);
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
mImageUri = result.getUri();
}
else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
save_changes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = user_name.getText().toString();
StorageReference filepath = mStorage.child("photos").child(mAuth.getCurrentUser().getUid()).child(mImageUri.getLastPathSegment());
filepath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
downloadUri = taskSnapshot.getDownloadUrl();
Picasso.with(UserProfile.this).load(downloadUri).into(userImage);
Toast.makeText(UserProfile.this,"התמונה עלתה בהצלחה",Toast.LENGTH_SHORT).show();
}
});
}
});
}
}
如果即時通訊使用Uri從firebase存儲中檢索圖像?我能做什麼? – Nauruto
然後你有請求firebase,然後從他們的圖像。但是你甚至可以保存位圖。 –
林不太熟悉位圖..你可以看看我的代碼,並解釋我需要做什麼改變? – Nauruto