Firebase存储映像通过Android App成功上传,但如何获取url?

时间:2019-06-29 05:03:22

标签: android firebase firebase-storage

我已经在我的Android应用中使用了这个库

implementation 'com.google.firebase:firebase-core:17.0.0'
implementation 'com.google.firebase:firebase-firestore:20.0.0'
implementation 'com.google.firebase:firebase-storage:18.0.0'

此方法用于将我的图像上传到Firebase存储上:

 StorageReference storageRef = mStorage.getReference();
 finalStorageReference mountainsRef = storageRef.child("myImgName");

 Uri file = Uri.fromFile(new File(myImgName));

UploadTask uploadTask = mountainsRef.putFile(file);

uploadTask.addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Handle unsuccessful uploads
        progressDialog.dismiss();
        Log.e(TAG, "img Error :" + exception.getMessage());
        //Toast.makeText(MainActivity.this, "Failed "+e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        progressDialog.dismiss();
        //Log.e(TAG, "Task :" + taskSnapshot.getTask());

        //Log.e(TAG, "Class Store:" + taskSnapshot.getStorage().getDownloadUrl());
        Log.e(TAG,"metaData :"+taskSnapshot.getMetadata().getPath());

        // taskSnapshot.getMetadata() contains file metadata such as size, content-type, etc.
        // ...
    }
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
        double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot
                .getTotalByteCount());
        progressDialog.setMessage("Uploaded " + (int) progress + "%");
    }
});

此代码非常适合我上传图片。但是如何知道图片的位置网址?

2 个答案:

答案 0 :(得分:2)

要获取Cloud Storage中文件的下载URL,请在文件的getDownloadUrl()上调用StorageReferencegetDownloadUrl()返回一个任务,因此您需要添加成功的侦听器以获取结果。

mountainsRef.getStorage().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {
        // Got the download URL for 'users/me/profile.png' in uri
        System.out.println(uri.toString());
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Handle any errors
    }
});

有关此的更多信息,请参见:

答案 1 :(得分:0)

您可以在Firebase控制台的“存储”的“文件”部分顶部找到项目的URL

Matrix::add()

您可以创建一个File对象,并通过使用新的File对象作为参数传递调用StorageReference上的getFile来尝试加载所需的文件。由于此操作是异步发生的,因此您可以添加OnSuccessListener和OnFailureListener

int Matrix::add()
{
    Matrix s1;
    int i, j=0;
    s1.getmatrix();
    cout << "Addition of Matrix A & B is" << endl;
    // cout << "Rows: " << s1.r << " Cols: " << s1.c << endl; 
    for (i = 0; i < s1.r; i++)
    {
        for (j = 0; j < s1.c; j++)
        {
            s1.C[i][j] = s1.A[i][j] + s1.B[i][j];
        }
    }
    cout << "\n";

    for (i = 0; i < s1.r; i++)
    {
        for (j = 0; j < s1.c; j++)
        {
            cout << " " << s1.C[i][j];
        }
    }
    return 0;
}

您可以使用StorageReference上的getDownloadUrl()方法获取文件的网址,这将为您提供一个指向文件位置的Uri。

 FirebaseStorage storage = FirebaseStorage.getInstance();
 StorageReference storageRef = storage.getReferenceFromUrl("gs://example-firebase.appspot.com").child("android.jpg");