2017-04-22 44 views
0

我有一個活動,其中包含一個用於顯示用戶照片的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(); 

        } 
       }); 

      } 
     }); 

    } 

} 

回答

0

我還沒有試過,但在選擇圖像分配一個位圖變位從圖庫中的值,你可以做一件事創建位圖類型的靜態變量。 並在您的onStart活動首先檢查該靜態位圖變量是否爲空如果不是這樣將位圖放入您的圖像查看

0

當您從當前活動啓動另一個活動時,您的上一個活動將被銷燬。所以所有的變量和設置爲圖像視圖的圖像也會被垃圾收集。

對於您可以節省內部onSaveInstance()

超越控制下面的方法您的活動中

@Override 
    public void onSaveInstanceState(Bundle savedInstanceState){ 
     super.onSaveInstanceState(savedInstanceState); 
     savedInstanceState.putParcelable("BitmapImage", bitmap); 
    } 

下面寫代碼中的onCreate(),我們可以得到保存的位圖的位圖

if (savedInstanceState != null) { 
     image = savedInstanceState.getParcelable("BitmapImage"); 
     targetImage.setImageBitmap(image); // set the bitmap.  
    } 

如果您有imagePath我們也可以保存並使用相同的方法。

希望這些幫助你。

+0

如果即時通訊使用Uri從firebase存儲中檢索圖像?我能做什麼? – Nauruto

+0

然後你有請求firebase,然後從他們的圖像。但是你甚至可以保存位圖。 –

+0

林不太熟悉位圖..你可以看看我的代碼,並解釋我需要做什麼改變? – Nauruto