我必须将某些信息上传到实时数据库中。除图片网址外,所有详细信息均已正确上传到其中。使用taskSnapshot.getMetadata()。getReference()。getDownloadUrl()我得到了类似com.google.android.gms.tasks.zzu@441922b的图像链接。该文件已上载到存储中。但是我没有确切的下载网址来访问它。代码在下面添加
Profileregistration.java
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
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.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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.StorageTask;
import com.google.firebase.storage.UploadTask;
import com.squareup.picasso.Picasso;
public class Profileregistration extends AppCompatActivity{
EditText hname, phone, hlocation;
ImageView image;
String name,email,house,location,number,photostring;
Button register;
private static final int PICK_IMAGE_REQUEST = 1;
private Uri mImageUri;
private StorageReference mStorageRef;
private DatabaseReference mDatabaseRef;
private ProgressBar mProgressBar;
private StorageTask mUploadTask;
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profileregistration);
firebaseAuth = FirebaseAuth.getInstance();
Intent intent = getIntent();
hname = (EditText) findViewById(R.id.userreghouse);
phone = (EditText) findViewById(R.id.userregphone);
hlocation = (EditText) findViewById(R.id.userreglocation);
name = intent.getExtras().getString("name");
email = intent.getExtras().getString("email");
house = hname.getText().toString().trim();
location = hlocation.getText().toString().trim();
number = phone.getText().toString().trim();
mProgressBar = findViewById(R.id.progress_bar);
mStorageRef = FirebaseStorage.getInstance().getReference("userdetails");
mDatabaseRef = FirebaseDatabase.getInstance().getReference("userdetails");
image = (ImageView) findViewById(R.id.userregprofilepic);
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openFileChooser();
}
});
register = (Button) findViewById(R.id.regprofilebutton);
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mUploadTask != null && mUploadTask.isInProgress()) {
Toast.makeText(Profileregistration.this, "Upload in progress", Toast.LENGTH_SHORT).show();
}
else {
uploadFile();
}
}
});
}
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.with(this).load(mImageUri).into(image);
}
}
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(phone.getText().toString().trim()
+ "." + getFileExtension(mImageUri));
mUploadTask = fileReference.putFile(mImageUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
final Task<Uri> result = taskSnapshot.getMetadata().getReference().getDownloadUrl();
result.addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
photostring = result.toString();
}
});
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mProgressBar.setProgress(0);
}
}, 500);
FirebaseUser user = firebaseAuth.getCurrentUser();
String useremail=user.getEmail().replace("@","").replace(".","");
Toast.makeText(Profileregistration.this, "Upload successful", Toast.LENGTH_LONG).show();
Upload upload = new Upload(name,user.getEmail(),hname.getText().toString().trim(),hlocation.getText().toString().trim(),
phone.getText().toString().trim(),result.toString());
String uploadId = useremail;
mDatabaseRef.child(uploadId).setValue(upload);
finish();
startActivity(new Intent(getApplicationContext(), Homewindow.class));
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Profileregistration.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();
}
}
}
Upload.java
public class Upload {
private String mEmail;
private String mPhone;
private String mHname;
private String mHlocation;
private String mName;
private String mImageUrl;
public Upload() {
//empty constructor needed
}
public Upload(String name,String email,String house,String location,String phone,String imageUrl) {
if (name.trim().equals("")) {
name = "No Name";
}
mName = name;
mEmail = email;
mHname = house;
mHlocation = location;
mPhone = phone;
mImageUrl = imageUrl;
}
public String getName() {
return mName;
}
public void setName(String name) {
mName = name;
}
public String getEmail() {
return mEmail;
}
public void setEmail(String email) {
mEmail = email;
}
public String getHname() {
return mHname;
}
public void setHouse(String house) {
mHname = house;
}
public String getHlocation() {
return mHlocation;
}
public void setLocation(String location) {
mHlocation = location;
}
public String getPhone() {
return mPhone;
}
public void setmPhone(String phone) {
mPhone = phone;
}
public String getImageUrl() {
return mImageUrl;
}
public void setImageUrl(String imageUrl) {
mImageUrl = imageUrl;
}
}
我不知道为什么会收到这样的错误,并且代码中也没有任何错误。 请帮助我。
答案 0 :(得分:0)
当您从Firebase存储中获取映像路径时,此方法将起作用
StorageReference storageReference = FirebaseStorage.getInstance().getReferenceFromUrl("storage ref url in string");
storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
//do your stuff- uri.toString() will give you download URL\\
}
});
这将在您上传图片时起作用
filepath.putFile(File).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
{
filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Uri downloadUrl = uri;
//Do what you want with the url
}
Toast.makeText(MtActivity.this, "Upload Done", Toast.LENGTH_LONG).show();
}
});
对于您的代码,将是这样的:
StorageReference fileReference = mStorageRef.child(phone.getText().toString().trim()
+ "." + getFileExtension(mImageUri));
fileReference.putFile(File).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
{
filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Uri downloadUrl = uri;
//Do what you want with the url
}
Toast.makeText(MtActivity.this, "Upload Done", Toast.LENGTH_LONG).show();
}
});