Firebase上传照片

时间:2017-06-04 12:34:33

标签: android firebase image-uploading firebase-storage

我有一个代码,用于将图像上传到Firebase存储。代码运行良好,但是当我更新Android Studio时,代码存在问题。您能否建议我如何更改代码?

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();

            }
        });
    }
}

感谢您的帮助。

0 个答案:

没有答案