project 13: Image and Video upload Firebase Realtime database and firebase storage with fetch data in android studio

 To upload a video file along with its thumbnail image to Firebase Storage and store their metadata in the Firebase Realtime Database in Android Studio, you can follow these general steps:


Set up Firebase in your Android Studio project by adding the Firebase SDK to your project and configuring your Firebase project settings.

Add the Firebase Storage and Firebase Realtime Database dependencies to your app's build.gradle file.


step 1:-

dependencies {
    // Firebase Storage
    implementation 'com.google.firebase:firebase-storage:19.2.0'
    // Firebase Realtime Database
    implementation 'com.google.firebase:firebase-database:20.0.1'
}
UPLLOAD DATA---
step 2:- activity_upload.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".UploadActivity">


<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginEnd="5dp">

<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="submit" />
</androidx.appcompat.widget.Toolbar>

<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">


<RelativeLayout
android:id="@+id/rls"
android:layout_width="match_parent"
android:layout_height="match_parent">


<RelativeLayout
android:id="@+id/rlss"
android:layout_width="match_parent"
android:layout_height="match_parent">

<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="200dp" />
</RelativeLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/rlss"
android:orientation="vertical">

<EditText
android:id="@+id/edtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@drawable/edt_bg"
android:hint="Title"
android:padding="10dp"
android:textColor="@color/black" />

<EditText
android:id="@+id/edtDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@drawable/edt_bg"
android:hint="Description"
android:padding="10dp"
android:textColor="@color/black" />

<Button
android:id="@+id/btnVideoThumbnail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:padding="10dp"
android:text="select video and thumbnail" />

<ImageView
android:id="@+id/imgThumbnail"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="fitXY"
android:visibility="visible" />

</LinearLayout>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progress"
android:visibility="gone"
android:layout_centerInParent="true"/>

</RelativeLayout>


</androidx.core.widget.NestedScrollView>




</LinearLayout>

step 3:- UploadActivity.java
package com.sandhya.movieappcreate;

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

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.widget.VideoView;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;

import java.util.HashMap;
import java.util.UUID;

public class UploadActivity extends AppCompatActivity {

StorageReference storage;
StorageReference storageVideo,storageThumb;
DatabaseReference reference;

VideoView videoView;
ImageView imgThumbnail;
EditText edtTitle,edtDescription;
Button btnVideoThumbnail,btnSubmit;
Uri thumbnailUri,videoUri;

ProgressBar progressBar;

static final int REQUEST_CODE_GALLERY = 100;
static final int REQUEST_CODE_VIDEO = 101;


@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);

getSupportActionBar().hide();

// firebase and storage init
storage = FirebaseStorage.getInstance().getReference().child("Contents");
reference = FirebaseDatabase.getInstance().getReference().child("Contents");

videoView = findViewById(R.id.videoView);
imgThumbnail = findViewById(R.id.imgThumbnail);
progressBar = findViewById(R.id.progress);
progressBar.setVisibility(View.GONE);
//video upload method
initSubmit();
//video and image show method
initVideoThumb();
}

private void initSubmit() {
edtTitle = findViewById(R.id.edtTitle);
edtDescription = findViewById(R.id.edtDescription);
btnSubmit = findViewById(R.id.btnSubmit);

//progressDialog show
ProgressDialog progressDialog = new ProgressDialog(UploadActivity.this);
progressDialog.setTitle("New Channel");
progressDialog.setMessage("Creating...");
progressDialog.setCanceledOnTouchOutside(false);

btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String title = edtTitle.getText().toString();
String description = edtDescription.getText().toString();
if (videoUri!=null&&thumbnailUri!=null&&!title.isEmpty()&&!description.isEmpty()){
progressDialog.show();

//video upload
storageVideo = storage.child(System.currentTimeMillis()+"."+ MimeTypeMap.getSingleton().getExtensionFromMimeType(getContentResolver().getType(videoUri)));

//storage
storageVideo.putFile(videoUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
storageThumb =storage.child(System.currentTimeMillis()+"."+ MimeTypeMap.getSingleton().getExtensionFromMimeType(getContentResolver().getType(thumbnailUri)));

//thumbnail upload
storageThumb.putFile(thumbnailUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

storageThumb.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri thumb) {

storageVideo.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri video) {
HashMap<String, Object> map = new HashMap<>();
map.put("thumb",thumb.toString());
map.put("video",video.toString());
map.put("title",title);
map.put("description",description);
reference.child(title).setValue(map).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
progressDialog.dismiss();
Toast.makeText(UploadActivity.this, "send", Toast.LENGTH_SHORT).show();
}else {
progressDialog.dismiss();
Toast.makeText(UploadActivity.this, "Error"+task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
});
}
});

}
});

}else {
Toast.makeText(UploadActivity.this, "error", Toast.LENGTH_SHORT).show();
}
}
});

}
//bottom dialog gallery and video id find
private void initVideoThumb(){
btnVideoThumbnail = findViewById(R.id.btnVideoThumbnail);
btnVideoThumbnail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
BottomSheetDialog bottomDialog = new BottomSheetDialog(UploadActivity.this);
bottomDialog.setContentView(R.layout.bottom_dialog);
bottomDialog.setCancelable(true);
bottomDialog.setCanceledOnTouchOutside(false);
bottomDialog.show();

ImageView imgClose;
LinearLayout llGallery,llVideo;
imgClose = bottomDialog.findViewById(R.id.imgClose);
llGallery = bottomDialog.findViewById(R.id.llGallery);
llVideo = bottomDialog.findViewById(R.id.llVideo);
imgClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bottomDialog.dismiss();
}
});

//open gallery file
llGallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent iGallery = new Intent();
iGallery.setType("image/*");
iGallery.setAction(Intent.ACTION_GET_CONTENT);
imgThumbnail.setVisibility(View.VISIBLE);
startActivityForResult(Intent.createChooser(iGallery, "Select image"), REQUEST_CODE_GALLERY);
bottomDialog.dismiss();
}
});
//open video file
llVideo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent iVideo = new Intent();
iVideo.setType("video/*");
iVideo.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(iVideo, "Select Video"), REQUEST_CODE_VIDEO);
bottomDialog.dismiss();
}
});

}
});
}

//video and image uri find
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==RESULT_OK) {
//thumbnail uri
if (requestCode == REQUEST_CODE_GALLERY) {
thumbnailUri = data.getData();
imgThumbnail.setImageURI(thumbnailUri);

//video uri
} else if (requestCode == REQUEST_CODE_VIDEO) {
videoUri = data.getData();
videoView.setVideoURI(videoUri);
videoView.start();
MediaController controller = new MediaController(UploadActivity.this);
controller.setMediaPlayer(videoView);
videoView.setMediaController(controller);
}
}
}
}

RECEIVE DATA---
step 4:- ContentModel.java
package com.sandhya.movieappcreate.model;

public class ContentModel {
String id,title,description,video,thumb;

public ContentModel() {
}

public ContentModel(String id, String title, String description, String video, String thumb) {
this.id = id;
this.title = title;
this.description = description;
this.video = video;
this.thumb = thumb;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getVideo() {
return video;
}

public void setVideo(String video) {
this.video = video;
}

public String getThumb() {
return thumb;
}

public void setThumb(String thumb) {
this.thumb = thumb;
}
}

step 5:- video_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_height="wrap_content">

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
app:cardCornerRadius="5dp"
android:layout_margin="5dp"
android:layout_height="wrap_content">

<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">

<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="fitXY"
android:id="@+id/imgThumb"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="2"
android:ellipsize="end"
android:layout_margin="5dp"
android:textSize="18sp"
android:textColor="@color/black"
android:text="@string/app_name"
android:id="@+id/txtTitle"/>
</LinearLayout>

</androidx.cardview.widget.CardView>




</LinearLayout>

step 6:- activity_video.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".VideoActivity">

<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/videoRecycler"/>

</RelativeLayout>

step 7:- ContentAdapter.java
package com.sandhya.movieappcreate.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
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.sandhya.movieappcreate.FullActivity;
import com.sandhya.movieappcreate.R;
import com.sandhya.movieappcreate.model.ContentModel;

import java.util.ArrayList;

public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ViewHolder> {

Context context;
ArrayList<ContentModel> models;
DatabaseReference databaseReference;
VideoView videoView;

public ContentAdapter(Context context, ArrayList<ContentModel> models) {
this.context = context;
this.models = models;
}

@NonNull
@Override
public ContentAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.video_item,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull ContentAdapter.ViewHolder holder, int position) {
ContentModel model = models.get(position);
holder.txtTitle.setText(model.getTitle());
Glide.with(context).load(model.getThumb()).placeholder(R.drawable.ic_launcher_background).into(holder.imgThumb);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, FullActivity.class);
intent.putExtra("title",model.getTitle());
intent.putExtra("description",model.getDescription());
intent.putExtra("video",model.getVideo());
context.startActivity(intent);
}
});


}
//video recycler video adapter 2
private void getVideoUrl() {

}

@Override
public int getItemCount() {
return models.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imgThumb;
TextView txtTitle;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imgThumb = itemView.findViewById(R.id.imgThumb);
txtTitle = itemView.findViewById(R.id.txtTitle);
}
}
}

step 2:- VideoActivity.java
package com.sandhya.movieappcreate;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.os.Bundle;

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.sandhya.movieappcreate.adapter.ContentAdapter;
import com.sandhya.movieappcreate.model.ContentModel;

import java.util.ArrayList;

public class VideoActivity extends AppCompatActivity {

RecyclerView videoRecycler;
DatabaseReference reference;
ArrayList<ContentModel> arrModels;
ContentAdapter adapter;
ContentModel contentModel;

@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);

videoRecycler = findViewById(R.id.videoRecycler);
videoRecycler.setLayoutManager(new LinearLayoutManager(this));
arrModels = new ArrayList<>();
reference = FirebaseDatabase.getInstance().getReference().child("Contents");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot :snapshot.getChildren()){
contentModel = dataSnapshot.getValue(ContentModel.class);
arrModels.add(contentModel);
}
adapter = new ContentAdapter(VideoActivity.this,arrModels);
videoRecycler.setAdapter(adapter);
adapter.notifyDataSetChanged();
}

@Override
public void onCancelled(@NonNull DatabaseError error) {

}
});

}
}






Post a Comment

Post a Comment (0)

Previous Post Next Post