Skip to content

Commit

Permalink
Changed Firebase query and Checked if media null
Browse files Browse the repository at this point in the history
  • Loading branch information
Manan Dhiman committed Jan 29, 2023
1 parent 9c60dfe commit e65546b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 57 deletions.
47 changes: 14 additions & 33 deletions app/src/main/java/com/manandhiman/sdcproject/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
package com.manandhiman.sdcproject;

import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.MediaController;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.manandhiman.sdcproject.databinding.ActivityMainBinding;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

Expand Down Expand Up @@ -102,6 +94,17 @@ else if(requestCode == 101){
}

private void uploadMedia() {
if(uri == null){
Toast.makeText(this, "Please Select a File to Upload", Toast.LENGTH_SHORT).show();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
finish();
startActivity(getIntent());
return;
}

progressDialog.setTitle("Uploading");
progressDialog.setMessage("Please wait while we upload the selected media");
Expand All @@ -118,20 +121,10 @@ private void uploadMedia() {
post.setNote(binding.editText.getText().toString());
post.setUrl(fileName);

databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
databaseReference.child(String.valueOf(System.currentTimeMillis()/1000)).setValue(post);
}
databaseReference.child(String.valueOf(System.currentTimeMillis()/1000)).setValue(post);

@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(MainActivity.this,"Failed to Upload",Toast.LENGTH_SHORT).show();
}
});

storageReference.putFile(uri).addOnSuccessListener(taskSnapshot -> {
uri = null;
binding.imageView.setImageURI(null);
binding.videoView.setVideoURI(null);
Toast.makeText(MainActivity.this,"File Uploaded Successfully", Toast.LENGTH_SHORT).show();
Expand All @@ -141,6 +134,7 @@ public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(MainActivity.this,"Failed to Upload",Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
});
uri = null;
}

private void hideAllViews(){
Expand All @@ -150,17 +144,4 @@ private void hideAllViews(){
binding.editText.setVisibility(View.GONE);
}

public static String getMimeType(Context context, Uri uri) {
String extension;

if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
final MimeTypeMap mime = MimeTypeMap.getSingleton();
extension = mime.getExtensionFromMimeType(context.getContentResolver().getType(uri));
} else {
extension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(uri.getPath())).toString());
}

return extension;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

public class UploadedMediaActivity extends AppCompatActivity {

StorageReference storageReference = FirebaseStorage.getInstance().getReference("media/");
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("notes");
ActivityUploadedMediaBinding binding;
public static ArrayList<Post> postsArrayList = new ArrayList<>();
Expand All @@ -36,24 +35,16 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(binding.getRoot());

loadActivity();

}

private void loadActivity() {

binding.recyclerView.setHasFixedSize(true);
binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));

//logExistingMedia();
logExistingDBEntries();

}

// @Override
// protected void onResume() {
// super.onResume();
// loadActivity();
// }

private void logExistingDBEntries() {
postsArrayList.clear();
Expand All @@ -62,8 +53,6 @@ private void logExistingDBEntries() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot childDataSnapshot : snapshot.getChildren()) {
// Log.v("TAG Key",""+ childDataSnapshot.getKey());
// Log.v("TAG value",""+ childDataSnapshot.getValue());

Map<String, Object> map = (HashMap<String, Object>) childDataSnapshot.getValue();
if (map==null){
Expand Down Expand Up @@ -91,17 +80,4 @@ public void onCancelled(@NonNull DatabaseError error) {
});
}

private void logExistingMedia() {
storageReference.listAll().addOnSuccessListener(listResult -> {
for(StorageReference prefix: listResult.getPrefixes()){
Log.v("listresult prefix", String.valueOf(prefix));
}
for(StorageReference item: listResult.getItems()){
Log.v("listresult item", String.valueOf(item));
}
})
.addOnFailureListener(e -> Toast.makeText(UploadedMediaActivity.this, "Some Error Occured", Toast.LENGTH_SHORT).show());
}


}

0 comments on commit e65546b

Please sign in to comment.