I am working on an app and I am trying to add my images to the storage and image URL in the real-time database but when I m trying to upload it is saying failed. I am not able to understand why it is happening. please look into it and guide me on why it is happening. I am new to android so it is a little but difficult for me to understand things. please look into it. thank you
Main Activity Code
public class MainActivity extends AppCompatActivity {
//widgets
private Button uploadBtn;
private ImageView imageView;
private ProgressBar progressBar;
private DatabaseReference databasereference = FirebaseDatabase.getInstance().getReference("Image");
private StorageReference storagereference = FirebaseStorage.getInstance().getReference();
private Uri imageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uploadBtn = findViewById(R.id.fabUploadImage);
progressBar = findViewById(R.id.progressBar);
imageView = findViewById(R.id.no_image);
progressBar.setVisibility(View.INVISIBLE);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent galleryIntent = new Intent();
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent , 2);
}
});
uploadBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (imageUri != null){
uploadToFirebase(imageUri);
}else{
Toast.makeText(MainActivity.this, "Please Select Image", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode ==2 && resultCode == RESULT_OK && data != null){
imageUri = data.getData();
imageView.setImageURI(imageUri);
}
}
private void uploadToFirebase(Uri uri){
final StorageReference fileRef = storagereference.child(System.currentTimeMillis() "." getFileExtension(uri));
fileRef.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
fileRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Model model = new Model(uri.toString());
String modelId = databasereference.push().getKey();
databasereference.child(modelId).setValue(model);
progressBar.setVisibility(View.INVISIBLE);
Toast.makeText(MainActivity.this, "Uploaded Successfully", Toast.LENGTH_SHORT).show();
imageView.setImageResource(R.drawable.ic_iamge);
}
});
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(@NonNull UploadTask.TaskSnapshot snapshot) {
progressBar.setVisibility(View.VISIBLE);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressBar.setVisibility(View.INVISIBLE);
Toast.makeText(MainActivity.this, "Uploading Failed !!", Toast.LENGTH_SHORT).show();
}
});
}
private String getFileExtension(Uri mUri){
ContentResolver cr = getContentResolver();
MimeTypeMap mime = MimeTypeMap.getSingleton();
return mime.getExtensionFromMimeType(cr.getType(mUri));
}
}
My Model Class
public class Model {
private String imageUrl;
public Model()
{
}
public Model(String imageUrl)
{
this.imageUrl = imageUrl;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
CodePudding user response:
As you have discovered after debugging your code, you should set the permissions to allow uploading objects to the Cloud Storage bucket attached to your project.
You should read the documentation and adapt the security rules, according to your access rights strategy.
Note that it may be interesting, in a test environment, to temporarily allow write access to everybody in order to check that your android code is correct (i.e. it correctly upload files). For that you would use the following rules.
service firebase.storage {
// The {bucket} wildcard indicates we match files in all Cloud Storage buckets
match /b/{bucket}/o {
// Match filename
match /filename {
allow read: if ...;
allow write: if true; // Allow uploading for everybody
}
}
}
Again, do not use such rules in a production environment: They allow everybody to write to your bucket! Unless you app is made to allow every unauthenticated user to upload files...