0

I'm trying to load an image from firebase real time database using picasso, but it keeps displaying blank image... I got the same result when I tried using Glide. I tried to use this solution : Image not loading with picasso or glide..keeps displaying black background But it doesn't work for me

Here's the code :

activity_profile_info.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_profile"
    android:orientation="vertical"
    tools:context="com.example.schooltest.ProfileInfoActivity">

    <include layout="@layout/toolbar"/>

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="280dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="50dp"
        android:background="#11032E" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scrollView_account">

        <RelativeLayout
            android:id="@+id/linear_account"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="190dp"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginTop="100dp"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:text="@string/profile_details"
                        android:textStyle="bold"
                        android:textSize="20sp"/>

                    <com.google.android.material.textfield.TextInputLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textColorHint="@color/edit_text_bg">

                        <EditText
                            android:id="@id/et_name"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:hint="@string/name"
                            android:imeOptions="actionNext"
                            android:textColorHint="@color/edit_text_bg"/>

                    </com.google.android.material.textfield.TextInputLayout>

                    <com.google.android.material.textfield.TextInputLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textColorHint="@color/edit_text_bg">

                        <EditText
                            android:id="@+id/et_email_address"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:hint="@string/email_address_hint"
                            android:imeOptions="actionNext"
                            android:textColorHint="@color/edit_text_bg"/>

                    </com.google.android.material.textfield.TextInputLayout>

                    <com.google.android.material.textfield.TextInputLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textColorHint="@color/edit_text_bg">

                        <EditText
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:hint="@string/tel_number"
                            android:imeOptions="actionNext"
                            android:textColorHint="@color/edit_text_bg"/>

                    </com.google.android.material.textfield.TextInputLayout>


                </LinearLayout>
            </LinearLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="50dp"
                android:layout_alignParentTop="true"
                android:layout_alignParentStart="true">

                <de.hdodenhof.circleimageview.CircleImageView
                    android:id="@+id/circle_image"
                    android:layout_width="150dp"
                    android:layout_height="150dp"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="24dp"
                    android:hapticFeedbackEnabled="false"
                    app:civ_border_color="#92FFFFFF"
                    app:civ_border_width="2dp"
                    android:src="@drawable/avatar"
                    android:clickable="true"
                    android:focusable="true" />

                <TextView
                    android:id="@+id/name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/name"
                    android:layout_marginTop="16dp"
                    android:layout_marginBottom="16dp"
                    android:textSize="20sp"
                    android:textColor="#92FFFFFF"
                    android:textStyle="bold"
                    android:layout_centerHorizontal="true"
                    android:layout_below="@id/circle_image"/>

            </RelativeLayout>

        </RelativeLayout>

    </ScrollView>
</RelativeLayout>

ProfileInfoActivity

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.bumptech.glide.Glide;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;
import com.theartofdev.edmodo.cropper.CropImage;

import java.io.File;
import java.util.Random;

import de.hdodenhof.circleimageview.CircleImageView;

public class ProfileInfoActivity extends AppCompatActivity {


    private DatabaseReference mDatabaseReference;
    private FirebaseUser mCurrentUser;

    private CircleImageView mDisplayImage;
    private TextView mName;
    private EditText mEditName, mEmail;

    private static final int GALLERY_PICK = 1;

    private StorageReference mImageStorage;

    private ProgressDialog mProgressDialog;

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

        mDisplayImage = findViewById(R.id.circle_image);
        mName = findViewById(R.id.name);
        mEditName = findViewById(R.id.et_name);
        mEmail = findViewById(R.id.et_email_address);

        mImageStorage = FirebaseStorage.getInstance().getReference();

        mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();

        String userID = mCurrentUser.getUid();

        mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child(userID);
        mDatabaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                String name = dataSnapshot.child("name").getValue().toString();
                String et_name = dataSnapshot.child("name").getValue().toString();
                String et_email_adress = dataSnapshot.child("email").getValue().toString();
                String image = dataSnapshot.child("image").getValue().toString();
                String thumb_image = dataSnapshot.child("thumb_image").getValue().toString();

                mName.setText(name);
                mEditName.setText(et_name);
                mEmail.setText(et_email_adress);

                Picasso.get().load(image).into(mDisplayImage);


            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

        mDisplayImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent galleryIntent = new Intent();
                galleryIntent.setType("image/*");
                galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

                startActivityForResult(Intent.createChooser(galleryIntent, "SELECT IMAGE"), GALLERY_PICK);
            }
        });
    }

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

        if(requestCode == GALLERY_PICK && resultCode == RESULT_OK ) {
            Uri imageUri = data.getData();

            CropImage.activity(imageUri)
                    .setAspectRatio(1, 1)
                    .start(this);

            //Toast.makeText(ProfileInfoActivity.this, imageUri, Toast.LENGTH_SHORT).show();
        }

        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            if (resultCode == RESULT_OK) {

                mProgressDialog = new ProgressDialog(ProfileInfoActivity.this);
                mProgressDialog.setTitle("Uploading Image...");
                mProgressDialog.setMessage("Please wait while we upload and process the image");
                mProgressDialog.setCanceledOnTouchOutside(false);
                mProgressDialog.show();


                Uri resultUri = result.getUri();
                String current_user_id = mCurrentUser.getUid();

                StorageReference filepath = mImageStorage.child("profile_images").child(current_user_id + ".jpg");

                filepath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                        if(task.isSuccessful()){

                            String download_url = task.getResult().getStorage().getDownloadUrl().toString();

                            mDatabaseReference.child("image").setValue(download_url).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {

                                    if(task.isSuccessful()) {
                                        mProgressDialog.dismiss();

                                    }

                                }
                            });


                        } else {

                            Toast.makeText(ProfileInfoActivity.this, "Error in uploading: ", Toast.LENGTH_SHORT).show();
                            mProgressDialog.dismiss();

                        }
                    }
                });

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


}

Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.schooltest">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".ProfileInfoActivity" />
        <activity android:name=".ResetPasswordActivity" />
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity android:name=".login.LoginActivity" />
        <activity android:name=".login.SignupActivity" />
        <activity
            android:name=".FullscreenActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:theme="@style/FullscreenTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SettingsActivity"
            android:label="@string/setting"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
        <activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
            android:theme="@style/Base.Theme.AppCompat"/> <!-- optional (needed if default theme has no action bar) -->

    </application>

</manifest>

PS : Also, when I upload an image from device, download url is getting as com.google.android.gms.tasks.zzu@6534db7. So, all details were uploaded into realtime database correctly except image url.

Wael Jomni
  • 361
  • 1
  • 9
  • 1
    Try hard-coding a image link for `Picasso.get().load(image).into(mDisplayImage);` to test out whether the original source is an issue. Recently I encountered cases too when my pictures aren't displayed using Picasso. Tried replacing with a simpler image and it got loaded, even though my original image link was working except that it is a larger size, and probably from a slower website. Also got to do with the versions of Picasso you are using. Very strange issue. Solved my issue by switching over to Glide, but the reason for the fail is still unknown. – lurker0152 Mar 21 '20 at 10:50
  • The image is loaded when I hard-code an image link. I tried with Glide but the problem persists. – Wael Jomni Mar 21 '20 at 11:16
  • Please post database structure so we can understand properly – Ashish Mar 21 '20 at 12:06
  • See https://guides.codepath.com/android/Displaying-Images-with-the-Picasso-Library Try this: ``` Picasso.Builder builder = new Picasso.Builder(getApplicationContext()); builder.listener(new Picasso.Listener() { @Override public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) { exception.printStackTrace(); }); Picasso picasso = builder.build(); picasso.load(image).into(mDisplayImage); ``` Mine is this error `com.squareup.picasso.Downloader$ResponseException: 301 Moved Permanently` Have not figure out how to solve this :( – lurker0152 Mar 21 '20 at 12:40
  • For my 301 issue, I upgraded my library (from 2.5.2 to 2.71828) and my problem is solved. – lurker0152 Mar 21 '20 at 13:20
  • I think I found the main issue here. when I upload an image from device, download url is getting as com.google.android.gms.tasks.zzu@6534db7. So, all details were uploaded into realtime database correctly except image url. Anyone has an idea haw to modify the code ? – Wael Jomni Mar 21 '20 at 13:51

0 Answers0