问题
task.getResult().getDownloadUrl().toString() 方法不起作用,我是 android studio 中 android 领域的新手,有人可以帮我解决这个问题吗? ? ?这是我的 setupActivity 类的完整代码。
- package com.example.ihsan;
- import android.app.ProgressDialog;
- import android.content.Intent;
- import androidx.annotation.NonNull;
- import androidx.appcompat.app.AppCompatActivity;
- import android.net.Uri;
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import com.google.android.gms.tasks.OnCompleteListener;
- import com.google.android.gms.tasks.Task;
- import com.google.firebase.auth.FirebaseAuth;
- 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.Picasso;
- import com.theartofdev.edmodo.cropper.CropImage;
- import com.theartofdev.edmodo.cropper.CropImageView;
- import java.util.HashMap;
- import java.util.Objects;
- import de.hdodenhof.circleimageview.CircleImageView;
- public class SetupActivity extends AppCompatActivity {
- private EditText UserName, FullName, CountryName;
- private Button SaveInformationbuttion;
- private CircleImageView ProfileImage;
- private ProgressDialog loadingBar;
- private FirebaseAuth mAuth;
- private DatabaseReference UsersRef;
- private StorageReference UserProfileImageRef;
- String currentUserID;
- final static int Gallery_Pick = 1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_setup);
- mAuth = FirebaseAuth.getInstance();
- currentUserID = mAuth.getCurrentUser().getUid();
- UsersRef = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID);
- UserProfileImageRef = FirebaseStorage.getInstance().getReference().child("Profile Images");
- UserName = (EditText) findViewById(R.id.setup_username);
- FullName = (EditText) findViewById(R.id.setup_full_name);
- CountryName = (EditText) findViewById(R.id.setup_country_name);
- SaveInformationbuttion = (Button) findViewById(R.id.setup_information_button);
- ProfileImage = (CircleImageView) findViewById(R.id.setup_profile_image);
- loadingBar = new ProgressDialog(this);
- SaveInformationbuttion.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- SaveAccountSetupInformation();
- }
- });
- ProfileImage.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view)
- {
- Intent galleryIntent = new Intent();
- galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
- galleryIntent.setType("image/*");
- startActivityForResult(galleryIntent, Gallery_Pick);
- }
- });
- ProfileImage.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent galleryIntent = new Intent();
- galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
- galleryIntent.setType("image/*");
- startActivityForResult(galleryIntent, Gallery_Pick);
- }
- });
- UsersRef.addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
- if (dataSnapshot.exists()) {
- if (dataSnapshot.hasChild("profileimage")) {
- String image = dataSnapshot.child("profileimage").getValue().toString();
- Picasso.with(SetupActivity.this).load(image).placeholder(R.drawable.profile).into(ProfileImage);
- }
- }
- else
- {
- Toast.makeText(SetupActivity.this, "Please select profile image first...", Toast.LENGTH_SHORT).show();
- }
- }
- @Override
- public void onCancelled(@NonNull DatabaseError databaseError) {
- }
- });
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data)
- {
- super.onActivityResult(requestCode, resultCode, data);
- if(requestCode==Gallery_Pick && resultCode==RESULT_OK && data!=null)
- {
- Uri ImageUri = data.getData();
- CropImage.activity()
- .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("Profile Image");
- loadingBar.setMessage("Please wait, while we updating your profile image...");
- loadingBar.show();
- loadingBar.setCanceledOnTouchOutside(true);
- Uri resultUri = result.getUri();
- StorageReference filePath = UserProfileImageRef.child(currentUserID + ".jpg");
- filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
- @Override
- public void onComplete(@NonNull final Task<UploadTask.TaskSnapshot> task)
- {
- if(task.isSuccessful())
- {
- Toast.makeText(SetupActivity.this, "Profile Image stored successfully...", Toast.LENGTH_SHORT).show();
- final String downloadUrl = task.getResult().getDownloadUrl().toString();
- UsersRef.child("profileimage").setValue(downloadUrl)
- .addOnCompleteListener(new OnCompleteListener<Void>() {
- @Override
- public void onComplete(@NonNull Task<Void> task)
- {
- if(task.isSuccessful())
- {
- Intent selfIntent = new Intent(SetupActivity.this, SetupActivity.class);
- startActivity(selfIntent);
- Toast.makeText(SetupActivity.this, "Profile Image stored to 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(this, "Error Occurred: Image can not be cropped. Try Again.", Toast.LENGTH_SHORT).show();
- loadingBar.dismiss();
- }
- }
- }
- private void SaveAccountSetupInformation() {
- String username = UserName.getText().toString();
- String fullname = FullName.getText().toString();
- String country = CountryName.getText().toString();
- if (TextUtils.isEmpty(username)) {
- Toast.makeText(this, "Please write your username...", Toast.LENGTH_SHORT).show();
- }
- if (TextUtils.isEmpty(fullname)) {
- Toast.makeText(this, "Please write your full name...", Toast.LENGTH_SHORT).show();
- }
- if (TextUtils.isEmpty(country)) {
- Toast.makeText(this, "Please write your country...", Toast.LENGTH_SHORT).show();
- } else {
- loadingBar.setTitle("Saving Information");
- loadingBar.setMessage("Please wait, while we are creating your new Account...");
- 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'm using IHSAN , developed by Mohamed EL Ghazali.");
- userMap.put("gender", "none");
- userMap.put("dob", "none");
- userMap.put("relationshipstatus", "none");
- UsersRef.updateChildren(userMap).addOnCompleteListener(new OnCompleteListener() {
- @Override
- public void onComplete(@NonNull Task task)
- {
- if(task.isSuccessful())
- {
- SendUserToMainActivity();
- Toast.makeText(SetupActivity.this, "your Account is created Successfully.", Toast.LENGTH_LONG).show();
- loadingBar.dismiss();
- }
- else
- {
- String message = task.getException().getMessage();
- Toast.makeText(SetupActivity.this, "Error Occurred: " + message, Toast.LENGTH_SHORT).show();
- loadingBar.dismiss();
- }
- }
- });
- }
- }
- private void SendUserToMainActivity() {
- Intent mainIntent = new Intent(SetupActivity.this, MainActivity.class);
- mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
- startActivity(mainIntent);
- finish();
- }
- }
复制代码
task.getResult().getDownloadUrl().toString() 方法不起作用,我是 android studio 中 android 领域的新手,有人可以帮我解决这个问题吗? ? ?这是我的 setupActivity 类的完整代码。
回答
注意 task.getResult().getDownloadUrl()
现在已弃用,可以替换以下部分代码
- filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
- @Override
- public void onComplete(@NonNull final Task<UploadTask.TaskSnapshot> task)
- {
- if(task.isSuccessful())
- {
- Toast.makeText(SetupActivity.this, "Profile Image stored successfully...", Toast.LENGTH_SHORT).show();
- final String downloadUrl = task.getResult().getDownloadUrl().toString();
复制代码
有
- filePath.putFile(resultUri)
- .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
- @Override
- public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
- final Task<Uri> firebaseUri = taskSnapshot.getStorage().getDownloadUrl();
- firebaseUri.addOnSuccessListener(new OnSuccessListener<Uri>() {
- @Override
- public void onSuccess(Uri uri) {
- final String downloadUrl = uri.toString();
- // complete the rest of your code
- }
- });
- }
- });
复制代码
|