Storage firebase in android studio

0

I'm starting in programming for android and I have a project in android studio that creates photo registration, I added the firebase and I do not know how to create the code to send this data with photo, name ... if anyone can give me an example of how to do this I thank. follows the method I used to load the image (photo).

mImageView.setOnClickListener( new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
   public void onClick(View v) {
      ActivityCompat.requestPermissions( Produtos.this, new String[]{
      Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE_GALLERY );
    }
} );


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == REQUEST_CODE_GALLERY) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Intent galleryIntent = new Intent( Intent.ACTION_GET_CONTENT );
            galleryIntent.setType( "image/*" );
            startActivityForResult( galleryIntent, REQUEST_CODE_GALLERY );
        } else {
            Toast.makeText( this, "Você não tem permissão", Toast.LENGTH_SHORT ).show();
        }
        return;
    }
    super.onRequestPermissionsResult( requestCode, permissions, grantResults );
}


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

    if (requestCode == REQUEST_CODE_GALLERY && resultCode == RESULT_OK) {
        Uri imageUri = data.getData();
        CropImage.activity( imageUri )
                .setGuidelines( CropImageView.Guidelines.ON )
                .setAspectRatio( 1, 1 )
                .start( this );

    }
    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult( data );
        if (resultCode == RESULT_OK) {
            Uri resultUri = result.getUri();
            mImageView.setImageURI( resultUri );


        } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
            Exception error = result.getError();
        }
    }

    super.onActivityResult( requestCode, resultCode, data );
}

How to submit this image to Firebase Storage?

    
asked by anonymous 18.07.2018 / 20:17

1 answer

1

Solved. I researched and got it that way.

mImageView.setOnClickListener( new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View v) {
  ActivityCompat.requestPermissions( Produtos.this, new String[]{
  Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE_GALLERY );
}
} );


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull 
String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_CODE_GALLERY) {
    if (grantResults.length > 0 && grantResults[0] ==  
PackageManager.PERMISSION_GRANTED) {
        Intent galleryIntent = new Intent( Intent.ACTION_GET_CONTENT  
);
        galleryIntent.setType( "image/*" );
        startActivityForResult( galleryIntent, REQUEST_CODE_GALLERY  
);
    } else {
        Toast.makeText( this, "Você não tem permissão",  
Toast.LENGTH_SHORT ).show();
    }
    return;
}
super.onRequestPermissionsResult( requestCode, permissions, grantResults );
}


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

if (requestCode == REQUEST_CODE_GALLERY && resultCode == RESULT_OK) {
    Uri imageUri = data.getData();
    CropImage.activity( imageUri )
            .setGuidelines( CropImageView.Guidelines.ON )
            .setAspectRatio( 1, 1 )
            .start( this );

}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
    CropImage.ActivityResult result = CropImage.getActivityResult( data );
    if (resultCode == RESULT_OK) {
        Uri resultUri = result.getUri();
        mImageView.setImageURI( resultUri );


    } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
        Exception error = result.getError();
    }

 //ACRESCENTEI ESSAS LINHAS//
 String N="";
        N=mEdtName.getText().toString().trim();
        StorageReference mountainsRef = mStorageRef.child( N+".jpg" );
        StorageReference mountainImagesRef = mStorageRef.child( "images/mountains.jpg" );
        mountainsRef.getName().equals( mountainImagesRef.getName() );
        mImageView.setDrawingCacheEnabled(true);
        mImageView.buildDrawingCache();
        Bitmap bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] datafoto = baos.toByteArray();

        UploadTask uploadTask = mountainsRef.putBytes(datafoto);
        uploadTask.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                // Handle unsuccessful uploads
            }
        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                // taskSnapshot.getMetadata() contains file metadata such as size, content-type, etc.
                // ...
            }
        });
}

super.onActivityResult( requestCode, resultCode, data );
}
    
19.07.2018 / 00:01