How to add Video(Part-1) in FirebaseRecyclerAdapter from Galllary..
Step:-1
First add below permission in menifest.xml
↓
Add below code AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.firstproject">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<application
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.firstproject">//Below activity name differ , as you type activity name during creating//Below activity code is automatic generated
<activity android:name=".video_Player"/>
<activity android:name=".videos" />
<activity android:name=".dashboardactivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
< uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
↓↓
Type below code in Application in Menifest.xml
android:hardwareAccelerated="true"
↓
Add dependency in app level Gradle
plugins {
id 'com.android.application'
id 'com.google.gms.google-services'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.phoneauthentication"
minSdkVersion 19
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
testImplementation 'junit:junit:'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation platform('com.google.firebase:firebase-bom:26.1.0')
implementation 'com.karumi:dexter:6.2.2'
implementation 'com.google.firebase:firebase-storage:20.0.0'
implementation 'com.google.firebase:firebase-messaging:22.0.0'
implementation 'com.firebaseui:firebase-ui-database:6.2.1'
}
↓
add below code in string.xml
<resources>
Tweet to @androidguru9211
<string name="add_song">add song</string>
<string name="enter_video_name">Enter video name</string>
<string name="browse_video">Browse video</string>
<string name="upload_video">Upload video</string>
</resources>
Step:-2
Click on res
↓
↓
Type menu name in the File name and select menu in Resource type and click ok
Then add below code to the menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/add_videos"
android:icon="@drawable/ic_baseline_add_circle_outline_24"
android:title="@string/add_song"
app:showAsAction="always"
/>
</menu>Step:-3
Right click on drawable and click on Vector assest
↓
Click on Clip art and search add and select then clickclick next and finish↓Step :-4
Add below code in addvideo.xml
Note:- You can replace addvideo with dashboardactivity
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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"
tools:context=".addvideo
">
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="346dp" />
<EditText
android:layout_width="match_parent"
android:layout_below="@id/videoView"
android:maxLines="2"
android:hint="@string/enter_video_name"
android:labelFor="@id/videoView"
android:id="@+id/vsongname"
android:layout_height="40dp"
android:autofillHints="My video"
android:inputType="textLongMessage" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/browse_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/videoView"
android:layout_alignParentStart="true"
android:layout_marginTop="101dp"
android:background="@color/black"
android:gravity="center"
android:text="@string/browse_video"
android:textColor="@color/white"
android:textSize="20sp" />
<Button
android:id="@+id/upload_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/browse_button"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="0dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="-3dp"
android:background="@color/black"
android:gravity="center"
android:text="@string/upload_video"
android:textColor="@color/white"
android:textSize="20sp" />
</RelativeLayout>Step:-5
Add below code in addvideo.java
Note:- You can type projectname replace phoneauthentication during creating project
package com.example.projectname;
import android.Manifest;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.database.Cursor;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.webkit.MimeTypeMap;
import android.widget.Button;
import android.widget.EditText;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.iid.FirebaseInstanceIdReceiver;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.karumi.dexter.Dexter;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.single.PermissionListener;
import com.squareup.okhttp.OkHttpClient;
import java.io.File;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
public class addvideo extends AppCompatActivity {
VideoView videoView;
Button browse, upload;
Uri videourl;
int second;
TextView msize;
String FCM_MESSAGE_URL;
EditText vsongname,msongtype;
MediaController mediaController;
boolean checkedpermission = true;
StorageReference storageReference;
DatabaseReference databaseReference;//ActivityResultLauncher userd for pick video ,image etc
ActivityResultLauncher<String> mGetContent = registerForActivityResult(new ActivityResultContracts.GetContent(), new ActivityResultCallback<Uri>() {
@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
public void onActivityResult(Uri result) {
videourl = result.normalizeScheme();
Cursor cursor = getApplicationContext().getContentResolver()
.query(videourl, null, null, null, null);
videoView.setVideoURI(videourl );
cursor.moveToFirst();
cursor.close();
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addvideo);
videoView = findViewById(R.id.videoView);
browse = findViewById(R.id.browse_button);
upload = findViewById(R.id.upload_button);
vsongname=findViewById(R.id.vsongname);
storageReference= FirebaseStorage.getInstance().getReference();
databaseReference=FirebaseDatabase.getInstance().getReference("Videos");
mediaController = new MediaController(this);
videoView.setMediaController(mediaController);
videoView.start();
browse.setOnClickListener(v -> Dexter.withContext(getApplicationContext()).withPermission(Manifest.permission.READ_EXTERNAL_STORAGE).withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse permissionGrantedResponse) {
checkedpermission = false;
mGetContent.launch("video/*");
}
@Override
public void onPermissionDenied(PermissionDeniedResponse permissionDeniedResponse) {
checkedpermission = false;
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permissionRequest, PermissionToken permissionToken) {
permissionToken.continuePermissionRequest();
}
}).check());
upload.setOnClickListener(v -> {
if(TextUtils.isEmpty(vsongname.getText().toString().toLowerCase())) {
Toast.makeText(getApplicationContext(), "Please enter your video name", Toast.LENGTH_LONG).show();
}
else if (videourl==null){
Toast.makeText(getApplicationContext(), "Please browse video", Toast.LENGTH_LONG).show();
}
else{
processvideouploading();
}
});
}
public String getExtention(){
MimeTypeMap mimeTypeMap=MimeTypeMap.getSingleton();
return mimeTypeMap.getExtensionFromMimeType(getContentResolver().getType(videourl));
}
public void processvideouploading() {
ProgressDialog pd=new ProgressDialog(this);
pd.setTitle("Video Uploader");
pd.setMessage("Please wait,while your video is uploading");
pd.show();
StorageReference uploader=storageReference.child("Videos/"+System.currentTimeMillis()+"."+getExtention());
uploader.putFile(videourl)
.addOnSuccessListener(taskSnapshot -> uploader.getDownloadUrl().addOnSuccessListener((Uri uri) -> {
// How to create a new java class in android studiosongs obj = new songs(vsongname.getText().toString(), uri.toString().toLowerCase());
databaseReference.child(Objects.requireNonNull(databaseReference.push().getKey())).setValue(obj)
.addOnSuccessListener(Void -> {
pd.dismiss();
Toast.makeText(getApplicationContext(), "Sucessfully uploaded", Toast.LENGTH_LONG).show();
}).addOnFailureListener(e -> {
pd.dismiss();
Toast.makeText(getApplicationContext(), "Failed to upload,please try again", Toast.LENGTH_LONG).show();
});
})).addOnProgressListener(snapshot -> {
double progress = (100.0 * snapshot.getBytesTransferred()) / snapshot.getTotalByteCount();
int currentprogress=(int)progress;
pd.setMessage("Uploaded "+currentprogress+"%"
+" Please wait,while your video is uploading.............");
// pd.setMessage("Please wait,while your video is uploading");
pd.setCanceledOnTouchOutside(false);
});
}
}Step:-6
Create a class songs.java
↓Right click on project name then click java class
public class songs {
String videoname,videourl,sotype;
public songs(String videoname, String videourl) {
this.videoname = videoname;
this.videourl = videourl;
}
public String getVideoname() {
return videoname;
}
public String getVideourl() {
return videourl;
}
songs(){}
}How to create a custom menu in android studio
How to create a new activity in an existing project in android studio
Comments
Post a Comment