我正在开发一个Android应用程序,用于从Firebase上传和下载音频,并且我已成功将三个音频文件(A.mp3,B.mp3和C.mp3)上传到Firebase存储。现在我要选择哪个文件要下载说B.mp3,这就是我遇到的问题。如何选择要下载然后下载的音频?
我有3个按钮,一个用于选择要上传的文件,一个上传按钮和一个下载按钮。
这是我的代码...
package com.hussainapplications.firebasestorageexample;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.OnProgressListener;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import java.io.File;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
Button buttonChoose,buttonUpload,buttonDownload;
TextView fileselected;
private StorageReference storageReference;
private static final int PICK_IMAGE_REQUEST = 234;
private Uri filePath;
public static int fileCount=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonChoose=findViewById(R.id.buttonChoose);
buttonUpload=findViewById(R.id.buttonUpload);
buttonDownload=findViewById(R.id.buttonDownload);
fileselected=findViewById(R.id.fileselected);
storageReference = FirebaseStorage.getInstance().getReference();
final ProgressDialog progressDialog = new ProgressDialog(this);
buttonChoose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("Audio/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Audio"), PICK_IMAGE_REQUEST);
}
});
buttonUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fileCount++;
if (filePath != null) {
//displaying a progress dialog while upload is going on
progressDialog.setTitle("Uploading");
progressDialog.show();
StorageReference riversRef = storageReference.child("Audio/audio" + fileCount);
riversRef.putFile(filePath)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//if the upload is successfull
//hiding the progress dialog
progressDialog.dismiss();
//and displaying a success toast
Toast.makeText(getApplicationContext(), "File Uploaded ", Toast.LENGTH_LONG).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
//if the upload is not successfull
//hiding the progress dialog
progressDialog.dismiss();
//and displaying error message
Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
//calculating progress percentage
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
//displaying percentage in progress dialog
progressDialog.setMessage("Uploading....");
//progressDialog.setMessage("Uploaded " + ((int) progress) + "%...");
}
});
}
//if there is not any file
else {
//you can display an error toast
}
}
});
buttonDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fileCount++;
try {
StorageReference riversRef = storageReference.child("Audio").child("New_Audio.mp3");
//download
riversRef = storageReference.child("Audio").child("New_Audio.mp3"+fileCount);
File localFile = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "New_Audio.mp3");
localFile .createNewFile();
riversRef.getFile(localFile);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
fileselected.setText("File Selected");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="200dp"
android:layout_height="50dp"
android:id="@+id/buttonChoose"
android:text="Choose File"/>
<Button
android:layout_width="200dp"
android:layout_height="50dp"
android:id="@+id/buttonUpload"
android:layout_below="@+id/buttonChoose"
android:text="Upload"/>
<Button
android:layout_width="200dp"
android:layout_height="50dp"
android:id="@+id/buttonDownload"
android:layout_below="@+id/buttonUpload"
android:text="Download"/>
<TextView
android:id="@+id/fileselected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/buttonChoose"
android:layout_marginStart="7dp"
android:textColor="#000"
android:textStyle="bold"
android:layout_toEndOf="@id/buttonChoose"
android:textSize="20sp" />
</RelativeLayout>