Firebase Storage: User does not have permission to access to this object (Android)
I'm trying to set up a blog app. The idea is that after the user logs in or signs up, they are directed to the setup page where they pick a profile picture and a name. The picture is saved to Firestore Storage and then the details are logged in the Firestore database.
So when the setup page loads, it is suppose to check first whether that user has existing data- picture and name, then lets them change or fill in their details.
Everything works well till the user tries to upload his picture and name. The picture stores in Firestore Storage successfully then proceed to log in Firestore and returns error
User does not have permission to access this object.
I have a this same function in other activities in my app and they all return the same error.
My setup logic is like this :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup);
firebaseAuth = FirebaseAuth.getInstance();
user_id = firebaseAuth.getCurrentUser().getUid();
firebaseFirestore = FirebaseFirestore.getInstance();
storageReference = FirebaseStorage.getInstance().getReference();
setupImage = findViewById(R.id.setup_image);
setupName = findViewById(R.id.setup_name);
setupBtn = findViewById(R.id.setup_btn);
setupProgress = findViewById(R.id.setup_progress);
setupProgress.setVisibility(View.VISIBLE);
setupBtn.setEnabled(false);
firebaseFirestore.collection("Users").document(user_id).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
if (task.getResult().exists()) {
String name = task.getResult().getString("name");
String image = task.getResult().getString("image");
mainImageURI = Uri.parse(image);
setupName.setText(name);
RequestOptions placeholderRequest = new RequestOptions();
placeholderRequest.placeholder(R.drawable.dummy);
Glide.with(Setup.this).setDefaultRequestOptions(placeholderRequest).load(image).into(setupImage);
}
} else {
String error = task.getException().getMessage();
Toast.makeText(Setup.this, "(FIRESTORE Retrieve Error) : " + error, Toast.LENGTH_LONG).show();
}
setupProgress.setVisibility(View.INVISIBLE);
setupBtn.setEnabled(true);
}
});
setupBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String user_name = setupName.getText().toString();
if (!TextUtils.isEmpty(user_name) && mainImageURI != null) {
setupProgress.setVisibility(View.VISIBLE);
if (isChanged) {
user_id = firebaseAuth.getCurrentUser().getUid();
File newImageFile = new File(mainImageURI.getPath());
try {
compressedImageFile = new Compressor(Setup.this)
.setMaxHeight(125)
.setMaxWidth(125)
.setQuality(20)
.compressToBitmap(newImageFile);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
compressedImageFile.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte thumbData = baos.toByteArray();
final UploadTask image_path = storageReference.child("profile_images").child(user_id + ".jpg").putBytes(thumbData);
Task<Uri> urlTask = image_path.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
// Continue with the task to get the download URL
return storageReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
storeFirestore(task, user_name, downloadUri);
} else {
Toast
.makeText
(Setup.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
setupProgress.setVisibility(View.INVISIBLE);
}
}
});
} else {
storeFirestore(null, user_name, null);
}
}else {
Toast.makeText(Setup.this, "Please make sure to put a name and pick a picture", Toast.LENGTH_LONG).show();
}
}
});
setupImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(Setup.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(Setup.this, "Permission Denied", Toast.LENGTH_LONG).show();
ActivityCompat.requestPermissions(Setup.this, new String{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
} else {
BringImagePicker();
}
} else {
BringImagePicker();
}
}
});
}
private void storeFirestore(@NonNull Task<Uri> task, String user_name, Uri downloadUri) {
if (task == null) {
downloadUri = mainImageURI;
}
Map<String, String> userMap = new HashMap<>();
userMap.put("name", user_name);
userMap.put("image", downloadUri.toString());
firebaseFirestore.collection("Users").document(user_id).set(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(Setup.this, "The user Settings are updated.", Toast.LENGTH_LONG).show();
Intent mainIntent = new Intent(Setup.this, MainActivity.class);
startActivity(mainIntent);
finish();
} else {
String error = task.getException().getMessage();
Toast.makeText(Setup.this, "(FIRESTORE Error) : " + error, Toast.LENGTH_LONG).show();
}
setupProgress.setVisibility(View.INVISIBLE);
}
});
}
private void BringImagePicker() {
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1, 1)
.start(Setup.this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
mainImageURI = result.getUri();
setupImage.setImageURI(mainImageURI);
isChanged = true;
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
So the setupButton triggers the upload and the storeInFirestore method is called after picture is stored in Firebase Storage.
I have the rules in Firestore like this
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth.uid != null;
}
}
}
Upon a close look, I find that the file uploads in Firebase Storage but the error is returned when I attempt to get the download Uri. So the else statement
addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
storeFirestore(task, user_name, downloadUri);
} else {
Toast
.makeText
(Setup.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
is what returns the error hence the method to log in firestore is never triggered.
So is there something wrong with how I am trying to get the downloadUri because I am following the Docs.
Also my storage rules are like this
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
add a comment |
I'm trying to set up a blog app. The idea is that after the user logs in or signs up, they are directed to the setup page where they pick a profile picture and a name. The picture is saved to Firestore Storage and then the details are logged in the Firestore database.
So when the setup page loads, it is suppose to check first whether that user has existing data- picture and name, then lets them change or fill in their details.
Everything works well till the user tries to upload his picture and name. The picture stores in Firestore Storage successfully then proceed to log in Firestore and returns error
User does not have permission to access this object.
I have a this same function in other activities in my app and they all return the same error.
My setup logic is like this :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup);
firebaseAuth = FirebaseAuth.getInstance();
user_id = firebaseAuth.getCurrentUser().getUid();
firebaseFirestore = FirebaseFirestore.getInstance();
storageReference = FirebaseStorage.getInstance().getReference();
setupImage = findViewById(R.id.setup_image);
setupName = findViewById(R.id.setup_name);
setupBtn = findViewById(R.id.setup_btn);
setupProgress = findViewById(R.id.setup_progress);
setupProgress.setVisibility(View.VISIBLE);
setupBtn.setEnabled(false);
firebaseFirestore.collection("Users").document(user_id).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
if (task.getResult().exists()) {
String name = task.getResult().getString("name");
String image = task.getResult().getString("image");
mainImageURI = Uri.parse(image);
setupName.setText(name);
RequestOptions placeholderRequest = new RequestOptions();
placeholderRequest.placeholder(R.drawable.dummy);
Glide.with(Setup.this).setDefaultRequestOptions(placeholderRequest).load(image).into(setupImage);
}
} else {
String error = task.getException().getMessage();
Toast.makeText(Setup.this, "(FIRESTORE Retrieve Error) : " + error, Toast.LENGTH_LONG).show();
}
setupProgress.setVisibility(View.INVISIBLE);
setupBtn.setEnabled(true);
}
});
setupBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String user_name = setupName.getText().toString();
if (!TextUtils.isEmpty(user_name) && mainImageURI != null) {
setupProgress.setVisibility(View.VISIBLE);
if (isChanged) {
user_id = firebaseAuth.getCurrentUser().getUid();
File newImageFile = new File(mainImageURI.getPath());
try {
compressedImageFile = new Compressor(Setup.this)
.setMaxHeight(125)
.setMaxWidth(125)
.setQuality(20)
.compressToBitmap(newImageFile);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
compressedImageFile.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte thumbData = baos.toByteArray();
final UploadTask image_path = storageReference.child("profile_images").child(user_id + ".jpg").putBytes(thumbData);
Task<Uri> urlTask = image_path.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
// Continue with the task to get the download URL
return storageReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
storeFirestore(task, user_name, downloadUri);
} else {
Toast
.makeText
(Setup.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
setupProgress.setVisibility(View.INVISIBLE);
}
}
});
} else {
storeFirestore(null, user_name, null);
}
}else {
Toast.makeText(Setup.this, "Please make sure to put a name and pick a picture", Toast.LENGTH_LONG).show();
}
}
});
setupImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(Setup.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(Setup.this, "Permission Denied", Toast.LENGTH_LONG).show();
ActivityCompat.requestPermissions(Setup.this, new String{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
} else {
BringImagePicker();
}
} else {
BringImagePicker();
}
}
});
}
private void storeFirestore(@NonNull Task<Uri> task, String user_name, Uri downloadUri) {
if (task == null) {
downloadUri = mainImageURI;
}
Map<String, String> userMap = new HashMap<>();
userMap.put("name", user_name);
userMap.put("image", downloadUri.toString());
firebaseFirestore.collection("Users").document(user_id).set(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(Setup.this, "The user Settings are updated.", Toast.LENGTH_LONG).show();
Intent mainIntent = new Intent(Setup.this, MainActivity.class);
startActivity(mainIntent);
finish();
} else {
String error = task.getException().getMessage();
Toast.makeText(Setup.this, "(FIRESTORE Error) : " + error, Toast.LENGTH_LONG).show();
}
setupProgress.setVisibility(View.INVISIBLE);
}
});
}
private void BringImagePicker() {
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1, 1)
.start(Setup.this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
mainImageURI = result.getUri();
setupImage.setImageURI(mainImageURI);
isChanged = true;
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
So the setupButton triggers the upload and the storeInFirestore method is called after picture is stored in Firebase Storage.
I have the rules in Firestore like this
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth.uid != null;
}
}
}
Upon a close look, I find that the file uploads in Firebase Storage but the error is returned when I attempt to get the download Uri. So the else statement
addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
storeFirestore(task, user_name, downloadUri);
} else {
Toast
.makeText
(Setup.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
is what returns the error hence the method to log in firestore is never triggered.
So is there something wrong with how I am trying to get the downloadUri because I am following the Docs.
Also my storage rules are like this
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
Are you sure you have the same rules also in Firebase Storage? Because it has its own rules.
– Alex Mamo
Nov 26 '18 at 7:43
The storage rules are fine. They don't give any problems. There are other parts of the app that just uploads files and works fine.
– Moses
Nov 26 '18 at 8:45
In this case, are you sure you are authenticated? What doesFirebaseAuth.getInstance().getCurrentUser().getUid()return?
– Alex Mamo
Nov 26 '18 at 8:51
I am authenticated otherwise the app wouldn't go past the log in screen. Also the user id is what i use to save the profile picture in cloud storage so that doesn't return null. I've been on this for about a week now... It's exasperating
– Moses
Nov 26 '18 at 10:40
add a comment |
I'm trying to set up a blog app. The idea is that after the user logs in or signs up, they are directed to the setup page where they pick a profile picture and a name. The picture is saved to Firestore Storage and then the details are logged in the Firestore database.
So when the setup page loads, it is suppose to check first whether that user has existing data- picture and name, then lets them change or fill in their details.
Everything works well till the user tries to upload his picture and name. The picture stores in Firestore Storage successfully then proceed to log in Firestore and returns error
User does not have permission to access this object.
I have a this same function in other activities in my app and they all return the same error.
My setup logic is like this :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup);
firebaseAuth = FirebaseAuth.getInstance();
user_id = firebaseAuth.getCurrentUser().getUid();
firebaseFirestore = FirebaseFirestore.getInstance();
storageReference = FirebaseStorage.getInstance().getReference();
setupImage = findViewById(R.id.setup_image);
setupName = findViewById(R.id.setup_name);
setupBtn = findViewById(R.id.setup_btn);
setupProgress = findViewById(R.id.setup_progress);
setupProgress.setVisibility(View.VISIBLE);
setupBtn.setEnabled(false);
firebaseFirestore.collection("Users").document(user_id).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
if (task.getResult().exists()) {
String name = task.getResult().getString("name");
String image = task.getResult().getString("image");
mainImageURI = Uri.parse(image);
setupName.setText(name);
RequestOptions placeholderRequest = new RequestOptions();
placeholderRequest.placeholder(R.drawable.dummy);
Glide.with(Setup.this).setDefaultRequestOptions(placeholderRequest).load(image).into(setupImage);
}
} else {
String error = task.getException().getMessage();
Toast.makeText(Setup.this, "(FIRESTORE Retrieve Error) : " + error, Toast.LENGTH_LONG).show();
}
setupProgress.setVisibility(View.INVISIBLE);
setupBtn.setEnabled(true);
}
});
setupBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String user_name = setupName.getText().toString();
if (!TextUtils.isEmpty(user_name) && mainImageURI != null) {
setupProgress.setVisibility(View.VISIBLE);
if (isChanged) {
user_id = firebaseAuth.getCurrentUser().getUid();
File newImageFile = new File(mainImageURI.getPath());
try {
compressedImageFile = new Compressor(Setup.this)
.setMaxHeight(125)
.setMaxWidth(125)
.setQuality(20)
.compressToBitmap(newImageFile);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
compressedImageFile.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte thumbData = baos.toByteArray();
final UploadTask image_path = storageReference.child("profile_images").child(user_id + ".jpg").putBytes(thumbData);
Task<Uri> urlTask = image_path.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
// Continue with the task to get the download URL
return storageReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
storeFirestore(task, user_name, downloadUri);
} else {
Toast
.makeText
(Setup.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
setupProgress.setVisibility(View.INVISIBLE);
}
}
});
} else {
storeFirestore(null, user_name, null);
}
}else {
Toast.makeText(Setup.this, "Please make sure to put a name and pick a picture", Toast.LENGTH_LONG).show();
}
}
});
setupImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(Setup.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(Setup.this, "Permission Denied", Toast.LENGTH_LONG).show();
ActivityCompat.requestPermissions(Setup.this, new String{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
} else {
BringImagePicker();
}
} else {
BringImagePicker();
}
}
});
}
private void storeFirestore(@NonNull Task<Uri> task, String user_name, Uri downloadUri) {
if (task == null) {
downloadUri = mainImageURI;
}
Map<String, String> userMap = new HashMap<>();
userMap.put("name", user_name);
userMap.put("image", downloadUri.toString());
firebaseFirestore.collection("Users").document(user_id).set(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(Setup.this, "The user Settings are updated.", Toast.LENGTH_LONG).show();
Intent mainIntent = new Intent(Setup.this, MainActivity.class);
startActivity(mainIntent);
finish();
} else {
String error = task.getException().getMessage();
Toast.makeText(Setup.this, "(FIRESTORE Error) : " + error, Toast.LENGTH_LONG).show();
}
setupProgress.setVisibility(View.INVISIBLE);
}
});
}
private void BringImagePicker() {
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1, 1)
.start(Setup.this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
mainImageURI = result.getUri();
setupImage.setImageURI(mainImageURI);
isChanged = true;
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
So the setupButton triggers the upload and the storeInFirestore method is called after picture is stored in Firebase Storage.
I have the rules in Firestore like this
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth.uid != null;
}
}
}
Upon a close look, I find that the file uploads in Firebase Storage but the error is returned when I attempt to get the download Uri. So the else statement
addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
storeFirestore(task, user_name, downloadUri);
} else {
Toast
.makeText
(Setup.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
is what returns the error hence the method to log in firestore is never triggered.
So is there something wrong with how I am trying to get the downloadUri because I am following the Docs.
Also my storage rules are like this
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
I'm trying to set up a blog app. The idea is that after the user logs in or signs up, they are directed to the setup page where they pick a profile picture and a name. The picture is saved to Firestore Storage and then the details are logged in the Firestore database.
So when the setup page loads, it is suppose to check first whether that user has existing data- picture and name, then lets them change or fill in their details.
Everything works well till the user tries to upload his picture and name. The picture stores in Firestore Storage successfully then proceed to log in Firestore and returns error
User does not have permission to access this object.
I have a this same function in other activities in my app and they all return the same error.
My setup logic is like this :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup);
firebaseAuth = FirebaseAuth.getInstance();
user_id = firebaseAuth.getCurrentUser().getUid();
firebaseFirestore = FirebaseFirestore.getInstance();
storageReference = FirebaseStorage.getInstance().getReference();
setupImage = findViewById(R.id.setup_image);
setupName = findViewById(R.id.setup_name);
setupBtn = findViewById(R.id.setup_btn);
setupProgress = findViewById(R.id.setup_progress);
setupProgress.setVisibility(View.VISIBLE);
setupBtn.setEnabled(false);
firebaseFirestore.collection("Users").document(user_id).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
if (task.getResult().exists()) {
String name = task.getResult().getString("name");
String image = task.getResult().getString("image");
mainImageURI = Uri.parse(image);
setupName.setText(name);
RequestOptions placeholderRequest = new RequestOptions();
placeholderRequest.placeholder(R.drawable.dummy);
Glide.with(Setup.this).setDefaultRequestOptions(placeholderRequest).load(image).into(setupImage);
}
} else {
String error = task.getException().getMessage();
Toast.makeText(Setup.this, "(FIRESTORE Retrieve Error) : " + error, Toast.LENGTH_LONG).show();
}
setupProgress.setVisibility(View.INVISIBLE);
setupBtn.setEnabled(true);
}
});
setupBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String user_name = setupName.getText().toString();
if (!TextUtils.isEmpty(user_name) && mainImageURI != null) {
setupProgress.setVisibility(View.VISIBLE);
if (isChanged) {
user_id = firebaseAuth.getCurrentUser().getUid();
File newImageFile = new File(mainImageURI.getPath());
try {
compressedImageFile = new Compressor(Setup.this)
.setMaxHeight(125)
.setMaxWidth(125)
.setQuality(20)
.compressToBitmap(newImageFile);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
compressedImageFile.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte thumbData = baos.toByteArray();
final UploadTask image_path = storageReference.child("profile_images").child(user_id + ".jpg").putBytes(thumbData);
Task<Uri> urlTask = image_path.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
// Continue with the task to get the download URL
return storageReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
storeFirestore(task, user_name, downloadUri);
} else {
Toast
.makeText
(Setup.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
setupProgress.setVisibility(View.INVISIBLE);
}
}
});
} else {
storeFirestore(null, user_name, null);
}
}else {
Toast.makeText(Setup.this, "Please make sure to put a name and pick a picture", Toast.LENGTH_LONG).show();
}
}
});
setupImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(Setup.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(Setup.this, "Permission Denied", Toast.LENGTH_LONG).show();
ActivityCompat.requestPermissions(Setup.this, new String{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
} else {
BringImagePicker();
}
} else {
BringImagePicker();
}
}
});
}
private void storeFirestore(@NonNull Task<Uri> task, String user_name, Uri downloadUri) {
if (task == null) {
downloadUri = mainImageURI;
}
Map<String, String> userMap = new HashMap<>();
userMap.put("name", user_name);
userMap.put("image", downloadUri.toString());
firebaseFirestore.collection("Users").document(user_id).set(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(Setup.this, "The user Settings are updated.", Toast.LENGTH_LONG).show();
Intent mainIntent = new Intent(Setup.this, MainActivity.class);
startActivity(mainIntent);
finish();
} else {
String error = task.getException().getMessage();
Toast.makeText(Setup.this, "(FIRESTORE Error) : " + error, Toast.LENGTH_LONG).show();
}
setupProgress.setVisibility(View.INVISIBLE);
}
});
}
private void BringImagePicker() {
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1, 1)
.start(Setup.this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
mainImageURI = result.getUri();
setupImage.setImageURI(mainImageURI);
isChanged = true;
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
So the setupButton triggers the upload and the storeInFirestore method is called after picture is stored in Firebase Storage.
I have the rules in Firestore like this
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth.uid != null;
}
}
}
Upon a close look, I find that the file uploads in Firebase Storage but the error is returned when I attempt to get the download Uri. So the else statement
addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
storeFirestore(task, user_name, downloadUri);
} else {
Toast
.makeText
(Setup.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
is what returns the error hence the method to log in firestore is never triggered.
So is there something wrong with how I am trying to get the downloadUri because I am following the Docs.
Also my storage rules are like this
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
edited Nov 27 '18 at 10:23
Moses
asked Nov 25 '18 at 21:19
MosesMoses
114
114
Are you sure you have the same rules also in Firebase Storage? Because it has its own rules.
– Alex Mamo
Nov 26 '18 at 7:43
The storage rules are fine. They don't give any problems. There are other parts of the app that just uploads files and works fine.
– Moses
Nov 26 '18 at 8:45
In this case, are you sure you are authenticated? What doesFirebaseAuth.getInstance().getCurrentUser().getUid()return?
– Alex Mamo
Nov 26 '18 at 8:51
I am authenticated otherwise the app wouldn't go past the log in screen. Also the user id is what i use to save the profile picture in cloud storage so that doesn't return null. I've been on this for about a week now... It's exasperating
– Moses
Nov 26 '18 at 10:40
add a comment |
Are you sure you have the same rules also in Firebase Storage? Because it has its own rules.
– Alex Mamo
Nov 26 '18 at 7:43
The storage rules are fine. They don't give any problems. There are other parts of the app that just uploads files and works fine.
– Moses
Nov 26 '18 at 8:45
In this case, are you sure you are authenticated? What doesFirebaseAuth.getInstance().getCurrentUser().getUid()return?
– Alex Mamo
Nov 26 '18 at 8:51
I am authenticated otherwise the app wouldn't go past the log in screen. Also the user id is what i use to save the profile picture in cloud storage so that doesn't return null. I've been on this for about a week now... It's exasperating
– Moses
Nov 26 '18 at 10:40
Are you sure you have the same rules also in Firebase Storage? Because it has its own rules.
– Alex Mamo
Nov 26 '18 at 7:43
Are you sure you have the same rules also in Firebase Storage? Because it has its own rules.
– Alex Mamo
Nov 26 '18 at 7:43
The storage rules are fine. They don't give any problems. There are other parts of the app that just uploads files and works fine.
– Moses
Nov 26 '18 at 8:45
The storage rules are fine. They don't give any problems. There are other parts of the app that just uploads files and works fine.
– Moses
Nov 26 '18 at 8:45
In this case, are you sure you are authenticated? What does
FirebaseAuth.getInstance().getCurrentUser().getUid() return?– Alex Mamo
Nov 26 '18 at 8:51
In this case, are you sure you are authenticated? What does
FirebaseAuth.getInstance().getCurrentUser().getUid() return?– Alex Mamo
Nov 26 '18 at 8:51
I am authenticated otherwise the app wouldn't go past the log in screen. Also the user id is what i use to save the profile picture in cloud storage so that doesn't return null. I've been on this for about a week now... It's exasperating
– Moses
Nov 26 '18 at 10:40
I am authenticated otherwise the app wouldn't go past the log in screen. Also the user id is what i use to save the profile picture in cloud storage so that doesn't return null. I've been on this for about a week now... It's exasperating
– Moses
Nov 26 '18 at 10:40
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472090%2ffirebase-storage-user-does-not-have-permission-to-access-to-this-object-androi%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472090%2ffirebase-storage-user-does-not-have-permission-to-access-to-this-object-androi%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you sure you have the same rules also in Firebase Storage? Because it has its own rules.
– Alex Mamo
Nov 26 '18 at 7:43
The storage rules are fine. They don't give any problems. There are other parts of the app that just uploads files and works fine.
– Moses
Nov 26 '18 at 8:45
In this case, are you sure you are authenticated? What does
FirebaseAuth.getInstance().getCurrentUser().getUid()return?– Alex Mamo
Nov 26 '18 at 8:51
I am authenticated otherwise the app wouldn't go past the log in screen. Also the user id is what i use to save the profile picture in cloud storage so that doesn't return null. I've been on this for about a week now... It's exasperating
– Moses
Nov 26 '18 at 10:40