2017-04-17 126 views
-1

我正在製作健身android應用程序,並使用Firebase存儲來存儲用戶的進度圖像。對Firebase數據庫的引用也是使用Firebase存儲中找到的圖片的網址進行的。顯示存儲在Firebase存儲中的Firebase數據庫中的圖像

我試圖獲取的圖像,並將其顯示在GridLayout但我recieveing一個「綁定到錯誤類型」

我顯示使用recyclerView圖像。 我也使用Picasso加載圖像。

照片活動

public class PhotosActivity extends BaseActivity { 

    @BindView(R.id.activity_photos_fab) 
    FloatingActionButton newPhoto; 

    private StorageReference mStorage; 
    private ProgressDialog mProgressDialog; 

    RecyclerView recyclerView; 
    FirebaseRecyclerAdapter adapter; 

    private static final int GALLERY_INTENT = 2; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_photos); 
     ButterKnife.bind(this); 
     mStorage = FirebaseStorage.getInstance().getReference(); 
     mProgressDialog = new ProgressDialog(this); 

     recyclerView = (RecyclerView) findViewById(R.id.activity_photos_recyclerView); 

    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK) { 

      mProgressDialog.setTitle("Uploading.."); 
      mProgressDialog.show(); 

      // Get the URI of the photo 
      Uri uri = data.getData(); 

      StorageReference filepath = mStorage.child("Photos").child(userEmail).child(uri.getLastPathSegment()); 
      Firebase photosReference = new Firebase(Utils.FIRE_BASE_PHOTOS_REFERENCE + userEmail); 

      // Add the photo to the database 
      // if successful then show a toast to say a photo has been added 
      filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { 
       @Override 
       public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 
        Toast.makeText(getApplicationContext(), "You added a photo", Toast.LENGTH_LONG).show(); 
        mProgressDialog.dismiss(); 
       } 
      }); 

      String photoURL = filepath.getDownloadUrl().toString(); 

      bus.post(new PhotoService.AddPhotoRequest(photoURL, userEmail)); 
     } 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     Firebase photosReference = new Firebase(Utils.FIRE_BASE_PHOTOS_REFERENCE + userEmail); 

     Query sortQuery = photosReference.orderByKey(); 

     adapter = new FirebaseRecyclerAdapter<Photo, PhotoViewHolder>(Photo.class, 
      R.layout.list_individual_photo, 
      PhotoViewHolder.class, 
      sortQuery) { 
      @Override 
      protected void populateViewHolder(PhotoViewHolder photoViewHolder, Photo photo, int i) { 
       photoViewHolder.populate(PhotosActivity.this, photo); 
      } 
     }; 

     GridLayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 4); 
     recyclerView.setLayoutManager(layoutManager); 
     recyclerView.setAdapter(adapter); 

    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     adapter.cleanup(); 
    } 

    @OnClick(R.id.activity_photos_fab) 
    public void setNewPhoto() { 
     Intent intent = new Intent(Intent.ACTION_PICK); 
     intent.setType("image/*"); 
     startActivityForResult(intent, GALLERY_INTENT); 
    } 
} 

PhotoViewHolder

public class PhotoViewHolder extends RecyclerView.ViewHolder { 

    @BindView(R.id.list_individual_photo_imageView) 
    ImageView photoImage; 

    @BindView(R.id.list_individual_photo_progressBar) 
    ProgressBar photoProgressBar; 

    public PhotoViewHolder(View itemView) { 
    super(itemView); 
    ButterKnife.bind(this, itemView); 
    } 

    public void populate (Context context, Photo photo) { 
     itemView.setTag(photo); 

    Picasso.with(context).load(photo.getPhotoURl()) 
      .fit() 
      .centerCrop() 
      .into(photoImage, new Callback() { 
       @Override 
       public void onSuccess() { 
        photoProgressBar.setVisibility(View.GONE); 
       } 

       @Override 
       public void onError() { 

       } 
      }); 
    } 
} 

LivePhotoService

public class LivePhotoService extends BaseLiveService { 

    @Subscribe 
    public void getPhotos(final PhotoService.GetPhotoRequest request) { 
     final PhotoService.GetPhotoResponse response = new PhotoService.GetPhotoResponse(); 

     response.valueEventListener = request.firebase.addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       response.photo = dataSnapshot.getValue(Photo.class); 
       if(response.photo != null) { 
        bus.post(response); 
       } 
      } 

      @Override 
      public void onCancelled(FirebaseError firebaseError) { 
       Toast.makeText(application.getApplicationContext(), firebaseError.getMessage(), Toast.LENGTH_LONG).show(); 
      } 
     }); 
    } 
} 

照片對象

public class Photo { 

    private String photoID; 
    private String photoURl; 
    private HashMap<String, Object> photoDate; 
    private String ownerEmail; 

    public Photo(String photoID, String photoURl, HashMap photoDate, String ownerEmail) { 
     this.photoID = photoID; 
     this.photoURl = photoURl; 
     this.photoDate = photoDate; 
     this.ownerEmail = ownerEmail; 
    } 

    public String getPhotoID() { 
     return photoID; 
    } 

    public String getPhotoURl() { 
     return photoURl; 
    } 

    public HashMap getPhotoDate() { 
     return photoDate; 
    } 

    public String getOwnerEmail() { 
     return ownerEmail; 
    } 
} 

回答

0

考慮使用火力地堡的UI的Android庫,讓你加載直接存儲參考圖像的能力。在你的情況下,它會是這個樣子

我不知道是否支持畢加索,但你可以用滑翔 例如:

mStorageRef = FirebaseStorage.getInstance().getReference(); 

Glide.with(this /* context */) 
    .using(new FirebaseImageLoader()) 
    .load(mStorageRef + "/Photos/" + userId) 
    .error(R.drawable.default) 
    .into(imageView); 
+0

我想存儲上傳的,雖然這就是爲什麼該照片的日期我將該URL存儲在數據庫中,然後調用該引用。這仍然有可能直接做到嗎? @IvanMilisavljevic –

+0

不知道我理解你 –

+0

因此,當用戶上傳照片時,我也會給它加上時間戳,以便我可以顯示他們上傳照片的日期和時間。 因此,當用戶從他們的圖庫中添加一張照片時,它會獲取存儲網址和日期,id等並將其存儲到數據庫中,然後適配器會顯示照片及其上傳日期@IvanMilisavljevic –

相關問題