使用我的Android应用程序将文件上传到firebase时出错

时间:2017-05-29 18:46:02

标签: android firebase firebase-storage

我特此分享我的代码

MainActivity.java

package com.example.auto;

 import android.net.Uri; 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 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.StorageMetadata; import
 com.google.firebase.storage.StorageReference; import
 com.google.firebase.storage.UploadTask;

 public class MainActivity extends AppCompatActivity implements
 View.OnClickListener {
     private StorageMetadata metadata;
     StorageReference storageReference;
     private TextView textView;
     private Button buttonUpload;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         buttonUpload = (Button) findViewById(R.id.buttonUpload);
         textView=(TextView) findViewById(R.id.textView);
         buttonUpload.setOnClickListener(this);
     }
     public  void uploadVideo() {
         //if there is a file to upload
         Uri filePath=Uri.parse("/storage/emulated/0/DCIM/Camera/myvideo.mp4");
         if (filePath != null) {

             FirebaseStorage storage = FirebaseStorage.getInstance();
             StorageReference storageRef = storage.getReference();

   metadata = new
 StorageMetadata.Builder().setContentType("video/mp4").build();

             StorageReference photoRef = storageRef.child("video.mp4");


         Metadata metadata = new StorageMetadata.Builder()
                     .setContentType("vidio/mp4")
                     .build();*/

             UploadTask uploadTask = photoRef.putFile(filePath,metadata);
             uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                 @Override
                 public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                     //if the upload is successfull

                     textView.setText("Success");

                 }
             })
                     .addOnFailureListener(new OnFailureListener() {
                         @Override
                         public void onFailure(@NonNull Exception exception) {
                             //if the upload is not successfull

                             textView.setText("Failed");

                         }
                     })
                     .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                         @Override
                         public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {

                             textView.setText("Uploading");

                         }
                     });

         }

     }
     @Override
     public void onClick(View v) {
      /*   if (v == buttonChoose) {
             chooseVideo();
         }*/
         if (v == buttonUpload) {
             uploadVideo();
         }
     } }

输出始终在文本视图中显示为“失败”

我的build.gradle(Module:app)文件是:

 apply plugin: 'com.android.application'

 android {
     compileSdkVersion 25
     buildToolsVersion "25.0.3"
     defaultConfig {
         applicationId "com.example.auto"
         minSdkVersion 19
         targetSdkVersion 25
         versionCode 1
         versionName "1.0"
         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
     }
     buildTypes {
         release {
             minifyEnabled false
             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
         }
     } }

 dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2',
 {
         exclude group: 'com.android.support', module: 'support-annotations'
     })
     compile 'com.android.support:appcompat-v7:25.3.1'
     compile 'com.google.firebase:firebase-storage:10.2.6'
     compile 'com.google.firebase:firebase-auth:10.2.6'
     testCompile 'junit:junit:4.12' }

0 个答案:

没有答案