我正在創建一個輪詢應用程序,並且每個投票都將具有特定主題的關聯圖像。使用Picasso優化圖像存儲/從Amazon S3獲取請求
我正在使用Firebase在事件發生時動態更新民意調查。在Firebase中,我存儲了相關的圖像URL(在Amazon S3中引用該URL),然後我使用Picasso將圖像加載到客戶端設備上(請參閱下面的代碼)。
我已經注意到我可能無效地處理了這些數據,導致在S3中對我的Amazon文件發出不必要的Get請求。我想知道我對畢加索有什麼選擇(即我正在考慮一些緩存)來爲每個客戶端拉取圖像一次,並將它們存儲在本地(但我不希望它們永久保留在客戶端設備上)。我的目標是降低成本,但不會影響性能。以下是我當前的代碼:
mPollsRef.child(mCurrentDateString).child(homePollFragmentIndexConvertedToFirebaseReferenceImmediatelyBelowDate).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int numberOfPollAnswersAtIndexBelowDate = (int) dataSnapshot.child("Poll_Answers").getChildrenCount();
Log.e("TAG", "There are " + numberOfPollAnswersAtIndexBelowDate + " polls answers at index " + homePollFragmentIndexConvertedToFirebaseReferenceImmediatelyBelowDate);
addRadioButtonsWithFirebaseAnswers(dataSnapshot, numberOfPollAnswersAtIndexBelowDate);
String pollQuestion = dataSnapshot.child("Poll_Question").getValue().toString();
mPollQuestion.setText(pollQuestion);
//This is where the image "GET" from Amazon S3 using Picasso begins; the URL is in Firebase and then I use that URL
//with the Picasso.load method
final String mImageURL = (String) dataSnapshot.child("Image").getValue();
Picasso.with(getContext())
.load(mImageURL)
.fit()
.into((ImageView) rootView.findViewById(R.id.poll_image));
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
謝謝!自動將OkHttp庫作爲依賴項添加默認緩存? – tccpg288
是的,如果OkHttp可用,將會使用: https://github.com/square/picasso/blob/master/picasso/src/main/java/com/squareup/picasso/OkHttpDownloader.java –
您可以檢查取出上面使用的Utils方法來查看所使用的默認緩存。 –