【发布时间】:2018-02-27 22:15:03
【问题描述】:
- 我是 Android 新手,不知道如何从以下位置下载
mediafirebase - 在
uploadFile方法中,我在FirebaseStorage中上传媒体,并在获得成功响应后downloadUrl = taskSnapshot.getDownloadUrl()发送到FirebaseDatabase - 我也收到了
url,但无法从url获得download媒体
private void uploadFile(Uri uri) {
StorageReference uploadStorageReference = mStorageReferenceMedia.child(uri.getLastPathSegment());
final UploadTask uploadTask = uploadStorageReference.putFile(uri);
showHorizontalProgressDialog("Uploading", "Please wait...");
uploadTask
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
hideProgressDialog();
downloadUrl = taskSnapshot.getDownloadUrl();
Log.e("MainActivity"+"241>>>>", downloadUrl.toString());
Toast.makeText(ActivityChatView.this, "ulr>>"+downloadUrl, Toast.LENGTH_SHORT).show();
sendURL();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
exception.printStackTrace();
// Handle unsuccessful uploads
hideProgressDialog();
}
})
.addOnProgressListener(ActivityChatView.this, new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
int progress = (int) (100 * (float) taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
Log.e("Progress>>", progress + "");
updateProgress(progress);
}
});
}
public class DownloadTask {
private static final String TAG = "Download Task";
private Context context;
private Button buttonText;
private String downloadUrl = "", downloadFileName = "";
public DownloadTask(Context context, String downloadUrl) {
this.context = context;
this.buttonText = buttonText;
this.downloadUrl = downloadUrl;
downloadFileName = downloadUrl.replace("", "");//Create file name by picking download file name from URL
Log.e(TAG, downloadFileName);
//Start Downloading Task
new DownloadingTask().execute();
}
private class DownloadingTask extends AsyncTask<Void, Void, Void> {
File apkStorage = null;
File outputFile = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(Void result) {
try {
if (outputFile != null) {
buttonText.setEnabled(true);
Log.e("57","download"+"completed");
} else {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.e("62","download again");
}
}, 3000);
Log.e(TAG, "Download Failed");
}
} catch (Exception e) {
e.printStackTrace();
Log.e("72,","download failed");
//Change button text if exception occurs
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.e("80","download again");
}
}, 3000);
Log.e(TAG, "Download Failed with Exception - " + e.getLocalizedMessage());
}
super.onPostExecute(result);
}
@Override
protected Void doInBackground(Void... arg0) {
try {
URL url = new URL(downloadUrl);//Create Download URl
HttpURLConnection c = (HttpURLConnection) url.openConnection();//Open Url Connection
c.setRequestMethod("GET");//Set Request Method to "GET" since we are grtting data
c.connect();//connect the URL Connection
//If Connection response is not OK then show Logs
if (c.getResponseCode() != HttpURLConnection.HTTP_OK) {
Log.e(TAG, "Server returned HTTP " + c.getResponseCode()
+ " " + c.getResponseMessage());
}
//Get File if SD card is present
if (new CheckForSDCard().isSDCardPresent()) {
apkStorage = new File(Environment.getExternalStorageDirectory() + "/"
+ "downloadDirectory");
} else
Toast.makeText(context, "Oops!! There is no SD Card.", Toast.LENGTH_SHORT).show();
//If File is not present create directory
if (!apkStorage.exists()) {
apkStorage.mkdir();
Log.e(TAG, "Directory Created.");
}
outputFile = new File(apkStorage, downloadFileName);//Create Output file in Main File
//Create New File if not present
if (!outputFile.exists()) {
outputFile.createNewFile();
Log.e(TAG, "File Created");
}
FileOutputStream fos = new FileOutputStream(outputFile);//Get OutputStream for NewFile Location
InputStream is = c.getInputStream();//Get InputStream for connection
byte[] buffer = new byte[1024];//Set buffer type
int len1 = 0;//init length
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);//Write new file
}
//Close all connection after doing task
fos.close();
is.close();
} catch (Exception e) {
//Read exception if something went wrong
e.printStackTrace();
outputFile = null;
Log.e(TAG, "Download Error Exception " + e.getMessage());
}
return null;
}
}
}
【问题讨论】:
标签: java android firebase firebase-realtime-database firebase-storage