我正在尝试将一些文本和图像上传到我的数据库,并且 getDownloadURL()方法无法识别,因为我正在使用
firebase 16.0.9
我想。所以现在我这样做:
Buildings upload = new
Buildings(mEditTextFileName.getText().toString().trim(),
downloadUrl.toString());
这有效,但是我不确定如何上传两个文本字段。在旧方法getDownloadURL()中,我将执行以下操作:
Buildings upload = new
Buildings(mEditTextAddress.getText().toString().trim(),
taskSnapshot.getDownloadUrl().toString(),
mEditTextAddress.getText ().toString ());
但是我不确定如何使用新方法上传多个文本字段。
UploadActivity.java
`private static final int PICK_IMAGE_REQUEST = 1;
private Button mButtonChooseImage;
private Button mButtonUpload;
private TextView mTextViewShowUploads;
private EditText mEditTextFileName;
private ImageView mImageView;
private ProgressBar mProgressBar;
private EditText mEditTextAddress;
private EditText mEditTextDescription;
private EditText mEditTextCategory;
private Uri mImageUri;
private StorageReference mStorageRef;
private DatabaseReference mDatabaseRef;
private StorageTask mUploadTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
FirebaseApp.initializeApp(this);
mButtonChooseImage = findViewById(R.id.chose_image_btn);
mButtonUpload = findViewById(R.id.upload_btn);
mTextViewShowUploads = findViewById(R.id.showRecycler_textView);
mEditTextFileName = findViewById(R.id.name_editText2);
mEditTextAddress = findViewById(R.id.address_editText3);
mEditTextDescription = findViewById(R.id.desc_editText4);
mEditTextCategory = findViewById(R.id.category_editText);
mImageView = findViewById(R.id.uploadedImgPreview);
mProgressBar = findViewById(R.id.progressBar);
mStorageRef = FirebaseStorage.getInstance().getReference("Buildings");
mDatabaseRef =
FirebaseDatabase.getInstance().getReference("Buildings");
mButtonChooseImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openFileChooser();
}
});
mButtonUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mUploadTask != null && mUploadTask.isInProgress()) {
Toast.makeText(UploadActivity.this, "Upload in progress",
Toast.LENGTH_SHORT).show();
} else {
uploadFile();
}
}
});
mTextViewShowUploads.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openImagesActivity();
}
});
}
private void openFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
&& data != null && data.getData() != null) {
mImageUri = data.getData();
Picasso.get().load(mImageUri).into(mImageView);
}
}
private String getFileExtension(Uri uri) {
ContentResolver cR = getContentResolver();
MimeTypeMap mime = MimeTypeMap.getSingleton();
return mime.getExtensionFromMimeType(cR.getType(uri));
}
private void uploadFile() {
if (mImageUri != null) {
StorageReference fileReference =
mStorageRef.child(System.currentTimeMillis()
+ "." + getFileExtension(mImageUri));
mUploadTask = fileReference.putFile(mImageUri)
.addOnSuccessListener(new
OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot
taskSnapshot) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mProgressBar.setProgress(0);
}
}, 500);
Toast.makeText(UploadActivity.this, "Upload
successful", Toast.LENGTH_LONG).show();
Task<Uri> urlTask =
taskSnapshot.getStorage().getDownloadUrl();
while (!urlTask.isSuccessful()) ;
Uri downloadUrl = urlTask.getResult();
Buildings upload = new
Buildings(mEditTextFileName.getText().toString().trim(),
downloadUrl.toString());
String uploadId = mDatabaseRef.push().getKey();
mDatabaseRef.child(uploadId).setValue(upload);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(UploadActivity.this,
e.getMessage(), Toast.LENGTH_SHORT).show();
}
})
.addOnProgressListener(new
OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot
taskSnapshot) {
double progress = (100.0 *
taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
mProgressBar.setProgress((int) progress);
}
});
} else {
Toast.makeText(this, "No file selected",
Toast.LENGTH_SHORT).show();
}
}
private void openImagesActivity() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}`