我正在使用新的Firebase存储并上传图片,然后获取下载网址。将下载网址转换为字符串后,我正在使用它显示在我的活动中,但它只显示下载网址。我正在使用以下代码上传图像并获取下载网址:
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();
}
}
});
}
});
获得网址后,我将其设置为显示图像:
public void setImg_url(String string_download_url){
TextView field = (TextView) mView.findViewById(R.id.tv_shopImg);
field.setText(string_download_url);
}
为了显示图像,您应该使用ImageView。
您可以使用毕加索资料库在 ImageView 中显示图像。您只需要使用图像视图将网址、uri 或资源传递给毕加索。
喜欢
Picasso.with(this).load(/* url of image */).into(/*your imageview id*/);
要使用毕加索,您需要在 Gradle 中添加以下内容
compile 'com.squareup.picasso:picasso:2.5.2'
看到这个答案。