Firebase上传

时间:2017-06-15 06:40:11

标签: android firebase firebase-storage

编程新手。我想从图库中选择一个图像,然后将它们上传到firebase以供日后使用。从库中选择图像的选项正在运行,但它没有上传到数据库。

package example.com.myapplication;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import android.net.Uri;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;


import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;

import java.io.File;


public class Profile extends AppCompatActivity {

private EditText t1,t2,t3,t4,t5;
private Button b1;
private static final int PICK_IMAGE_REQUEST = 234;
private Spinner s1,s2,s3,s4,s5;
ArrayAdapter<CharSequence> adapter;
Context cntx;





//Buttons
private ImageButton pb1;
// Context cntx;
private Uri filePath;
private StorageReference mStorageRef;




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




    pb1 = (ImageButton)findViewById(R.id.pickimage);

    pb1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_PICK);
            startActivityForResult(Intent.createChooser(intent, "Select     Image"), PICK_IMAGE_REQUEST);

        }


    });




    b1 = (Button)findViewById(R.id.ProfSubmit);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {



            t1 = (EditText)findViewById(R.id.FirstNameField);
            t2 = (EditText)findViewById(R.id.LastNameField);
            t3 = (EditText)findViewById(R.id.AgeField);
            t4 = (EditText)findViewById(R.id.WL1);
            t5 = (EditText)findViewById(R.id.WL2);

            s1 = (Spinner)findViewById(R.id.spinner);
            s2 = (Spinner)findViewById(R.id.spinner2);
            s3 = (Spinner)findViewById(R.id.spinner3);
            s4 = (Spinner)findViewById(R.id.spinner5);
            s5 = (Spinner) findViewById(R.id.spinner5);

            adapter = ArrayAdapter.createFromResource(Profile.this,R.array.craftmenu,android.R.layout.simple_spinner_item);
            s1.setAdapter(adapter);
            s2.setAdapter(adapter);
            s3.setAdapter(adapter);
            s4.setAdapter(adapter);
            s5.setAdapter(adapter);

            FirebaseDatabase User1 = FirebaseDatabase.getInstance();
            DatabaseReference Name = User1.getReference("Fname");
            DatabaseReference LName = User1.getReference("LName");
            DatabaseReference age = User1.getReference("age");
            DatabaseReference Work1 = User1.getReference("WorkLink1");
            DatabaseReference Work2 = User1.getReference("WorkLink2");
            final DatabaseReference Craft1 = User1.getReference("Craft1");
            final DatabaseReference Craft2 = User1.getReference("Craft2");
            final DatabaseReference Craft3 = User1.getReference("Craft3");
            final DatabaseReference Craft4 = User1.getReference("Craft4");
            final DatabaseReference Craft5 = User1.getReference("Craft5");
            mStorageRef = FirebaseStorage.getInstance().getReference();

            Uri filePath = Uri.fromFile(new File("images/*"));
            StorageReference profimg = mStorageRef.("refimage");

            profimg.putFile(filePath).addOnCompleteListener(new OnSuccessListener<UploadTask.TaskSnapshot>(){
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Uri filepath = taskSnapshot.getDownloadUrl();
                }
            })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(cntx, "Upload Failed", Toast.LENGTH_SHORT).show();
                        }
                    });




            Name.setValue(t1.getText().toString());
            LName.setValue(t2.getText().toString());
            age.setValue(t3.getText().toString());
            Work1.setValue(t4.getText().toString());
            Work2.setValue(t5.getText().toString());
          /*  Craft1.setValue(s1.getOnItemSelectedListener().toString());
            Craft2.setValue(s2.getOnItemSelectedListener().toString());
            Craft3.setValue(s3.getOnItemSelectedListener().toString());
            Craft4.setValue(s4.getOnItemSelectedListener().toString());
            Craft5.setValue(s5.getOnItemSelectedListener().toString()); */
            s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    Craft1.setValue(s1.getOnItemSelectedListener().toString());
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    Craft1.setValue(null);

                }
            });

            s2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    Craft2.setValue(s2.getOnItemSelectedListener().toString());
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    Craft2.setValue(null);

                }
            });

            s3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    Craft3.setValue(s3.getOnItemSelectedListener().toString());
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    Craft3.setValue(null);

                }
            });

            s4.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    Craft4.setValue(s4.getOnItemSelectedListener().toString());
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    Craft4.setValue(null);

                }
            });
            s5.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    Craft5.setValue(s5.getOnItemSelectedListener().toString());
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    Craft5.setValue(null);

                }
            });










            startActivity(new Intent(Profile.this, HomeScreen.class));
        }
    });












// ...
    }
}

1 个答案:

答案 0 :(得分:0)

希望这可以帮助你...如果你愿意,你可以拿出短信...并下载uri请评论,以便我可以回复你!<​​/ p>

package org.stackanswer.*******8;

import android.app.Notification;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuthException;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;

import java.net.URI;

public class PostActivity4Home extends AppCompatActivity {

private ImageButton mSelectImage;

private EditText mPostTitle;
private EditText mPostDesc;
//if you have textviews

private Button mSubmitBtn;
private Uri mImageUri = null;

private static final int  GALLERY_REQUEST = 1;

private StorageReference mStorage;
private DatabaseReference mDatabase;
private ProgressDialog mProgress;


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

    mStorage = FirebaseStorage.getInstance().getReference();
    mDatabase = FirebaseDatabase.getInstance().getReference().child("Home");

    mSelectImage = (ImageButton)findViewById(R.id.imageSelect);
    mPostTitle = (EditText) findViewById(R.id.titleField);
    mPostDesc = (EditText) findViewById(R.id.descField);

    mSubmitBtn = (Button) findViewById(R.id.Submitbtn);

    mProgress = new ProgressDialog(this);

    mSelectImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
            galleryIntent.setType("image/*");
            startActivityForResult(galleryIntent, GALLERY_REQUEST);
        }
    });


    mSubmitBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startPosting();
        }
    });

}

private void startPosting() {

    mProgress.setMessage("Posting...");
    mProgress.show();

     final String title_val = mPostTitle.getText().toString().trim();
     final String desc_val = mPostDesc.getText().toString().trim();

    if(!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(desc_val) && mImageUri != null){

        StorageReference filepath = mStorage.child("HomePostImages").child(mImageUri.getLastPathSegment());
        filepath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                Uri downloadUrl = taskSnapshot.getDownloadUrl();
                mProgress.dismiss();

                DatabaseReference newPost = mDatabase.push();
                newPost.child("Title").setValue(title_val);
                newPost.child("Desc").setValue(desc_val);
                newPost.child("Image").setValue(downloadUrl.toString());


                startActivity(new Intent(PostActivity4Home.this, MainActivity.class));
            }
        });
    }
}

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

    if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK);
     mImageUri = data.getData();

    mSelectImage.setImageURI(mImageUri);
}

}