Android FirebaseStorage无法找到文件

时间:2017-04-04 11:55:23

标签: android file firebase upload firebase-storage

这是我的功能:

 public static void sendLogFileToFirebase(Context context, File file, final CrudStateCallback back){
    UserRecord user = UserRecord.getMyUser(context);
    Calendar calendar = Calendar.getInstance();
    FirebaseStorage storage = FirebaseStorage.getInstance();
    StorageReference storageRef = storage.getReference();
    String folder = "users/";
    final StorageReference logsRef = storageRef.child(folder + user.getId() + "/logs/" + calendar.get(Calendar.YEAR) + "_" + (calendar.get(Calendar.MONTH) + 1) + "_" + calendar.get(Calendar.DAY_OF_MONTH) + "/"
            + calendar.get(Calendar.HOUR) + calendar.get(Calendar.MINUTE) + calendar.get(Calendar.SECOND) + ".zip");
    String[] files = new String[1];
    files[0] = file.getAbsolutePath();
    Compress compress = new Compress(files, Environment.getExternalStorageDirectory() + "/Passenger/log.zip");
    final File fileLog = new File(Environment.getExternalStorageDirectory() + "/Passenger/log.zip");
    compress.zip(new CrudStateCallback() {
        @Override
        public void onResponse(String string) {
            Log.i("","path will be:" + logsRef.getPath());
            logsRef.putFile(Uri.parse(fileLog.getAbsolutePath()));
            if(back != null)
                back.onResponse("");
        }
    });
}

当它从compress方法进入回调时, 它在这一行得到例外:

logsRef.putFile(Uri.parse(fileLog.getAbsolutePath()));

错误:

 04-04 13:47:53.659: W/UploadTask(10206): could not retrieve file size for upload /storage/emulated/0/Passenger/log.zip
 04-04 13:47:53.659: W/UploadTask(10206): java.io.FileNotFoundException: No content provider: /storage/emulated/0/Passenger/log.zip
 04-04 13:47:53.659: W/UploadTask(10206):   at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1090)
 04-04 13:47:53.659: W/UploadTask(10206):   at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:942)
 04-04 13:47:53.659: W/UploadTask(10206):   at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:795)
 04-04 13:47:53.659: W/UploadTask(10206):   at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:749)
 04-04 13:47:53.659: W/UploadTask(10206):   at com.google.firebase.storage.UploadTask.<init>(Unknown Source)
 04-04 13:47:53.659: W/UploadTask(10206):   at com.google.firebase.storage.StorageReference.putFile(Unknown Source)
 04-04 13:47:53.659: W/UploadTask(10206):   at nl.hgrams.passenger.utils.Utils$12.onResponse(Utils.java:765)
 04-04 13:47:53.659: W/UploadTask(10206):   at nl.hgrams.passenger.utils.Compress.zip(Compress.java:51)
 04-04 13:47:53.659: W/UploadTask(10206):   at nl.hgrams.passenger.utils.Utils.sendLogFileToFirebase(Utils.java:761)
 04-04 13:47:53.659: W/UploadTask(10206):   at nl.hgrams.passenger.activities.PSAboutActivity.onCreate(PSAboutActivity.java:81)
 04-04 13:47:53.659: W/UploadTask(10206):   at android.app.Activity.performCreate(Activity.java:6251)
 04-04 13:47:53.659: W/UploadTask(10206):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
 04-04 13:47:53.659: W/UploadTask(10206):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
 04-04 13:47:53.659: W/UploadTask(10206):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
 04-04 13:47:53.659: W/UploadTask(10206):   at android.app.ActivityThread.-wrap11(ActivityThread.java)
 04-04 13:47:53.659: W/UploadTask(10206):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)     
 04-04 13:47:53.659: W/UploadTask(10206):   at android.os.Handler.dispatchMessage(Handler.java:102)
 04-04 13:47:53.659: W/UploadTask(10206):   at android.os.Looper.loop(Looper.java:148)
 04-04 13:47:53.659: W/UploadTask(10206):   at android.app.ActivityThread.main(ActivityThread.java:5417)
 04-04 13:47:53.659: W/UploadTask(10206):   at java.lang.reflect.Method.invoke(Native Method)
 04-04 13:47:53.659: W/UploadTask(10206):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)     
 04-04 13:47:53.659: W/UploadTask(10206):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 04-04 13:47:53.660: E/UploadTask(10206): could not locate file for uploading:/storage/emulated/0/Passenger/log.zip
 04-04 13:47:53.682: W/Binder_10(4347): type=1400 audit(0.0:13319): avc: denied { ioctl } for path="socket:[1194698]" dev="sockfs" ino=1194698 ioctlcmd=7704 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=unix_stream_socket permissive=0
 04-04 13:47:53.682: W/Binder_10(4347): type=1400 audit(0.0:13320): avc: denied { ioctl } for path="socket:[1194698]" dev="sockfs" ino=1194698 ioctlcmd=7704 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=unix_stream_socket permissive=0
 04-04 13:47:53.676: E/StorageException(10206): StorageException has occurred.
 04-04 13:47:53.676: E/StorageException(10206): An unknown error occurred, please check the HTTP result code and inner exception for server response.
 04-04 13:47:53.676: E/StorageException(10206):  Code: -13000 HttpResult: 0
 04-04 13:47:53.678: E/StorageException(10206): No content provider: /storage/emulated/0/Passenger/log.zip
 04-04 13:47:53.678: E/StorageException(10206): java.io.FileNotFoundException: No content provider: /storage/emulated/0/Passenger/log.zip
 04-04 13:47:53.678: E/StorageException(10206):     at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1090)
 04-04 13:47:53.678: E/StorageException(10206):     at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:942)
 04-04 13:47:53.678: E/StorageException(10206):     at android.content.ContentResolver.openInputStream(ContentResolver.java:662)
 04-04 13:47:53.678: E/StorageException(10206):     at com.google.firebase.storage.UploadTask.<init>(Unknown Source)
 04-04 13:47:53.678: E/StorageException(10206):     at com.google.firebase.storage.StorageReference.putFile(Unknown Source)
 04-04 13:47:53.678: E/StorageException(10206):     at nl.hgrams.passenger.utils.Utils$12.onResponse(Utils.java:765)
 04-04 13:47:53.678: E/StorageException(10206):     at nl.hgrams.passenger.utils.Compress.zip(Compress.java:51)
 04-04 13:47:53.678: E/StorageException(10206):     at nl.hgrams.passenger.utils.Utils.sendLogFileToFirebase(Utils.java:761)
 04-04 13:47:53.678: E/StorageException(10206):     at nl.hgrams.passenger.activities.PSAboutActivity.onCreate(PSAboutActivity.java:81)
 04-04 13:47:53.678: E/StorageException(10206):     at android.app.Activity.performCreate(Activity.java:6251)     
 04-04 13:47:53.678: E/StorageException(10206):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
 04-04 13:47:53.678: E/StorageException(10206):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
 04-04 13:47:53.678: E/StorageException(10206):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
 04-04 13:47:53.678: E/StorageException(10206):     at android.app.ActivityThread.-wrap11(ActivityThread.java)
 04-04 13:47:53.678: E/StorageException(10206):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
 04-04 13:47:53.678: E/StorageException(10206):     at android.os.Handler.dispatchMessage(Handler.java:102)
 04-04 13:47:53.678: E/StorageException(10206):     at android.os.Looper.loop(Looper.java:148)
 04-04 13:47:53.678: E/StorageException(10206):     at android.app.ActivityThread.main(ActivityThread.java:5417)
 04-04 13:47:53.678: E/StorageException(10206):     at java.lang.reflect.Method.invoke(Native Method)
 04-04 13:47:53.678: E/StorageException(10206):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
 04-04 13:47:53.678: E/StorageException(10206):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 04-04 13:47:53.695: E/StorageException(10206): StorageException has occurred.
 04-04 13:47:53.695: E/StorageException(10206): An unknown error occurred, please check the HTTP result code and inner exception for server response.
 04-04 13:47:53.695: E/StorageException(10206):  Code: -13000 HttpResult: 0
 04-04 13:47:53.695: E/StorageException(10206): No content provider: /storage/emulated/0/Passenger/log.zip
 04-04 13:47:53.695: E/StorageException(10206): java.io.FileNotFoundException: No content provider: /storage/emulated/0/Passenger/log.zip
 04-04 13:47:53.695: E/StorageException(10206):     at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1090)     
 04-04 13:47:53.695: E/StorageException(10206):     at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:942)
 04-04 13:47:53.695: E/StorageException(10206):     at android.content.ContentResolver.openInputStream(ContentResolver.java:662)
 04-04 13:47:53.695: E/StorageException(10206):     at com.google.firebase.storage.UploadTask.<init>(Unknown Source)
 04-04 13:47:53.695: E/StorageException(10206):     at com.google.firebase.storage.StorageReference.putFile(Unknown Source)
 04-04 13:47:53.695: E/StorageException(10206):     at nl.hgrams.passenger.utils.Utils$12.onResponse(Utils.java:765)
 04-04 13:47:53.695: E/StorageException(10206):     at nl.hgrams.passenger.utils.Compress.zip(Compress.java:51)
 04-04 13:47:53.695: E/StorageException(10206):     at nl.hgrams.passenger.utils.Utils.sendLogFileToFirebase(Utils.java:761)
 04-04 13:47:53.695: E/StorageException(10206):     at nl.hgrams.passenger.activities.PSAboutActivity.onCreate(PSAboutActivity.java:81)
 04-04 13:47:53.695: E/StorageException(10206):     at android.app.Activity.performCreate(Activity.java:6251)
 04-04 13:47:53.695: E/StorageException(10206):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
 04-04 13:47:53.695: E/StorageException(10206):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)

为什么会这样?我检查了,文件存在于我的文件夹中。 我也尝试过直接logsRef.putFile(Uri.parse(Environment.getExternalStorageDirectory() + "/Passenger/log.zip")); 但同样的事情发生了。任何想法?

1 个答案:

答案 0 :(得分:0)

这有效:

 InputStream stream = new FileInputStream(fileLog);
 UploadTask uploadTask = logsRef.putStream(stream);

而不是尝试直接上传文件