使用毕加索从Firebase数据库更新图像视图

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

标签: android firebase firebase-realtime-database firebase-storage picasso

我是编码的新手:我已经创建了锻炼应用,并在将用户个人资料图片存储到Firebase数据库后尝试更新用户个人资料图片 我尝试过使用毕加索和滑翔伞,无论我用什么,它都会不断产生相同的结果... 图像视图已保存到Firebase,但是当必须更新图像视图时,什么也没有发生...

我什至尝试更改Firebase存储和数据库规则.. 请帮助

此处是代码 清单:

<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: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_layout:

<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/profile21"></de.hdodenhof.circleimageview.CircleImageView>
        <!--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>

设置活动:

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(DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists())
                {
                    String image =dataSnapshot.child("profileimage").getValue().toString();
//
//                    Glide.with(SetupActivity.this)
//                            .load(image)
//                            .into(profileimage);


//                    Picasso.with(SetupActivity.this)
//                            .load(image)
//                            .fit()
//                            .placeholder(R.drawable.profile)
//                            .memoryPolicy(MemoryPolicy.NO_CACHE)
//                            .networkPolicy(NetworkPolicy.NO_CACHE)
//                            .into(profileimage);
                    Picasso.get().load(image).resize(100,100).centerCrop().placeholder(R.drawable.profile).into(profileimage);


                    }


            }

            @Override
            public void onCancelled(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(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)
                                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                                        @Override
                                        public void onComplete(Task<Void> task) {
                                            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();
            }

        }

    }

请帮助!

1 个答案:

答案 0 :(得分:0)

您必须将onSuccessListener添加到filePath.getDownloadUrl()。我在这里添加了代码

dummyData= [
    {
        brand: 'volcom',
        first_stock_amount: 100,
        total_income: 20,
        total_expend: 5,
        final_stock_amount: 7,
        expend: [{
            out_date: 1,
            out_amount:2,
        },
{
            out_date: 2,
            out_amount:3,
        }]
    },
     {
        brand: 'billabong',
         first_stock_amount: 300,
         total_income: 10,
         total_expend: 5,
         final_stock_amount: 7,
        expend: [{
            out_date: 2,
            out_amount:3
        }]
    },
     {
       brand: 'ripcurl',
         first_stock_amount: 200,
         total_income: 5,
         total_expend: 5,
         final_stock_amount: 7,
        expend: [{
            out_date: 3,
            out_amount:4
        }]
    },
];