HOW TO RETRIVED IMAGE FROM FIREBASE IN ANDROID

 HOW TO RETRIVED IMAGE FROM FIREBASE IN   ANDROID



  • add Dependences in build.gradle file
  • dependencies {
       
    // Import the BoM for the Firebase platform
        implementation platform
    ('com.google.firebase:firebase-bom:28.4.0')


       
    // Declare the dependency for the Cloud Storage library
       
    // When using the BoM, you don't specify versions in Firebase library dependencies
       
    implementation 'com.google.firebase:firebase-storage-latest version'//include latest version
  • Add Imageview in xml to retrived image
  • <ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.966"
    android:contentDescription="TODO" />
  •  Now come to Backend part (JAVA)
  • Create a reference to upload, download, or delete a file, or to get or update its metadata.
  • private StorageReference mStorageReference;
  • Next, you can create a reference to a location lower in the tree, say "images/space.jpg" by using the child() method on an existing reference.
  • mStorageReference = FirebaseStorage.getInstance().getReference().child("picture/pandey.png");
    try {
    final File localFile = File.createTempFile("pandey", "png");
    mStorageReference.getFile(localFile)
    .addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
    Toast.makeText(MainActivity.this,"Picture Retrieved",Toast.LENGTH_SHORT).show();
    Bitmap bitmap = BitmapFactory.decodeFile(localFile.getAbsolutePath());
    ((ImageView)findViewById(R.id.image)).setImageBitmap(bitmap);
    }
    }).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {

    Toast.makeText(MainActivity.this,"Error ",Toast.LENGTH_SHORT).show();
    }
    });
    } catch (IOException e) {
    e.printStackTrace();
    }




Comments

Popular posts from this blog

How to implement animation or lottie files in android.

Number is even or odd in java

22. Reverse the number and find number is Pallindrome or not.