将照片从SD上传到Firebase存储,无需任何路径

时间:2018-01-12 10:28:01

标签: android firebase firebase-storage

我想将照片上传到FirebaseStorage,从我的设备中取出。如果照片位于设备上,则会在/Photos/namefile.jpg文件夹下创建照片,而如果我从SD卡加载它会为我提供所有路径/Photos/storage/emulated/0/namefile.jpg。我想用/Photos/namefile.jpg

保存所有数据

这是我的代码:

    if (requestCode==GALERY_INTENT &&resultCode==RESULT_OK){
        final Uri uri = data.getData();
        Log.d(TAG,uri.getLastPathSegment().toString());
        Log.d(TAG,uri.toString());
        StorageReference path = storeRef.child(uid).child("Photos").child(uri.getLastPathSegment());
        path.putFile(uri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                if(task.isSuccessful()){

                   //file uploaded

                }

            }


        });

更新

现在它正好适用于以下代码:

final Uri uri = data.getData();
            File file = new File(uri+"");            
            String [] segments = file.getName().split("[\\p{Punct}‘]2F");
            StorageReference path = storeRef.child(uid).child("Photos").child(segments[segments.length-1]);

1 个答案:

答案 0 :(得分:0)

您可以将该Uri强制转换为文件:

 fetchSearchTopStories(searchTerm, page = 0){
      fetch(`${PATH_BASE}${PATH_SEARCH}?${PARAM_SEARCH}${searchTerm}&${PARAM_PAGE}`+encodeURIComponent(`\${page}`))
      .then(response => response.json())
      .then(result => this.setSearchTopStories(result))
      .catch(e => {

      })
    }

然后从路径中获取文件名并将其传递给storageReference:

File file = new File(uri+"");