Firebase upload photo

Viewed 85

I have a code for upload an image to Firebase Storage. The code was running well but when I updated Android Studio there is a problem with code. Can you please suggest how should I change the code?

private StorageReference mStorage;
private static final int GALLERY_INTENT = 2;
private ProgressDialog mProgressDialog;
private ImageView imageViewProfilePicturePreview;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mProgressDialog = new ProgressDialog(this);
mDatabase = FirebaseDatabase.getInstance().getReference();
mStorage = FirebaseStorage.getInstance().getReference();
imageViewProfilePicturePreview = (ImageView) findViewById(R.id.imageViewProfilePicturePreview);
}
...
 @Override
public void onClick(View view) {
...
if (view == textViewUploadPhoto) {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        startActivityForResult(intent, GALLERY_INTENT);
    }
}
...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == GALLERY_INTENT && resultCode == RESULT_OK) {
        mProgressDialog.setMessage("Uploading...");
        mProgressDialog.show();

        StorageReference imageRef = mStorage.child(email);
        StorageReference imageImagesRef = mStorage.child("photos/" + email);

        imageRef.getName().equals(imageImagesRef.getName());    // true
        imageRef.getPath().equals(imageImagesRef.getPath());    // false

        Uri uri = data.getData();
        imageViewProfilePicturePreview.setImageURI(uri);

        imageViewProfilePicturePreview.setDrawingCacheEnabled(true);
        imageViewProfilePicturePreview.buildDrawingCache();
        Bitmap bitmap = imageViewProfilePicturePreview.getDrawingCache();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] data2 = baos.toByteArray();

        UploadTask uploadTask = imageImagesRef.putBytes(data2);
        uploadTask.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                Toast.makeText(getApplicationContext(), "Sorry, your image wasn't set, please try again",
                        Toast.LENGTH_LONG).show();
            }
        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                Intent intent = new Intent(getApplicationContext(), MapsActivity.class);
                startActivity(intent);
                Uri downloadUrl = taskSnapshot.getDownloadUrl();
                mProgressDialog.dismiss();
                Picasso.with(getApplicationContext()).load(downloadUrl).fit().centerCrop().into(imageViewProfilePicturePreview);
                Toast.makeText(getApplicationContext(), "Upload done",
                        Toast.LENGTH_SHORT).show();

            }
        });
    }
}

Thanks for all help.

0 Answers
Related