java - 如何将 mp3 文件发送到 firebase
问题描述
我想将 mp3 文件发送到 firebase 存储,但我的代码不起作用。我没有错误。它只是行不通。
我想将我从手机中选择的声音文件上传到 Firebase 存储。
这是我的代码:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RQS_OPEN_AUDIO_MP3 && resultCode == Activity.RESULT_OK) {
if ((data != null) && (data.getData() != null)) {
Uri mp3FileUri = data.getData();
Toast.makeText(sesyolla.this, "OKAY", Toast.LENGTH_SHORT).show();
info.setText(mp3FileUri.getPath());
}
}
}
private void uploadFile() {
if (mp3FileUri != null) {
final StorageReference fileReference = mStorageRef.child(System.currentTimeMillis()
+ "." + getFileExtension(mp3FileUri));
fileReference.putFile(mp3FileUri).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();
}
return fileReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
Log.e(TAG, "then: " + downloadUri.toString());
Upload upload = new Upload(edittext.getText().toString().trim(),
downloadUri.toString());
mDatabaseRef.push().setValue(upload);
} else {
Toast.makeText(sesyolla.this, "upload failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
解决方案
完整解决方案:
CloudStorageDatabaseUtils.java
public class CloudStorageDatabaseUtils {
public void uploadImage(String storageChildAndPath, File file) {
Uri uriFile = Uri.fromFile(file);
StorageReference storageRef = FirebaseStorage.getInstance().getReference();
final StorageReference ref = storageRef.child(storageChildAndPath);
ref.putFile(uriFile);
}
public void getDownloadURL(String path, UrlReceiver receiver) {
StorageReference reference = FirebaseStorage.getInstance().getReference().child(path);
reference.getDownloadUrl().addOnSuccessListener(uri -> receiver.onUrlReceived(uri.toString()));
}
}
activity_upload_image.xml 只是一个按钮
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button_upload"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/action_upload_an_image"/>
</LinearLayout>
UrlReceiver.java
public interface UrlReceiver {
/**
* @param url URL which will be received
*/
void onUrlReceived(String url);
}
上传图片活动.java
public class UploadImageActivity extends AppCompatActivity implements View.OnClickListener, UrlReceiver {
CloudStorageDatabaseUtils databaseUtils = new CloudStorageDatabaseUtils();
String downloadUrl;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload_image);
Button upload = findViewById(R.id.button_upload);
upload.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_upload:
File rootStorage = Environment.getExternalStorageDirectory();
File exactlyFile = new File(rootStorage.toString() + "/download/q1.jpeg");//or whatever
databaseUtils.uploadImage("/example1/" + exactlyFile.getName(), exactlyFile);
databaseUtils.getDownloadURL("/example1/" + exactlyFile.getName(), this);
}
}
@Override
public void onUrlReceived(String url) {
if (url != null) {
downloadUrl = url;
Log.d("MyS", "url: " + url);
}
}
}
推荐阅读
- javascript - 我正在尝试执行 regexp 命令,但它不起作用。我怎样才能使它可行?
- android - 如何使用颤振读取excel文件中的逐行详细信息?
- java - 如何在java中获取数据库表列名
- vb.net - 如何从 ComboBox 和 TextBox 获取值到 Argument 行?
- c# - RichTextBox 设置/更改下划线颜色(\ulcN)和下划线样式(例如\ulwave)以供选择
- azure - Azure API 管理 - 当 APM 落后于 WAF 时如何获取原始 IP
- wpf - 上下文菜单项未触发 Click 事件
- java - 回收视图不显示来自适配器的数据
- ios - 替换对象数组中的特定值 swift 5
- regex - 如何使用 ansible regexp 将新字符串插入到 telegraf.conf 的 inputs.ping