2

新しい Firebase Storage を使用して画像をアップロードし、ダウンロード URL を取得しています。ダウンロード URL を文字列に変換した後、それを使用してアクティビティに表示していますが、ダウンロード 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);
    }
4

1 に答える 1

3

画像を表示するには、ImageViewを使用する必要があります。

Picasso Library を使用して、 ImageViewで画像を表示できます。URL、URI、またはリソースを画像ビューで Picasso に渡すだけです。

お気に入り

Picasso.with(this).load(/* url of image */).into(/*your imageview id*/);

picasso を使用するには、Gradle に以下を追加する必要があります

compile 'com.squareup.picasso:picasso:2.5.2'

この回答を参照してください。

于 2016-05-30T12:04:52.353 に答える