Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

android - How can I upload videos in Firebase Storage?

I'm trying to upload some pictures and videos from the gallery to the Firebase Storage. I tried For the images I had no problems, but for the videos yes. I tried with several tutorial found on google but none worked. How could I do? This is the code I have written so far:

public class GalleryActivity extends AppCompatActivity {

    private Button chooseImage;
    private Button chooseVideo;
    private Button sendToCloud;
    private ImageView selectedImage;
    private VideoView selectedVideo;
    private TextView txtDescription;

    private static final int PICK_IMAGE = 100;
    private static final int PICK_VIDEO = 200;

    private Uri mImageUri;

    //Firebase stuff
    private FirebaseStorage storage;
    private StorageReference storageReference;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);

        txtDescription = findViewById(R.id.diplay);
        selectedImage = findViewById(R.id.image_taken);
        selectedVideo = findViewById(R.id.video_taken);
        chooseImage = findViewById(R.id.imageBtn);
        chooseVideo = findViewById(R.id.videoBtn);
        sendToCloud = findViewById(R.id.send_to_cloud);

        // get the Firebase  storage reference
        storage = FirebaseStorage.getInstance();
        storageReference = storage.getReference();

        chooseImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                chooseImageFromGallery();
            }
        });

        chooseVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                chooseVideoFromGallery();
            }
        });

        sendToCloud.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(selectedImage.getDrawable() != null) {
                    uploadImage();
                }
                //check if videoview is empty and call uploadVideo()
            }
        });
    }


    private void chooseVideoFromGallery() {
        Intent i = new Intent();
        i.setType("video/*");
        i.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(i, "choose App"), PICK_VIDEO);
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && data != null) {
            if (requestCode == PICK_IMAGE) {// img from gallery
                previewImage(data);

            } else if (requestCode == PICK_VIDEO) {
                previewVideo(data);
            }
        }
    }

private void previewVideo() {
        try {
            txtDescription.setVisibility(View.GONE);
            selectedImage.setVisibility(View.GONE);

            selectedVideo.setVisibility(View.VISIBLE);
            selectedVideo.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void previewImage(Intent data) {
        try {
            txtDescription.setVisibility(View.GONE);
            selectedVideo.setVisibility(View.GONE);

            selectedImage.setVisibility(View.VISIBLE);
            Uri imgUri = data.getData();
            InputStream imageStream = GalleryActivity.this.getContentResolver().openInputStream(imgUri);//2
            Bitmap selectedImageBitmap = BitmapFactory.decodeStream(imageStream);//3}
            mImageUri = imgUri;
            selectedImage.setImageBitmap(selectedImageBitmap);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

Thanks in advance to everyone


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is the working code:

if (resultCode == RESULT_OK) {
    if (requestCode == PICK_VIDEO) {
        Uri videoUri= data.getData();
        String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
        StorageReference storageRef = FirebaseStorage.getInstance().getReference();
        String filename = "give a unique filename for each";
        videoRef = storageRef.child("/videos/" + uid+ "/" + filename);
       

        uploadVideo(videoUri);

    }
}

Upload Video here:

private void uploadVideo(Uri videoUri) {

if(videoUri != null){
    UploadTask uploadTask = videoRef.putFile(videoUri);
    uploadTask.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>({
        @Override
        public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
            if(task.isSuccessful())
                Toast.makeText(getContext(), "Video Upload Completed", Toast.LENGTH_SHORT).show();
        }
    }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
            updateProgress(taskSnapshot);
        }
    });
}else {
    Toast.makeText(getContext(), "upload failed!", Toast.LENGTH_SHORT).show();
}

}

// StorageReference videoRef;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...