因此,当我上传图片时,我正在将图片保存到App中的数据库中。当我重新启动应用程序时,它会将其正确性上传到存储和数据库中,但是没有显示给我。我使用方法filechooser()选择文件,然后使用fileuploader()上传文件。效果很好。
enter code herepublic class ProfilBearbeiten extends AppCompatActivity {
Button bestätigen;
private SharedPreferences speicher;
private SharedPreferences.Editor editor;
TextView fullName,email;
EditText ort,beschreibung, telefonummer, interessen;
FirebaseAuth fAuth;
FirebaseFirestore fStore;
String userId;
ImageView user;
Uri imageurl;
StorageReference mStorageRef;
private StorageTask uploadtask;
private Bitmap compressor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profilbearbeiten);
fAuth = FirebaseAuth.getInstance();
fStore = FirebaseFirestore.getInstance();
FirebaseDatabase database = FirebaseDatabase.getInstance();
mStorageRef= FirebaseStorage.getInstance().getReference().child("Images");
DatabaseReference reference = database.getReference();
userId = fAuth.getCurrentUser().getUid();
fullName = findViewById(R.id.tv_name);
email = findViewById(R.id.tvEmail);
bestätigen = findViewById(R.id.btbestätigen);
ort = findViewById(R.id.tv_address);
telefonummer = findViewById(R.id.tvTel);
interessen = findViewById(R.id.tvInt);
beschreibung = findViewById(R.id.tvBesc);
user = findViewById(R.id.User);
user.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
filechooser();
// fileuploader();
Picasso.get().load(imageurl).into(user);
}
});
bestätigen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// get Information from Edit Text or fileuploader()
final String eemail = email.getText().toString().trim();
final String efullname = fullName.getText().toString();
final String eort = ort.getText().toString();
final String einteresssen = interessen.getText().toString();
final String ebeschreibung = beschreibung.getText().toString();
final String etelefonnummer = telefonummer.getText().toString();
final String bild = "";
final String image = imageurl != null ? imageurl.toString() : null;
userId = fAuth.getCurrentUser().getUid();
DocumentReference documentReference = fStore.collection("users").document(userId);
//save edited Information in Database
Map<String, Object> user = new HashMap<>();
user.put("Benutername", efullname);
user.put("EMail", eemail);
user.put("Ort", eort);
user.put("Interessen", einteresssen);
user.put("Beschreibung", ebeschreibung);
user.put("Telefonnummer", etelefonnummer);
user.put("Image", image);
documentReference.set(user);
Intent intent = new Intent(ProfilBearbeiten.this, Profile.class);
startActivity(intent);
fileuploader();
}
});
DocumentReference documentReference = fStore.collection("users").document(userId);
documentReference.addSnapshotListener(this, new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
fullName.setText(documentSnapshot.getString("Benutername"));
email.setText(documentSnapshot.getString("EMail"));
ort.setText(documentSnapshot.getString("Ort"));
telefonummer.setText(documentSnapshot.getString("Telefonnummer"));
interessen.setText(documentSnapshot.getString("Interessen"));
beschreibung.setText(documentSnapshot.getString("Beschreibung"));
Picasso.get().load(documentSnapshot.getString("Image")).into(user);
}
});
}
// uploads and open gallery on device
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode== 1 && resultCode==RESULT_OK && data != null && data.getData() != null){
imageurl=data.getData();
user.setImageURI(imageurl);
}
}
private void filechooser(){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1);
}
private String getExtension(Uri uri) {
ContentResolver cr = getContentResolver();
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
return mimeTypeMap.getExtensionFromMimeType(cr.getType(uri));
}
private void fileuploader () {
StorageReference Ref = mStorageRef.child(System.currentTimeMillis()+","+getExtension(imageurl));
uploadtask = Ref.putFile(imageurl)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Get a URL to the uploaded content
Toast.makeText(ProfilBearbeiten.this, "Image Uploaded", Toast.LENGTH_LONG).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
// ...
}
});
}
}
如您所见。该URL保存在数据库中的“图像”下。当我上传图像并切换其他页面时,当我回到此幻灯片时,将显示图像。但是,每当我关闭应用程序并重新启动´时,即使URL仍然以相同的方式出现在数据库中,图片也会消失。也许我的错误是负载如何? Picasso.get().load(documentSnapshot.getString("Image")).into(user);
您存储在数据库中的URL是将图像上传到Cloud Storage的设备上图像的本地URL。这是相机在其中写入文件的特定Android设备上的本地路径。
虽然此URL最初将在该特定设备上工作,但以后可能无法工作(因为该文件可能会被Android清理),并且它肯定不会在其他设备上工作(因为该映像不存在) 。
要拥有一个可以在任何地方任何时间使用的URL,您将要生成一个所谓的下载URL,并将that值存储在数据库中。您通常在上传图像后立即执行此操作,并调用getDownloadURL
:
StorageReference Ref = mStorageRef.child(System.currentTimeMillis()+","+getExtension(imageurl));
uploadtask = Ref.putFile(imageurl)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(ProfilBearbeiten.this, "Image Uploaded", Toast.LENGTH_LONG).show();
// Get a URL to the uploaded content
Ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
// Got the download URL, so write it to the database
userId = fAuth.getCurrentUser().getUid();
DocumentReference documentReference = fStore.collection("users").document(userId);
documentReference.update("Image", uri.toString());
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
// ...
}
});
另请参阅uploading files上的文档,该文档还显示了如何防止嵌套任务失败处理程序。