代码之家  ›  专栏  ›  技术社区  ›  B.Anto

将照片从SD上载到Firebase存储,无需所有路径

  •  1
  • B.Anto  · 技术社区  · 7 年前

    我想将照片上载到 FirebaseStorage ,从我的设备中获取。如果照片位于设备上,则会在文件夹下创建它 /Photos/namefile.jpg 如果我从SD卡加载,它会提供所有路径 /Photos/storage/emulated/0/namefile.jpg . 我想保存所有的数据 /照片/名称文件。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 回复  |  直到 7 年前
        1
  •  0
  •   Rosário Pereira Fernandes Aabid Ansar    7 年前

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

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

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

    String [] segments = file.getName().split();
    StorageReference path = storeRef.child(uid).child("Photos").child(segments[segments.length-1]);