简体   繁体   English

将带有文本(已连接)的图像上载到Firebase存储

[英]Upload Image with a Text (connected) to Firebase Storage

I upload an image to Firebase Storage with Android. 我使用Android将图像上传到Firebase存储。 I have an app which send image immediately after captured a photo. 我有一个应用程序,在拍摄照片后立即发送图像。

But I would like to create an app which show a photo in ImageView before sending. 但是我想在发送之前创建一个在ImageView中显示照片的应用程序。 I'd like to write some text under the photo and next, upload both of them. 我想在照片下面写一些文字然后上传两个。

Which tools should I use to do this? 我应该使用哪些工具来执行此操作? I've Picasso to download from Storage. 我从Picasso下载Storage。 But I can't manage how to show an ImageView after capturing a photo, next write some text(like name or description) and send it to Firebase. 但我无法管理如何在捕获照片后显示ImageView,然后编写一些文本(如名称或描述)并将其发送到Firebase。 The second challenge is how to show particular text with this image? 第二个挑战是如何使用此图像显示特定文本?

IMAGE <-> TEXT. 图片< - >文字。

In Firebase Storage I don't see some field to store image with a text. 在Firebase存储中,我没有看到一些用文本存储图像的字段。

在此输入图像描述

MainActivity.java: MainActivity.java:

 private ProgressDialog mProgress;
    private Bitmap bitmap;
    private Button mUploadBtn;
    private Button mSendBtn;
    private EditText mEditText;
    private ImageView mImageView;
    Uri picUri;

    private StorageReference mStorage;



    private static final int CAMERA_REQUEST_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mStorage = FirebaseStorage.getInstance().getReference();

        mEditText = (EditText) findViewById(R.id.editText);

        mSendBtn = (Button) findViewById(R.id.sendBtn);
        mUploadBtn = (Button) findViewById(R.id.uploadBtn);
        mImageView = (ImageView) findViewById(R.id.imageView);

        mProgress = new ProgressDialog(this);

        mUploadBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                File file=getOutputMediaFile(1);
                picUri = Uri.fromFile(file); // create
                i.putExtra(MediaStore.EXTRA_OUTPUT,picUri); // set the image file

                startActivityForResult(i, CAMERA_REQUEST_CODE);
            }
        });
    }

    public void UploadWithText(){

        String name = mEditText.getText().toString().trim();

    }


    /** Create a File for saving an image */
    private  File getOutputMediaFile(int type){
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "MyApplication");

        /**Create the storage directory if it does not exist*/
        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                return null;
            }
        }

        /**Create a media file name*/
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        if (type == 1){
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "IMG_"+ timeStamp + ".png");
        } else {
            return null;
        }

        return mediaFile;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


        if(requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK ) {

            mProgress.setMessage("Uploading Image...");
            mProgress.show();


            Uri uri = picUri;


            StorageReference filepath = mStorage.child("Photos").child(uri.getLastPathSegment());


            filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                    mProgress.dismiss();

                    Uri downloadUri = taskSnapshot.getDownloadUrl();

                    Picasso.with(MainActivity.this).load(downloadUri).fit().centerCrop().into(mImageView);

                    Toast.makeText(MainActivity.this, "Uploaded Successfully.", Toast.LENGTH_LONG).show();
                }
            });
        }
    }

Maybe there is a way that I can send the image to Firebase Storage but a Text(description) is sending to Database with a Name field(from Firebase Storage) and with this I can recognize which Text belongs to a particular image? 也许有一种方法可以将图像发送到Firebase存储,但文本(描述)通过名称字段(来自Firebase存储)发送到数据库,并且我可以识别哪个文本属于特定图像?

Could somebody help me to resolve this challenge? 有人可以帮我解决这个挑战吗?

Regards 问候

I can only answer broadly, since there's a bit too much code to answer specifically without doing it for you. 我只能广泛回答,因为有一些代码太多而无法为你做专门的回答。 In general, there are few ways to do this: 通常,有几种方法可以做到这一点:

  • Add text to the image file itself, upload the new image with changes made 将文本添加到图像文件本身,上传新图像并进行更改
  • Store text as an image attribute, upload the file and save attributes elsewhere 将文本存储为图像属性,上传文件并将属性保存在其他位置
    • You can either store the data in the Realtime Database, or 您可以将数据存储在实时数据库中,也可以
    • You can store the data in Storage file metadata (assuming pairs) 您可以将数据存储在存储文件元数据中(假设成对)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM