【发布时间】:2017-03-03 18:21:49
【问题描述】:
我有一个包含照片的 Firebase 存储。 我正在按名称1、2、3等组织照片...... 我正在尝试获取所有照片的下载 URL,因此将来我会将它们输入到 URL 的 Arraylist 中并使用 Glide 将它们呈现在照片库中(这就是我只寻找 URL 的原因)
我正在寻找一种方法,该方法仅在调用 onSucsess 时才会继续给我 Urls,当调用 onFailure 时(因为没有更多照片)我希望循环结束。
我正在尝试使用 Firebase 的 getDownloadUrl 方法并添加了一个布尔值,当调用 onFailure 时将触发 false。 并增加我从 1 开始的 photoOrder int,这样会改变所选照片的路径。
public class Photopackges extends AppCompatActivity {
public static boolean shouldRun = true;
public static final String TAG = "debug";
public static int photoOrder = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photopackges);
//just normal firebase setup
FirebaseStorage storage = FirebaseStorage.getInstance();
// while shouldRun boolean is true keep going , until it will be defined false
while (shouldRun == true) {
// define my Firebase storage path,which contain one folder called "test" and three photos 1.jpg,2.jpg,3.jpg
// the number of the photo is represented in the photoOrder int.
String storagePath = "test/" + String.valueOf(photoOrder) + ".jpg";
StorageReference ref = storage.getReference().child(storagePath);
try {
ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
// trying to define that if the action was successful , keep on going.
shouldRun = true;
Log.e(TAG, "sucsess! " + uri);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// trying to define that if the action has failed , stop.
shouldRun = false;
Log.e(TAG, "Fail! " + e);
}
});
} catch (Exception e) {
Log.e(TAG, "Error! " + e);
break;
}
//increment the photoOrder so it will go to the next path.
photoOrder++;
Log.e(TAG,"value of photoOrder "+photoOrder);
}
}
}
日志 -
我不认为他在得到答案之前发送了更多请求。 我只需要他在事情发生时停下来
StorageException:该位置不存在对象。
我猜一定有一种更简单的方法可以使用 firebase 获取所有存储空间。
感谢您的帮助!
【问题讨论】:
标签: android multithreading firebase while-loop firebase-storage