|
| 1 | +package com.firebase.uidemo.storage; |
| 2 | + |
| 3 | +import android.Manifest; |
| 4 | +import android.content.Intent; |
| 5 | +import android.net.Uri; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.provider.MediaStore; |
| 8 | +import android.support.annotation.NonNull; |
| 9 | +import android.support.v7.app.AppCompatActivity; |
| 10 | +import android.util.Log; |
| 11 | +import android.view.View; |
| 12 | +import android.widget.Button; |
| 13 | +import android.widget.ImageView; |
| 14 | +import android.widget.Toast; |
| 15 | + |
| 16 | +import com.bumptech.glide.Glide; |
| 17 | +import com.firebase.ui.storage.images.FirebaseImageLoader; |
| 18 | +import com.firebase.uidemo.R; |
| 19 | +import com.google.android.gms.tasks.OnCompleteListener; |
| 20 | +import com.google.android.gms.tasks.OnFailureListener; |
| 21 | +import com.google.android.gms.tasks.OnSuccessListener; |
| 22 | +import com.google.android.gms.tasks.Task; |
| 23 | +import com.google.firebase.auth.AuthResult; |
| 24 | +import com.google.firebase.auth.FirebaseAuth; |
| 25 | +import com.google.firebase.storage.FirebaseStorage; |
| 26 | +import com.google.firebase.storage.StorageReference; |
| 27 | +import com.google.firebase.storage.UploadTask; |
| 28 | + |
| 29 | +import java.util.UUID; |
| 30 | + |
| 31 | +import butterknife.BindView; |
| 32 | +import butterknife.ButterKnife; |
| 33 | +import butterknife.OnClick; |
| 34 | +import pub.devrel.easypermissions.AfterPermissionGranted; |
| 35 | +import pub.devrel.easypermissions.EasyPermissions; |
| 36 | + |
| 37 | +public class ImageActivity extends AppCompatActivity { |
| 38 | + |
| 39 | + private static final String TAG = "ImageDemo"; |
| 40 | + private static final int RC_CHOOSE_PHOTO = 101; |
| 41 | + private static final int RC_IMAGE_PERMS = 102; |
| 42 | + |
| 43 | + private StorageReference mImageRef; |
| 44 | + |
| 45 | + @BindView(R.id.button_choose_photo) |
| 46 | + Button mUploadButton; |
| 47 | + |
| 48 | + @BindView(R.id.button_download_direct) |
| 49 | + Button mDownloadDirectButton; |
| 50 | + |
| 51 | + @BindView(R.id.first_image) |
| 52 | + ImageView mImageView; |
| 53 | + |
| 54 | + @Override |
| 55 | + protected void onCreate(Bundle savedInstanceState) { |
| 56 | + super.onCreate(savedInstanceState); |
| 57 | + setContentView(R.layout.activity_image); |
| 58 | + ButterKnife.bind(this); |
| 59 | + |
| 60 | + // By default, Firebase Storage files require authentication to read or write. |
| 61 | + // For this sample to function correctly, enable Anonymous Auth in the Firebase console: |
| 62 | + // https://console.firebase.google.com/project/_/authentication/providers |
| 63 | + FirebaseAuth.getInstance().signInAnonymously() |
| 64 | + .addOnCompleteListener(new OnCompleteListener<AuthResult>() { |
| 65 | + @Override |
| 66 | + public void onComplete(@NonNull Task<AuthResult> task) { |
| 67 | + Log.d(TAG, "signInAnonymously:" + task.isSuccessful()); |
| 68 | + if (!task.isSuccessful()) { |
| 69 | + Log.w(TAG, "signInAnonymously", task.getException()); |
| 70 | + Log.w(TAG, getString(R.string.anonymous_auth_failed_msg)); |
| 71 | + |
| 72 | + Toast.makeText(ImageActivity.this, |
| 73 | + getString(R.string.anonymous_auth_failed_toast), |
| 74 | + Toast.LENGTH_SHORT).show(); |
| 75 | + } |
| 76 | + } |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 82 | + super.onActivityResult(requestCode, resultCode, data); |
| 83 | + |
| 84 | + if (requestCode == RC_CHOOSE_PHOTO) { |
| 85 | + if (resultCode == RESULT_OK) { |
| 86 | + Uri selectedImage = data.getData(); |
| 87 | + uploadPhoto(selectedImage); |
| 88 | + } else { |
| 89 | + Toast.makeText(this, "No image chosen", Toast.LENGTH_SHORT).show(); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { |
| 96 | + super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
| 97 | + EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this); |
| 98 | + } |
| 99 | + |
| 100 | + @OnClick(R.id.button_choose_photo) |
| 101 | + @AfterPermissionGranted(RC_IMAGE_PERMS) |
| 102 | + protected void choosePhoto() { |
| 103 | + String perm = Manifest.permission.READ_EXTERNAL_STORAGE; |
| 104 | + if (!EasyPermissions.hasPermissions(this, perm)) { |
| 105 | + EasyPermissions.requestPermissions(this, getString(R.string.rational_image_perm), |
| 106 | + RC_IMAGE_PERMS, perm); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); |
| 111 | + startActivityForResult(i, RC_CHOOSE_PHOTO); |
| 112 | + } |
| 113 | + |
| 114 | + protected void uploadPhoto(Uri uri) { |
| 115 | + // Reset UI |
| 116 | + hideDownloadUI(); |
| 117 | + Toast.makeText(this, "Uploading...", Toast.LENGTH_SHORT).show(); |
| 118 | + |
| 119 | + // Upload to Firebase Storage |
| 120 | + String uuid = UUID.randomUUID().toString(); |
| 121 | + mImageRef = FirebaseStorage.getInstance().getReference(uuid); |
| 122 | + mImageRef.putFile(uri) |
| 123 | + .addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() { |
| 124 | + @Override |
| 125 | + public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { |
| 126 | + Log.d(TAG, "uploadPhoto:onSuccess:" + |
| 127 | + taskSnapshot.getMetadata().getReference().getPath()); |
| 128 | + Toast.makeText(ImageActivity.this, "Image uploaded", |
| 129 | + Toast.LENGTH_SHORT).show(); |
| 130 | + |
| 131 | + showDownloadUI(); |
| 132 | + } |
| 133 | + }) |
| 134 | + .addOnFailureListener(this, new OnFailureListener() { |
| 135 | + @Override |
| 136 | + public void onFailure(@NonNull Exception e) { |
| 137 | + Log.w(TAG, "uploadPhoto:onError", e); |
| 138 | + Toast.makeText(ImageActivity.this, "Upload failed", |
| 139 | + Toast.LENGTH_SHORT).show(); |
| 140 | + } |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + @OnClick(R.id.button_download_direct) |
| 145 | + protected void downloadDirect() { |
| 146 | + // Download directly from StorageReference using Glide |
| 147 | + Glide.with(this) |
| 148 | + .using(new FirebaseImageLoader()) |
| 149 | + .load(mImageRef) |
| 150 | + .centerCrop() |
| 151 | + .crossFade() |
| 152 | + .into(mImageView); |
| 153 | + } |
| 154 | + |
| 155 | + private void hideDownloadUI() { |
| 156 | + mDownloadDirectButton.setEnabled(false); |
| 157 | + |
| 158 | + mImageView.setImageResource(0); |
| 159 | + mImageView.setVisibility(View.INVISIBLE); |
| 160 | + } |
| 161 | + |
| 162 | + private void showDownloadUI() { |
| 163 | + mDownloadDirectButton.setEnabled(true); |
| 164 | + |
| 165 | + mImageView.setVisibility(View.VISIBLE); |
| 166 | + } |
| 167 | +} |
0 commit comments