无法显示来自 Firebase 存储的图像
Unable to show images from Firebase Storage
我正在使用新的 Firebase 存储并上传图片,然后下载 url。将下载 url 转换为字符串后,我用它在我的 activity 中显示,但它只显示下载 url。
我正在使用以下代码上传图片并下载 url :
dialogBuilder.setTitle("Add Shop");
dialogBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//do something with edt.getText().toString();
Uri image_uri = getImageUri();
enteredShopName = shopName.getText().toString();
enteredShopDescription = shopDescription.getText().toString();
FirebaseStorage storage = FirebaseStorage.getInstance();
// Create a storage reference from our app
StorageReference storageRef = storage.getReferenceFromUrl("gs://myapp.com");
StorageReference imagesRef = storageRef.child("shop_images");
StorageReference selectimage_ref = imagesRef.child(image_uri.getLastPathSegment());
// upload file to firebase storage
selectimage_ref.putFile(image_uri).
addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri download_url = taskSnapshot.getDownloadUrl();
String string_download_url = download_url.toString();
Shop shop = new Shop(enteredShopName, enteredShopDescription,string_download_url);
listRef.push().setValue(shop, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if (databaseError != null) {
Log.e(TAG, "Failed to write message", databaseError.toException());
} else {
Toast.makeText(MainActivity.this, "Shop added successfully", Toast.LENGTH_SHORT).show();
}
}
});
}
});
获得 url 后,我将其设置为显示图像:
public void setImg_url(String string_download_url){
TextView field = (TextView) mView.findViewById(R.id.tv_shopImg);
field.setText(string_download_url);
}
为了显示图像,您应该使用 ImageView。
您可以使用 Picasso 库在 ImageView 中显示图像。您只需要将 url、uri 或资源传递给带有图像视图的 Picasso。
喜欢
Picasso.with(this).load(/* url of image */).into(/*your imageview id*/);
要使用毕加索,您需要在 Gradle
中添加以下内容
compile 'com.squareup.picasso:picasso:2.5.2'
看到这个。
我正在使用新的 Firebase 存储并上传图片,然后下载 url。将下载 url 转换为字符串后,我用它在我的 activity 中显示,但它只显示下载 url。 我正在使用以下代码上传图片并下载 url :
dialogBuilder.setTitle("Add Shop");
dialogBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//do something with edt.getText().toString();
Uri image_uri = getImageUri();
enteredShopName = shopName.getText().toString();
enteredShopDescription = shopDescription.getText().toString();
FirebaseStorage storage = FirebaseStorage.getInstance();
// Create a storage reference from our app
StorageReference storageRef = storage.getReferenceFromUrl("gs://myapp.com");
StorageReference imagesRef = storageRef.child("shop_images");
StorageReference selectimage_ref = imagesRef.child(image_uri.getLastPathSegment());
// upload file to firebase storage
selectimage_ref.putFile(image_uri).
addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri download_url = taskSnapshot.getDownloadUrl();
String string_download_url = download_url.toString();
Shop shop = new Shop(enteredShopName, enteredShopDescription,string_download_url);
listRef.push().setValue(shop, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if (databaseError != null) {
Log.e(TAG, "Failed to write message", databaseError.toException());
} else {
Toast.makeText(MainActivity.this, "Shop added successfully", Toast.LENGTH_SHORT).show();
}
}
});
}
});
获得 url 后,我将其设置为显示图像:
public void setImg_url(String string_download_url){
TextView field = (TextView) mView.findViewById(R.id.tv_shopImg);
field.setText(string_download_url);
}
为了显示图像,您应该使用 ImageView。
您可以使用 Picasso 库在 ImageView 中显示图像。您只需要将 url、uri 或资源传递给带有图像视图的 Picasso。
喜欢
Picasso.with(this).load(/* url of image */).into(/*your imageview id*/);
要使用毕加索,您需要在 Gradle
中添加以下内容compile 'com.squareup.picasso:picasso:2.5.2'
看到这个