如何设置图片上传按钮?

时间:2017-05-31 20:46:44

标签: android firebase upload photo firebase-storage

我想构建这个应用程序,您可以从相机或图库中选择一张图片。此图片未设置为ImageView。现在我如何使用uploadbutton构建一个OnClickListener,它将图像上传到firebase?

public class MainActivity extends AppCompatActivity {

private StorageReference mStorage;
private static final int GALLERY_INTENT = 2;
private ImageButton mCamera;
private ImageButton mGallery;
private ImageView mImageview;
private static final int CAMERA_REQUEST_CODE = 1;
private ImageButton mUpload;


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

    mStorage = FirebaseStorage.getInstance().getReference();
    mGallery = (ImageButton) findViewById(R.id.UpGallery);

  mUpload = (ImageButton) findViewById(R.id.UpImage);

    mCamera = (ImageButton) findViewById(R.id.UpCamera);
    mImageview = (ImageView) findViewById(R.id.imageView1);
    mImageview.buildDrawingCache();
    mImageview.setDrawingCacheEnabled(true);
    mImageview.buildDrawingCache();




    final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
    File newdir = new File(dir);
    newdir.mkdirs();


    mGallery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType("image/*");
            startActivityForResult(intent, GALLERY_INTENT);
        }
    });

    mCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent1, CAMERA_REQUEST_CODE);
        }
    });

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


                }

    });
}

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

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

        final Uri uri = data.getData();
                Picasso.with(MainActivity.this).load(uri).fit().centerCrop().into(mImageview);

  }


    if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK){
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        mImageview.setImageBitmap(photo);

        Uri uri1 = data.getData();

                Picasso.with(MainActivity.this).load(uri1).fit().centerCrop().into(mImageview);



   }
    }


}

1 个答案:

答案 0 :(得分:0)

    final StorageReference storageRef = FirebaseStorage.getInstance().getReferenceFromUrl("gs://{your-storage-name-from-firebase}.appspot.com");

    mImageview.setDrawingCacheEnabled(true);
    mImageview.buildDrawingCache();
    Bitmap bitmap = mImageview .getDrawingCache();

    ByteArrayOutputStream baas = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baas);

    byte[] data = baas.toByteArray();

    UploadTask uploadTask = storageRef.child("{image-folder}").child("unique-id-image").putBytes(data);

    uploadTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            Toast.makeText(MainActivity.this, "Picture upload failed", Toast.LENGTH_LONG).show();
        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            String downloadUrl = taskSnapshot.getDownloadUrl();
        }
    });

希望这有帮助。