gpt4 book ai didi

java - Android - 如何将图像存储在已创建的 Firestore 文件夹中

转载 作者:太空宇宙 更新时间:2023-11-04 10:20:56 25 4
gpt4 key购买 nike

public void upload() {
if(imageUri!=null) {
StorageReference reference=storageReference.child("customers/");
reference.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Toast.makeText(getApplicationContext(),"File Uploaded",Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double totalProgress=(100*taskSnapshot.getBytesTransferred())/taskSnapshot.getTotalByteCount();
// Toast.makeText(getApplicationContext(),"File uploaded"+(int)totalProgress,Toast.LENGTH_LONG).show();
}
});
}
}

在 Firebase 存储中,有一个名为“customers”的文件夹,我想在其中存储所选图像。当我尝试上传图像时,它将其存储为不同的文件夹,而不是已创建的文件夹。这里,“customers”是已经创建的文件夹。

最佳答案

public void upload(String imageName) {
if(imageUri!=null) {
StorageReference reference=storageReference.child("customers).child(imageName);
reference.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Toast.makeText(getApplicationContext(),"File Uploaded",Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double totalProgress=(100*taskSnapshot.getBytesTransferred())/taskSnapshot.getTotalByteCount();
// Toast.makeText(getApplicationContext(),"File uploaded"+(int)totalProgress,Toast.LENGTH_LONG).show();
}
});
}
}

为了检索图像名称,请使用:-

public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData( );
String picturePath = getPath( getActivity( ).getApplicationContext( ), selectedImageUri );
Log.d("Picture Path", picturePath);
upload(picturePath);

}
}

public static String getPath( Context context, Uri uri ) {
String result = null;
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver( ).query( uri, proj, null, null, null );
if(cursor != null){
if ( cursor.moveToFirst( ) ) {
int column_index = cursor.getColumnIndexOrThrow( proj[0] );
result = cursor.getString( column_index );
}
cursor.close( );
}
if(result == null) {
result = "Not found";
}
return result;
}

关于java - Android - 如何将图像存储在已创建的 Firestore 文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51183231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com