图像显示 Firebase 存储项目中的问题
Image showing issue in firebase storage project
我已经设置了 firebase 项目,我可以成功地在两个用户之间交换聊天消息,但是我无法显示用户的个人资料图片。当我在网络浏览器中打开此 url 图像时显示,但如果我在 android phone 中使用此 url,图像不显示。
我正在从另一个 activity 上传图片,然后我尝试从另一个 activity 上传图片 URL 获取图片。
下面是我用来从 Firebase 数据库中检索图像和其他数据的代码。我成功获得了 URL 的上传图片并将它们传递到适配器中,
我尝试使用 Picasso 或 Glide 通过使用此 URL.
来显示该图像
由于 URL 图片在 Web 浏览器中显示成功,但无法在 android 的 imageview 中显示此图片。
DatabaseReference mFirebaseDatabaseReference=FirebaseDatabase.getInstance().getReference();
mFirebaseDatabaseReference.child(USERPROFILE_CHILD).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if(FirebaseAuth.getInstance().getCurrentUser().getEmail().equalsIgnoreCase(dataSnapshot.child("email").getValue().toString())) {
} else {
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("email", dataSnapshot.child("email").getValue().toString());
try {
// Got uploaded image url by using dataSnapshot.child("photourl").getValue() and put it in hashMap
hashMap.put("photourl",dataSnapshot.child("photourl").getValue().toString());
} catch (Exception e) {
e.printStackTrace();
}
hashMap.put("uid", dataSnapshot.child("uid").getValue().toString());
hashMap.put("firebaseToken",dataSnapshot.child("firebaseToken").getValue().toString());
testData.add(hashMap);
adapter.notifyDataSetChanged();
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
这是我在图像视图中加载图像的自定义适配器代码:---
final ImageView profileimage = (ImageView) v.findViewById(R.id.profileImageView);
try {
String imageUrl = data.get(position).get("photourl");
if (imageUrl.startsWith("gs://")) {
final StorageReference storageReference = FirebaseStorage.getInstance()
.getReferenceFromUrl(imageUrl);
storageReference.getDownloadUrl().addOnCompleteListener(
new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
String downloadUrl = task.getResult().toString();
System.out.println("====download url===="+downloadUrl);
/*Glide.with(context)
.load(downloadUrl)
.into(profileimage);*/ Picasso.with(context).load(downloadUrl).into(profileimage);
} else {
Log.w("thank you", "Getting download url was not successful.",
task.getException());
}
}
});
} else {
Picasso.with(context)
.load(imageUrl)
.into(profileimage);
}
} catch (Exception e) {
e.printStackTrace();
}
给你!
首先转到 Firebase 规则
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write; //here You Are Allowing everyone to read and write
}
}
}
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null; //only auth person k read and write
}
}
}
然后添加 Glide 或 picasso 库。!
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.github.bumptech.glide:glide:3.6.1'
compile 'com.android.support:support-v4:25.1.0'
StorageReference filePath;
StorageReference mFStorage;
mFStorage=FirebaseStorage.getInstance().getReference();
String Id= UUID.randomUUID().toString();
filePath= mFStorage.child("images").child(Id+".png");
filePath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
String imageDownloadUrl=taskSnapshot.getDownloadUrl();
Glide.with(ctx)
.load(imageDownloadUrl)
.into(imageview);
Picasso.with(context).load(imageDownloadUrl.toString()).into(imageview);
尝试使用 Picasso 或 Glide 它将加载您的图像。!
我已经设置了 firebase 项目,我可以成功地在两个用户之间交换聊天消息,但是我无法显示用户的个人资料图片。当我在网络浏览器中打开此 url 图像时显示,但如果我在 android phone 中使用此 url,图像不显示。
我正在从另一个 activity 上传图片,然后我尝试从另一个 activity 上传图片 URL 获取图片。
下面是我用来从 Firebase 数据库中检索图像和其他数据的代码。我成功获得了 URL 的上传图片并将它们传递到适配器中, 我尝试使用 Picasso 或 Glide 通过使用此 URL.
来显示该图像由于 URL 图片在 Web 浏览器中显示成功,但无法在 android 的 imageview 中显示此图片。
DatabaseReference mFirebaseDatabaseReference=FirebaseDatabase.getInstance().getReference();
mFirebaseDatabaseReference.child(USERPROFILE_CHILD).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if(FirebaseAuth.getInstance().getCurrentUser().getEmail().equalsIgnoreCase(dataSnapshot.child("email").getValue().toString())) {
} else {
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("email", dataSnapshot.child("email").getValue().toString());
try {
// Got uploaded image url by using dataSnapshot.child("photourl").getValue() and put it in hashMap
hashMap.put("photourl",dataSnapshot.child("photourl").getValue().toString());
} catch (Exception e) {
e.printStackTrace();
}
hashMap.put("uid", dataSnapshot.child("uid").getValue().toString());
hashMap.put("firebaseToken",dataSnapshot.child("firebaseToken").getValue().toString());
testData.add(hashMap);
adapter.notifyDataSetChanged();
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
这是我在图像视图中加载图像的自定义适配器代码:---
final ImageView profileimage = (ImageView) v.findViewById(R.id.profileImageView);
try {
String imageUrl = data.get(position).get("photourl");
if (imageUrl.startsWith("gs://")) {
final StorageReference storageReference = FirebaseStorage.getInstance()
.getReferenceFromUrl(imageUrl);
storageReference.getDownloadUrl().addOnCompleteListener(
new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
String downloadUrl = task.getResult().toString();
System.out.println("====download url===="+downloadUrl);
/*Glide.with(context)
.load(downloadUrl)
.into(profileimage);*/ Picasso.with(context).load(downloadUrl).into(profileimage);
} else {
Log.w("thank you", "Getting download url was not successful.",
task.getException());
}
}
});
} else {
Picasso.with(context)
.load(imageUrl)
.into(profileimage);
}
} catch (Exception e) {
e.printStackTrace();
}
给你!
首先转到 Firebase 规则
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write; //here You Are Allowing everyone to read and write
}
}
}
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null; //only auth person k read and write
}
}
}
然后添加 Glide 或 picasso 库。!
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.github.bumptech.glide:glide:3.6.1'
compile 'com.android.support:support-v4:25.1.0'
StorageReference filePath;
StorageReference mFStorage;
mFStorage=FirebaseStorage.getInstance().getReference();
String Id= UUID.randomUUID().toString();
filePath= mFStorage.child("images").child(Id+".png");
filePath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
String imageDownloadUrl=taskSnapshot.getDownloadUrl();
Glide.with(ctx)
.load(imageDownloadUrl)
.into(imageview);
Picasso.with(context).load(imageDownloadUrl.toString()).into(imageview);
尝试使用 Picasso 或 Glide 它将加载您的图像。!