【发布时间】:2019-06-11 01:15:33
【问题描述】:
我是编码新手:我正在创建一个锻炼应用程序,并在我已经将用户个人资料图片存储到 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>
设置布局:
<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();
}
}
}
请帮忙!!!
【问题讨论】:
-
您是否记录了数据快照图像?它返回什么?它有错误吗?如果是这样,错误是什么?你确定事件正在触发吗?
-
这行代码返回
dataSnapshot.child("profileimage").getValue().toString()是什么?请回复@AlexMamo 并添加您的数据库结构。 -
@AlexMamo 它假设从上传的 firebase 数据库中返回图像 url ...
-
@ivan 不,我没有在数据快照图像中添加任何日志...我试试看
标签: android firebase firebase-realtime-database firebase-storage picasso