|
1 | 1 | package com.dragosholban.myinstagramapp; |
2 | 2 |
|
| 3 | +import android.Manifest; |
| 4 | +import android.content.Intent; |
| 5 | +import android.content.pm.PackageManager; |
| 6 | +import android.net.Uri; |
| 7 | +import android.support.annotation.NonNull; |
| 8 | +import android.support.v4.app.ActivityCompat; |
| 9 | +import android.support.v4.content.ContextCompat; |
3 | 10 | import android.support.v7.app.AppCompatActivity; |
4 | 11 | import android.os.Bundle; |
5 | 12 | import android.view.View; |
| 13 | +import android.widget.Toast; |
| 14 | + |
| 15 | +import com.google.android.gms.tasks.OnFailureListener; |
| 16 | +import com.google.android.gms.tasks.OnSuccessListener; |
| 17 | +import com.google.firebase.auth.FirebaseAuth; |
| 18 | +import com.google.firebase.auth.FirebaseUser; |
| 19 | +import com.google.firebase.storage.FirebaseStorage; |
| 20 | +import com.google.firebase.storage.StorageReference; |
| 21 | +import com.google.firebase.storage.UploadTask; |
| 22 | + |
| 23 | +import java.text.SimpleDateFormat; |
| 24 | +import java.util.Date; |
6 | 25 |
|
7 | 26 | public class FeedActivity extends AppCompatActivity { |
8 | 27 |
|
| 28 | + FirebaseUser fbUser; |
| 29 | + |
| 30 | + static final int RC_PERMISSION_READ_EXTERNAL_STORAGE = 1; |
| 31 | + static final int RC_IMAGE_GALLERY = 2; |
| 32 | + |
9 | 33 | @Override |
10 | 34 | protected void onCreate(Bundle savedInstanceState) { |
11 | 35 | super.onCreate(savedInstanceState); |
12 | 36 | setContentView(R.layout.activity_feed); |
| 37 | + |
| 38 | + fbUser = FirebaseAuth.getInstance().getCurrentUser(); |
| 39 | + if (fbUser == null) { |
| 40 | + finish(); |
| 41 | + } |
13 | 42 | } |
14 | 43 |
|
15 | 44 | public void uploadImage(View view) { |
| 45 | + if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { |
| 46 | + ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, RC_PERMISSION_READ_EXTERNAL_STORAGE); |
| 47 | + } else { |
| 48 | + Intent intent = new Intent(Intent.ACTION_GET_CONTENT); |
| 49 | + intent.setType("image/*"); |
| 50 | + startActivityForResult(intent, RC_IMAGE_GALLERY); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { |
| 56 | + if (requestCode == RC_PERMISSION_READ_EXTERNAL_STORAGE) { |
| 57 | + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { |
| 58 | + Intent intent = new Intent(Intent.ACTION_GET_CONTENT); |
| 59 | + intent.setType("image/*"); |
| 60 | + startActivityForResult(intent, RC_IMAGE_GALLERY); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 67 | + if (requestCode == RC_IMAGE_GALLERY && resultCode == RESULT_OK) { |
| 68 | + Uri uri = data.getData(); |
| 69 | + |
| 70 | + StorageReference storageRef = FirebaseStorage.getInstance().getReference(); |
| 71 | + StorageReference imagesRef = storageRef.child("images"); |
| 72 | + StorageReference userRef = imagesRef.child(fbUser.getUid()); |
| 73 | + String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); |
| 74 | + String filename = fbUser.getUid() + "_" + timeStamp; |
| 75 | + StorageReference fileRef = userRef.child(filename); |
16 | 76 |
|
| 77 | + UploadTask uploadTask = fileRef.putFile(uri); |
| 78 | + uploadTask.addOnFailureListener(new OnFailureListener() { |
| 79 | + @Override |
| 80 | + public void onFailure(@NonNull Exception exception) { |
| 81 | + // Handle unsuccessful uploads |
| 82 | + Toast.makeText(FeedActivity.this, "Upload failed!\n" + exception.getMessage(), Toast.LENGTH_LONG).show(); |
| 83 | + } |
| 84 | + }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { |
| 85 | + @Override |
| 86 | + public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { |
| 87 | + Uri downloadUrl = taskSnapshot.getDownloadUrl(); |
| 88 | + Toast.makeText(FeedActivity.this, "Upload finished!", Toast.LENGTH_SHORT).show(); |
| 89 | + } |
| 90 | + }); |
| 91 | + } |
17 | 92 | } |
18 | 93 | } |
0 commit comments