when I initialize mStorageRef = FirebaseStorage.getInstance().getReference("uploads"); mDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");, my app has stopped My anndroid sdk verstion 28; buildToolsVersion '28.0.3'
FATAL EXCEPTION: Failed resolution of: Lcom/google/android/gms/common/internal/zzbq; at com.google.firebase.storage.FirebaseStorage.getInstance(Unknown Source:11)
I don't get what you're doing wrong here, let me write you the whole code to solve your issue.
FirebaseStorage firebaseStorage;
StorageReference storageReference;
private Button btnChoose, btnUpload;
private ImageView imageView;
private Uri filePath;
private final int IMAGE_REQUEST = 71;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
storage = FirebaseStorage.getInstance();
storageReference = storage.getReference();
btnChoose = (Button) findViewById(R.id.btnChoose);
btnUpload = (Button) findViewById(R.id.btnUpload);
imageView = (ImageView) findViewById(R.id.imgView);
btnChoose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chooseImage();
}
});
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadImage();
}
});
}
private void chooseImage() {
//an intent to choose your required image.
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select your photo."), IMAGE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IMAGE_REQUEST && resultCode == RESULT_OK
&& data != null && data.getData() != null) {
filePath = data.getData();
//this will update your imageView
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
} catch (IOException e) {
e.printStackTrace();
}
imageView.setImageBitmap(bitmap);
}
}
private void uploadImage() {
if (filePath != null) {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading your image");
progressDialog.show();
//Put uid of the user to make it easy to get your image back when needed. I would suggest you to use an external
//library to compress the image for best performance.
StorageReference ref = storageReference.child("uploads/" + "PUT_UID_OF_USER");
//method to upload a file.
ref.putFile(filePath)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//if the image is successfully uploaded
progressDialog.dismiss();
Toast.makeText(Activity.this, "Uploaded", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
//if image upload was failed due to some reason.
progressDialog.dismiss();
Toast.makeText(Activity.this, "Failed " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot
.getTotalByteCount());
//this will actually show the progress in the progress dialog.
progressDialog.setMessage("Uploaded " + (int) progress + "%");
}
});
}
}
//use picasso library for this for the best performance/result.
private void getImageBack(String uid_of_user){
StorageReference ref = storageReference.child("uploads/" + "PUT_UID_OF_USER").getImage("PUT_NAME_OF_IMAGE");
load.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
// Got the download URL for 'users/me/profile.png'
// Pass it to Picasso to download, show in ImageView and caching
Picasso.with(Test.this).load(uri.toString()).into(imageView);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
Toast.makeText(Test.this, "failed", Toast.LENGTH_SHORT).show();
}
});
}
Code is untested, errors are possible, you can use my help whenever needed in anything :)
Get picasso library by searching on google, its a very famous library.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.