我正在将图像上传到Firebase,并且要将其URL存储到数据库中
我的图像在Firebase存储中上传,名称存储在Firebase数据库中
但是在ImageUrl“ com.google.android.gms.tasks.zzu@5ef6ed8”中会存储这种网址
为了存储正确的URL,我应该做些什么更改
请提供完整代码
这是我的Upload_photos.java
package com.namandevloper.satyampublic;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.OnProgressListener;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.squareup.picasso.Picasso;
public class Upload_Photos extends AppCompatActivity {
private static final int PICK_IMAGE_REQUEST = 1;
private ImageView imageView;
private EditText name;
private Button uploadbutton;
private Uri imageuri;
private StorageReference mStorageref;
private DatabaseReference mDatabaseref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload__photos);
imageView = findViewById(R.id.image);
name = findViewById(R.id.name);
uploadbutton = findViewById(R.id.btn_upload);
mStorageref = FirebaseStorage.getInstance().getReference("Upload Photos");
mDatabaseref = FirebaseDatabase.getInstance().getReference("Upload Photos");
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openfilechooser();
}
});
uploadbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadfile();
}
});
}
private String getFileExtension(Uri uri){
ContentResolver cr = getContentResolver();
MimeTypeMap mime = MimeTypeMap.getSingleton();
return mime.getExtensionFromMimeType(cr.getType(uri));
}
private void uploadfile() {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading");
progressDialog.show();
if (imageuri !=null){
StorageReference filereference = mStorageref.child(System.currentTimeMillis()+
"."+getFileExtension(imageuri));
filereference.putFile(imageuri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(Upload_Photos.this, "Upload Successfully", Toast.LENGTH_SHORT).show();
Upload upload = new Upload(name.getText().toString().trim(),taskSnapshot.getMetadata().getReference().getDownloadUrl()
.toString());
progressDialog.show();
String uploadId = mDatabaseref.push().getKey();
mDatabaseref.child(uploadId).setValue(upload);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.dismiss();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0*taskSnapshot.getBytesTransferred())/taskSnapshot.getTotalByteCount();
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setMessage("Uploaded " +(int)progress+"%");
}
});
}else
Toast.makeText(this, "Please Select a Image", Toast.LENGTH_SHORT).show();
}
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, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data !=null &&
data.getData()!=null){
imageuri = data.getData();
Picasso .get().load(imageuri).into(imageView);
}
}
}
这是我的upload.java
package com.namandevloper.satyampublic;
public class Upload {
private String mName;
private String mImageUrl;
public Upload() {
//empty constructor needed
}
public Upload(String name, String imageUrl) {
if (name.trim().equals("")) {
name = "No Name";
}
mName = name;
mImageUrl = imageUrl;
}
public String getName() {
return mName;
}
public void setName(String name) {
mName = name;
}
public String getImageUrl() {
return mImageUrl;
}
public void setImageUrl(String imageUrl) {
mImageUrl = imageUrl;
}
}
这是我的activity_upload__photos
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:padding="16dp"
android:layout_height="match_parent"
tools:context=".Upload_Photos">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Image to Upload"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:textSize="22dp"
android:textStyle="bold"
/>
<ImageView
android:id="@+id/image"
android:layout_width="380dp"
android:layout_height="249dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:src="@drawable/click" />
<EditText
android:id="@+id/name"
android:layout_width="354dp"
android:layout_height="66dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="400dp"
android:hint="Image Name" />
<Button
android:id="@+id/btn_upload"
android:layout_width="249dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="500dp"
android:background="@android:color/holo_red_dark"
android:text="Upload"
android:textColor="@android:color/white"
android:textStyle="bold" />
</RelativeLayout>
这是我的数据库的外观
您必须呼叫监听器才能从Firebase存储中获取下载uri。使用以下代码可以做到这一点。阅读docs