我正在创建一个轮询应用程序,并且每个投票都将具有特定主题的关联图像。使用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方法来查看所使用的默认缓存。 –