Storage firebase in android studio

Asked

Viewed 67 times

0

I’m starting programming for android and I have a project in android studio that creates registration with photo, I added firebase and not how to create the code to send this data with photo, name... If anyone could give me an example of how to do that, I’d appreciate it. follows the method I used to upload 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 send this image to Firebase Storage?

1 answer

1

Solved. I looked it up and got it this 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 );
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.