2017-03-31 44 views
0

我正在尝试测试Firebase存储。我手动上传图片/ egg.jpg。Firebase存储:即使在公开访问时也无法下载文件

我构建了一个简单的Android应用程序,用于测试Firebase指南中的代码以下载图像。只需一个启动程序的按钮即可。

文件无法下载和所有我得到的是从onFailure处(异常),上面写着“用户没有权限访问该对象”。

我看到类似的问题,解决方案是允许通过规则读取和写入任何人,所以我复制它们,你可以看到下面。

service firebase.storage { 
    match /b/savephoto-a1cc3.appspot.com/o { 
    match /{allPaths=**} { 
     // Allow access by all users 
     allow read, write; 
    } 
    } 
} 

这里是activitys代码

private StorageReference pathRef = FirebaseStorage.getInstance().getReference().child("images/egg.jpg"); 
private ImageView imageView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    this.imageView = (ImageView) this.findViewById(R.id.imageView); 
} 

public void getImage(View v){ 
    File localFile; 
    try { 
     localFile = File.createTempFile("images","jpg"); 
     pathRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() { 
      @Override 
      public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) { 
       //Local temp file has been created 
       Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show(); 
      } 
     }).addOnFailureListener(new OnFailureListener() { 
      @Override 
      public void onFailure(@NonNull Exception e) { 
       //handle error 
       Toast.makeText(MainActivity.this, e.getMessage()+"\n"+e.getCause(), Toast.LENGTH_LONG).show(); 
      } 
     }); 
    }catch (IOException exception){ 
     Toast.makeText(this, "IOEXCEPTION", Toast.LENGTH_SHORT).show(); 
    } 
} 

只是可以肯定我还添加了这些权限清单。

<uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 

为什么我不能下载该死的文件?

回答

1

尝试改变:

service firebase.storage { 
    match /b/{bucket}/o { 
    match /{allPaths=**} { 
     allow read, write:if true 
    } 
    } 
} 
+0

这给出了一个错误“错误保存规则 - 地铁5号线:意外‘如果’ ” – Skemelio

+0

我变了,再试一次 – Dmitry

+0

嗯,就是这样!有什么问题? – Skemelio

相关问题