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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…