Java 如何将mp3文件发送到firebase
我想将mp3文件发送到firebase存储,但我的代码不起作用。我没有错。它就是不起作用 我想将我从手机中选择的声音文件上载到firebase存储 这是我的密码:Java 如何将mp3文件发送到firebase,java,android,firebase,Java,Android,Firebase,我想将mp3文件发送到firebase存储,但我的代码不起作用。我没有错。它就是不起作用 我想将我从手机中选择的声音文件上载到firebase存储 这是我的密码: @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode =
@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();
}
}
});
}
}
@覆盖
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
if(requestCode==RQS\u OPEN\u AUDIO\u MP3&&resultCode==Activity.RESULT\u OK){
if((data!=null)&&(data.getData()!=null)){
Uri mp3FileUri=data.getData();
Toast.makeText(sesyolla.this,“OK”,Toast.LENGTH_SHORT.show();
info.setText(mp3FileUri.getPath());
}
}
}
私有void上传文件(){
if(mp3FileUri!=null){
final-StorageReference fileReference=mStorageRef.child(System.currentTimeMillis()
+“+getFileExtension(mp3FileUri));
fileReference.putFile(mp3FileUri).continueWithTask(新的Continuation(){
@凌驾
公共任务(@NonNull Task Task)引发异常{
如果(!task.issusccessful()){
抛出task.getException();
}
返回fileReference.getDownloadUrl();
}
}).addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
Uri downloadUri=task.getResult();
Log.e(标记“then:”+downloadUri.toString());
Upload Upload=新上载(edittext.getText().toString().trim(),
下载uri.toString());
mDatabaseRef.push().setValue(上传);
}否则{
Toast.makeText(sesyolla.this,“上载失败:+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()));
}
}
活动\u上传\u 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>
UploadImageActivity.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);
}
}
}
你试过这个了吗,这个可能会帮助你()@unzila,这不准确。Firebase可以存储多种文件,包括图像、视频和音频文件;而且没有理由分割这些文件。此外,OP询问的是mp3,而不是图像。对于OP,请在发布前进行一些故障排除。您可以逐步检查代码并检查变量,看看哪些变量不是您所期望的,然后更新您的问题。请花点时间回顾一下我对安卓系统还不熟悉。我不知道如何在按钮内使用uploadImage()方法发送音频文件。uploadImage()这只是示例^ ^您可以上载任何文件。下一个我会改变答案,等一下。。。如果你正在使用Android SDK > 23,那么你需要在运行时请求许可(即使你已经将它添加到清单中),考虑一下,我忘了告诉你,Logcat可以告诉你很多有用的东西,例如“我找不到文件,因为您没有权限\该文件\ SD卡等。我已经尝试了几个小时,但它没有。”。我从手机中选择mp3文件。但我不能转移firebase。你能推荐一个来源吗?
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);
}
}
}