图像未加载毕加索或滑行..keeps,显示黑色背景

时间:2019-01-21 16:00:35

标签: android firebase firebase-realtime-database picasso

我正在尝试使用毕加索从Firebase数据库加载图像,但是无论我如何尝试,它始终会做同样的事情... 我什至尝试使用滑行

这是我选择图片时发生的事情:

what's happening when I choose the image

请帮助。...

这是代码:

设置活动:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SetupActivity"
    android:background="@drawable/setup_background">


    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/profile_image"
        android:layout_width="170dp"
        android:layout_height="170dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="0dp"
        android:src="@drawable/profile"
        app:civ_border_color="@color/colorPrimaryDark"
        app:civ_border_width="1dp"/>

    <EditText
        android:id="@+id/setup_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="237dp"
        android:drawableLeft="@drawable/username"
        android:ems="10"
        android:hint="User Name"
        android:inputType="textMultiLine"
        android:textColor="#ffffff"
        android:textColorHint="@android:color/background_light"
        android:textSize="20sp"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/setup_fullname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/setup_username"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:drawableLeft="@drawable/name"
        android:ems="10"
        android:hint="Full Name"
        android:inputType="textMultiLine"
        android:textColor="#ffffff"
        android:textColorHint="@android:color/background_light"
        android:textSize="20sp"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/setup_country"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/setup_fullname"
        android:layout_alignParentStart="true"
        android:drawableLeft="@drawable/country"
        android:ems="10"
        android:hint="Country"
        android:inputType="textMultiLine"
        android:textColor="#ffffff"
        android:textColorHint="@android:color/background_light"
        android:textSize="20sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/setup_saveinformation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:textStyle="bold"
        android:textSize="18dp"
        android:layout_marginBottom="62dp"
        android:text="Save"
        android:background="@drawable/button"/>

</RelativeLayout> 

清单:

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

    <application
        android:allowBackup="true"
        android:configChanges="orientation|screenSize"
        android:hardwareAccelerated="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".RegisterActivity" />
        <activity android:name=".Login" />
        <activity android:name=".SetupActivity"></activity>
        <activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
            android:theme="@style/Base.Theme.AppCompat"/>
    </application>

setup_activity:

public class SetupActivity extends AppCompatActivity {
    private Button Save;
    private EditText Username,Userfullname,Usercountry;
    private CircleImageView profileimage;
    private FirebaseAuth mAuth;
    private DatabaseReference userRef;
    private String currentuserid;
    private ProgressDialog loadingbar;
    private static int GALLARY_PICK = 1;
    private StorageReference Userprofileimageref;

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

        mAuth=FirebaseAuth.getInstance();
        currentuserid=mAuth.getCurrentUser().getUid();
        userRef =FirebaseDatabase.getInstance().getReference().child("Users").child(currentuserid);
        Userprofileimageref = FirebaseStorage.getInstance().getReference().child("profile image");
        Save= findViewById(R.id.setup_saveinformation);
        Username= findViewById(R.id.setup_username);
        Userfullname= findViewById(R.id.setup_fullname);
        Usercountry= findViewById(R.id.setup_country);
        profileimage= findViewById(R.id.profile_image);
        loadingbar = new ProgressDialog(this);

        Save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveusersetupinfirmation();
            }
        });

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

        userRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                String image = dataSnapshot.child("profileimage").getValue(String.class);
                Picasso.get()
                        .load(image)
                        .placeholder(R.drawable.profile)
                        .into(profileimage);
            }

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

            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode ==GALLARY_PICK && resultCode== RESULT_OK && data!=null)
        {
            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) {

                loadingbar.setTitle("saving image");
                loadingbar.setMessage("please wait ....");
                loadingbar.show();
                loadingbar.setCanceledOnTouchOutside(true);
                Uri resultUri = result.getUri();
                final StorageReference filePath = Userprofileimageref.child(currentuserid + "jpg");

                filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onComplete(final Task<UploadTask.TaskSnapshot> task) {

                        if (task.isSuccessful())
                        {
                            Toast.makeText(SetupActivity.this,"Profile image stored successfully",Toast.LENGTH_SHORT).show();
                            final String downloadUrl = filePath.getDownloadUrl().toString();
                            userRef.child("profileimage").setValue(downloadUrl)
                                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                                        @Override
                                        public void onSuccess(Void aVoid) {
                                            if (task.isSuccessful())
                                            {
                                                Intent selfintent = new Intent(SetupActivity.this,SetupActivity.class);
                                                startActivity(selfintent);
                                                Toast.makeText(SetupActivity.this,"image saved successfully",Toast.LENGTH_SHORT).show();
                                                loadingbar.dismiss();

                                            }
                                            else
                                            {
                                                String message = task.getException().getMessage();
                                                Toast.makeText(SetupActivity.this,"Error occurred: "+ message,Toast.LENGTH_SHORT).show();
                                                loadingbar.dismiss();

                                            }

                                        }
                                    });
                        }

                    }
                });
            }else
            {
                Toast.makeText(SetupActivity.this,"image cannot be cropped",Toast.LENGTH_SHORT).show();
                loadingbar.dismiss();
            }

        }

    }

    private void saveusersetupinfirmation() {
        String username = Username.getText().toString();
        String fullname =Userfullname.getText().toString();
        String country = Usercountry.getText().toString();

        if (TextUtils.isEmpty(username))
        {
            Toast.makeText(SetupActivity.this,"please enter username...",Toast.LENGTH_SHORT).show();
        }
        if (TextUtils.isEmpty(fullname))
        {
            Toast.makeText(SetupActivity.this,"please enter full name...",Toast.LENGTH_SHORT).show();
        }
        if (TextUtils.isEmpty(country))
        {
            Toast.makeText(SetupActivity.this,"please enter country of origin...",Toast.LENGTH_SHORT).show();
        }
        else
            {
                loadingbar.setTitle("Login in");
                loadingbar.setMessage("please wait ....");
                loadingbar.show();
                loadingbar.setCanceledOnTouchOutside(true);

                HashMap userMap = new HashMap();
                userMap.put("username", username);
                userMap.put("fullname", fullname);
                userMap.put("country", country);
                userMap.put("status", "Hey there, i am using BFit fitness app...");
                userMap.put("gender", "none");
                userMap.put("dob", "none");
                userMap.put("relationshipstatus", "none");
                userMap.put("body goal","none");
                userRef.updateChildren(userMap).addOnCompleteListener(new OnCompleteListener() {
                    @Override
                    public void onComplete(@NonNull Task task) {
                        if (task.isSuccessful())
                        {
                            sendusertomainactivitty();
                            Toast.makeText(SetupActivity.this,"your information has been saved successfully",Toast.LENGTH_SHORT).show();
                            loadingbar.dismiss();
                        }
                        else
                            {
                                String message = task.getException().getMessage();
                                Toast.makeText(SetupActivity.this,"Error Occurred " +message,Toast.LENGTH_SHORT).show();
                                loadingbar.dismiss();
                            }

                    }
                });

            }
    }

    private void sendusertomainactivitty() {
        Intent mainintent = new Intent(SetupActivity.this,MainActivity.class);
        mainintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(mainintent);
        finish();
    }
}

1 个答案:

答案 0 :(得分:0)

使用以下行时

final String downloadUrl = filePath.getDownloadUrl().toString();

filePath.getDownloadUrl()返回任务ID(com.google.android.gms.tasks.zzu@f30b587) 然后将其保存到数据库中,而不是实际的url中,因此,当您使用以下行获取url(通过picasso加载图像)时,您只是在获取上传ID!没有可用的网址。

String image = dataSnapshot.child("profileimage").getValue(String.class);

要解决此问题,您需要在Firebase存储上传后在异步任务中获取下载URL。按照https://firebase.google.com/docs/storage/android/upload-files#get_a_download_url

然后将实际的url添加到firebase数据库中,该数据库将数据库链接到存储的图像:)我敢肯定还有一种更优雅的方法,但这可行。