2016-07-14 71 views
0

的進展I型代碼火力存儲結果的文件只有0.0上傳圖片與firebase。不能得到上傳

uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() { 
    @Override 
    public void onProgress(UploadTask.TaskSnapshot taskSnapshot) { 
     double progress = 100.0* (taskSnapshot.getBytesTransferred()/taskSnapshot.getTotalByteCount()); 

     dialog.setMessage("uploading "+progress+" %"); 
     dialog.show(); 
    } 
}); 
+0

有錯在代碼示例中。請參閱http://stackoverflow.com/questions/38278249/update-progress-bar-with-firebase-uploading –

+0

謝謝弗蘭克 工作 –

+0

不要感謝我。我是在文檔中編寫破碎的代碼示例的人。 ;-)感謝@qbix(通過提高他的答案)。 –

回答

0

這是你可以上傳圖片到火力地堡存儲好運;)

private void uploadFromUri(Uri fileUri) { 
     Log.d(TAG, "uploadFromUri:src:" + fileUri.toString()); 

     // [START get_child_ref] 
     // Get a reference to store file at photos/<FILENAME>.jpg 
     final StorageReference photoRef = storageRef.child("images") 
       .child(fileUri.getLastPathSegment()); 
     // [END get_child_ref] 

     // Upload file to Firebase Storage 
     // [START_EXCLUDE] 
     showProgressDialog(); 
     // [END_EXCLUDE] 
     Log.d(TAG, "uploadFromUri:dst:" + photoRef.getPath()); 
     photoRef.putFile(fileUri) 
       .addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() { 
        @Override 
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 
         // Upload succeeded 
         Log.d(TAG, "uploadFromUri:onSuccess"); 

         // Get the public download URL 
         mDownloadUrl = taskSnapshot.getMetadata().getDownloadUrl(); 
         Log.d("The Download Url", mDownloadUrl.toString()); 


         // [START_EXCLUDE] 
         hideProgressDialog(); 
        // updateUI(mAuth.getCurrentUser()); 
         // [END_EXCLUDE] 
        } 
       }) 
       .addOnFailureListener(this, new OnFailureListener() { 
        @Override 
        public void onFailure(@NonNull Exception exception) { 
         // Upload failed 
         Log.w(TAG, "uploadFromUri:onFailure", exception); 

         mDownloadUrl = null; 

         // [START_EXCLUDE] 
         hideProgressDialog(); 
         Toast.makeText(NewProduct.this, "Error: upload failed", 
           Toast.LENGTH_SHORT).show(); 
         // updateUI(mAuth.getCurrentUser()); 
         // [END_EXCLUDE] 
        } 
       }); 
    } 
    // [END upload_from_uri]