gpt4 book ai didi

java - 如何将 mp3 文件发送到 firebase

转载 作者:行者123 更新时间:2023-11-29 23:02:30 26 4
gpt4 key购买 nike

我想将 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>

网址接收者.java

public interface UrlReceiver {
/**
* @param url URL which will be received
*/
void onUrlReceived(String url);
}

上传ImageActivity.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);
}
}
}

关于java - 如何将 mp3 文件发送到 firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56833732/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com