#android
#Android
Вопрос:
после загрузки видео в хранилище firebase я не могу получить URI загрузки, и это ошибка
com.google.android.gms.tasks.RuntimeExecutionException: com.google.firebase.storage.StorageException: An unknown error occurred, please check the HTTP result code and inner exception for server response.
Я хочу исправить эту ошибку, чтобы загрузить этот URI в базу данных
мой код:
java
package com.example.rami_.esmatsongs;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
import com.google.android.gms.tasks.Continuation;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.FieldValue;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.SetOptions;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class AddPost extends AppCompatActivity {
private EditText titleEdt;
private EditText descriptionEdt;
private VideoView videoView;
private ImageView addVideo;
private Button share;
private FirebaseFirestore firebaseFirestore;
private FirebaseStorage storage;
private StorageReference storageRef;
private Uri videoUri = null;
private String title;
private TextView test;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_post);
firebaseFirestore = FirebaseFirestore.getInstance();
storage =FirebaseStorage.getInstance();
//storageRef = storage.getReference();
storageRef = FirebaseStorage.getInstance().getReference();
titleEdt= (EditText) findViewById(R.id.addTitle);
descriptionEdt = (EditText) findViewById(R.id.addDescription);
videoView = (VideoView) findViewById(R.id.addVideo);
addVideo = (ImageView) findViewById(R.id.addnewVideo);
share = (Button) findViewById(R.id.addPostBtn);
test = (TextView) findViewById(R.id.textView4);
// add video from gallery
addVideo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 0);
}
});
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
title = titleEdt.getText().toString().trim();
// check title
if(TextUtils.isEmpty(title))
{
Toast.makeText(AddPost.this,"Please Enter title",Toast.LENGTH_LONG).show();
}
// check if user add video or not
else if(videoUri == null)
{
Toast.makeText(AddPost.this,"Please Add Video",Toast.LENGTH_LONG).show();
}
else
{
// get id
String id= UUID.randomUUID().toString();
Map<String , Object> postMap = new HashMap<>();
postMap.put("Description" , descriptionEdt.getText().toString().trim());
postMap.put( "id", id);
postMap.put("likes", 0);
postMap.put("timestamp", FieldValue.serverTimestamp());
postMap.put("title", titleEdt.getText().toString().trim());
postMap.put("uri", videoUri "");
// add post to firebase and go to main
firebaseFirestore.collection("Posts").document(id).set(postMap, SetOptions.merge()).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful())
{
Intent intent = new Intent(AddPost.this, MainActivity.class);
startActivity(intent);
finish();
}
else
Toast.makeText(AddPost.this, task.getException().toString(), Toast.LENGTH_LONG).show();
}
});
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// get video uri and set in videoView
if(resultCode == RESULT_OK){
videoUri = data.getData();
try{
videoView.setVisibility(View.VISIBLE);
videoView.setVideoURI(videoUri);
MediaController mediacontroller = new MediaController(AddPost.this);
mediacontroller.setAnchorView(videoView);
videoView.setMediaController(mediacontroller);
// add video to firebase storage
String randomName = UUID.randomUUID().toString();
StorageReference filePath = storageRef.child("videos").child(randomName ".mp4");
UploadTask uploadTask = filePath.putFile(Uri.parse(videoUri ""));
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
test.setText(storageRef.getDownloadUrl() "");
// Continue with the task to get the download URL
return storageRef.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
Uri downloadUri = task.getResult();
videoUri = downloadUri;
}
});
}catch(Exception e){
e.printStackTrace();
}
}
}
}
Это должно вернуть URI загрузки.
Ответ №1:
я выполнил свою задачу таким образом:-
File file =new File(mediaPath);
if(file.exists()) {
UploadTask uploadTask = mReference.putFile(Uri.fromFile(file),metadata);
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
// Continue with the task to get the download URL
return mReference.getDownloadUrl();
}
}).addOnCompleteListener(task -> {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
getUploadedMediaPath.getUploadedFileUrl(downloadUri.toString());
}
});
}