如何从sd卡

时间:2017-05-04 04:56:56

标签: android-studio file-upload firebase-storage

这是我的代码。我试图将文件从我的SD卡上传到firebasae存储,但它抛出“发生未知错误。请检查HTTP结果代码和服务器响应的内部异常。”我已经检查了很多解决方案,但没有任何对我有用所以请帮我解决这个问题。

 //upload file
private void uploadFile() throws FileNotFoundException {
    if (fileuri != null) {
        //displaying a progress dialog while upload is going on
        final ProgressDialog progress = new ProgressDialog(this);
        progress.setMessage("Uploading....");
        progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progress.setIndeterminate(true);
        progress.setCancelable(true);
        progress.setProgress(0);
        progress.show();

        StorageReference riversRef = storageReference.child("path");
        riversRef.putFile(fileuri).addOnSuccessListener(new 
        OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        //if the upload is successfull
        //hiding the progress dialog
        progress.dismiss();
        //and displaying a success toast
        Intent intent = new Intent(UploadActivity.this, hanksActivity.class);
        startActivity(intent);
        finish();
        }
       })
     .addOnFailureListener(new OnFailureListener() {                 
     @Override
     public void onFailure(@NonNull Exception exception) {                           
    //if the upload is not successfull
    //hiding the progress dialog
    progress.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
    @SuppressWarnings("VisibleForTests")
   double progresstime = (100.0 * taskSnapshot.getBytesTransferred()) / 
   taskSnapshot.getTotalByteCount();
  //displaying percentage in progress dialog
  progress.setProgress((int) progresstime);

                    }
                });
    } else {
   Toast.makeText(getApplicationContext(), "Choose file", 
   Toast.LENGTH_LONG).show();
    }
  }

0 个答案:

没有答案