我已在Firebase
上存储了一些图片,当我尝试下载这些图片时,我收到了以下错误:E/StorageException: StorageException has occurred. User does not have permission to access this object. Code: -13021 HttpResult: 403
。我也在使用Google身份验证。
以下是我上传图片的方式:
StorageReference storageReference = firebaseStorage.getReferenceFromUrl("gs://appname-e2a32.appspot.com").child("hImage");
UploadTask uploadTask = storageReference.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Toast.makeText(getBaseContext(), exception.getMessage(), Toast.LENGTH_SHORT).show();
// Handle unsuccessful uploads
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
Uri downloadUrl = taskSnapshot.getDownloadUrl();
savingHelpRequest.dismiss();
Toast.makeText(getBaseContext(), "image uploaded", Toast.LENGTH_SHORT).show();
}
});
以下是我尝试下载的方式:
FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
storageReference = firebaseStorage.getReferenceFromUrl("gs://appname-e2a32.appspot.com").child("hImage");
storageReference.getBytes(Long.MAX_VALUE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
// Use the bytes to display the image
ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
bytes = baoStream.toByteArray();
bmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
Toast.makeText(getBaseContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
}
});
以下是安全规则:
service firebase.storage {
match /b/appname-e2a32.appspot.com/o {
match /{allPaths=**} {
allow read, write;
}
}
}
请让我知道如何摆脱此错误并成功下载图片?
答案 0 :(得分:0)
在Android 6.0或API 23中,我们必须从用户那里获得运行时权限,仅在清单中声明权限是不够的。
public class SampleActivity extends AppCompatActivity {
private static final int REQUEST_RUNTIME_PERMISSION = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
checkPremission();
//SomeTask(); which need permission will become like below in API 23
}
void checkPremission() {
//select which permission you want
final String permission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
if (ContextCompat.checkSelfPermission(SampleActivity.this, permission)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(SampleActivity.this, permission)) {
} else {
ActivityCompat.requestPermissions(SampleActivity.this, new String[]{permission}, REQUEST_RUNTIME_PERMISSION);
}
} else {
// you have permission go ahead launch service
SomeTask();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_RUNTIME_PERMISSION:
final int numOfRequest = grantResults.length;
final boolean isGranted = numOfRequest == 1
&& PackageManager.PERMISSION_GRANTED == grantResults[numOfRequest - 1];
if (isGranted) {
// you have permission go ahead
SomeTask();
} else {
// you dont have permission show toast
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
void SomeTask() {
FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
storageReference = firebaseStorage.getReferenceFromUrl("gs://appname-e2a32.appspot.com");
storageReference.getBytes(Long.MAX_VALUE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
// Use the bytes to display the image
ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
bytes = baoStream.toByteArray();
bmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
Toast.makeText(getBaseContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}