[软件设计/软件工程] task.getResult().getDownloadUrl().toString() 方法不起作用

[复制链接]
发表于 2022-5-3 10:20:44
问题
task.getResult().getDownloadUrl().toString() 方法不起作用,我是 android studio 中 android 领域的新手,有人可以帮我解决这个问题吗? ? ?这是我的 setupActivity 类的完整代码。
  1. package com.example.ihsan;

  2. import android.app.ProgressDialog;
  3. import android.content.Intent;
  4. import androidx.annotation.NonNull;
  5. import androidx.appcompat.app.AppCompatActivity;

  6. import android.net.Uri;
  7. import android.os.Bundle;
  8. import android.text.TextUtils;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.Toast;

  13. import com.google.android.gms.tasks.OnCompleteListener;
  14. import com.google.android.gms.tasks.Task;
  15. import com.google.firebase.auth.FirebaseAuth;
  16. import com.google.firebase.database.DataSnapshot;
  17. import com.google.firebase.database.DatabaseError;
  18. import com.google.firebase.database.DatabaseReference;
  19. import com.google.firebase.database.FirebaseDatabase;
  20. import com.google.firebase.database.ValueEventListener;
  21. import com.google.firebase.storage.FirebaseStorage;
  22. import com.google.firebase.storage.StorageReference;
  23. import com.google.firebase.storage.UploadTask;
  24. import com.squareup.picasso.Picasso;
  25. import com.theartofdev.edmodo.cropper.CropImage;
  26. import com.theartofdev.edmodo.cropper.CropImageView;

  27. import java.util.HashMap;
  28. import java.util.Objects;

  29. import de.hdodenhof.circleimageview.CircleImageView;

  30. public class SetupActivity extends AppCompatActivity {

  31.     private EditText UserName, FullName, CountryName;
  32.     private Button SaveInformationbuttion;
  33.     private CircleImageView ProfileImage;
  34.     private ProgressDialog loadingBar;

  35.     private FirebaseAuth mAuth;
  36.     private DatabaseReference UsersRef;
  37.     private StorageReference UserProfileImageRef;

  38.     String currentUserID;
  39.     final static int Gallery_Pick = 1;

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


  44.         mAuth = FirebaseAuth.getInstance();
  45.         currentUserID = mAuth.getCurrentUser().getUid();
  46.         UsersRef = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID);
  47.         UserProfileImageRef = FirebaseStorage.getInstance().getReference().child("Profile Images");


  48.         UserName = (EditText) findViewById(R.id.setup_username);
  49.         FullName = (EditText) findViewById(R.id.setup_full_name);
  50.         CountryName = (EditText) findViewById(R.id.setup_country_name);
  51.         SaveInformationbuttion = (Button) findViewById(R.id.setup_information_button);
  52.         ProfileImage = (CircleImageView) findViewById(R.id.setup_profile_image);
  53.         loadingBar = new ProgressDialog(this);


  54.         SaveInformationbuttion.setOnClickListener(new View.OnClickListener() {
  55.             @Override
  56.             public void onClick(View view) {
  57.                 SaveAccountSetupInformation();
  58.             }
  59.         });



  60.         ProfileImage.setOnClickListener(new View.OnClickListener() {
  61.             @Override
  62.             public void onClick(View view)
  63.             {
  64.                 Intent galleryIntent = new Intent();
  65.                 galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
  66.                 galleryIntent.setType("image/*");
  67.                 startActivityForResult(galleryIntent, Gallery_Pick);
  68.             }
  69.         });



  70.         ProfileImage.setOnClickListener(new View.OnClickListener() {
  71.             @Override
  72.             public void onClick(View view) {
  73.                 Intent galleryIntent = new Intent();
  74.                 galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
  75.                 galleryIntent.setType("image/*");
  76.                 startActivityForResult(galleryIntent, Gallery_Pick);
  77.             }
  78.         });

  79.         UsersRef.addValueEventListener(new ValueEventListener() {
  80.             @Override
  81.             public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

  82.                 if (dataSnapshot.exists()) {
  83.                     if (dataSnapshot.hasChild("profileimage")) {
  84.                         String image = dataSnapshot.child("profileimage").getValue().toString();
  85.                         Picasso.with(SetupActivity.this).load(image).placeholder(R.drawable.profile).into(ProfileImage);
  86.                     }
  87.                 }
  88.                 else
  89.                 {
  90.                     Toast.makeText(SetupActivity.this, "Please select profile image first...", Toast.LENGTH_SHORT).show();
  91.                 }

  92.                 }


  93.             @Override
  94.             public void onCancelled(@NonNull DatabaseError databaseError) {

  95.             }
  96.         });

  97.     }

  98.     @Override
  99.     protected void onActivityResult(int requestCode, int resultCode, Intent data)
  100.     {
  101.         super.onActivityResult(requestCode, resultCode, data);

  102.         if(requestCode==Gallery_Pick && resultCode==RESULT_OK && data!=null)
  103.         {
  104.             Uri ImageUri = data.getData();

  105.             CropImage.activity()
  106.                     .setGuidelines(CropImageView.Guidelines.ON)
  107.                     .setAspectRatio(1, 1)
  108.                     .start(this);
  109.         }

  110.         if(requestCode==CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
  111.         {
  112.             CropImage.ActivityResult result = CropImage.getActivityResult(data);

  113.             if(resultCode == RESULT_OK)
  114.             {
  115.                 loadingBar.setTitle("Profile Image");
  116.                 loadingBar.setMessage("Please wait, while we updating your profile image...");
  117.                 loadingBar.show();
  118.                 loadingBar.setCanceledOnTouchOutside(true);

  119.                 Uri resultUri = result.getUri();

  120.                 StorageReference filePath = UserProfileImageRef.child(currentUserID + ".jpg");

  121.                 filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
  122.                     @Override
  123.                     public void onComplete(@NonNull final Task<UploadTask.TaskSnapshot> task)
  124.                     {
  125.                         if(task.isSuccessful())
  126.                         {
  127.                             Toast.makeText(SetupActivity.this, "Profile Image stored successfully...", Toast.LENGTH_SHORT).show();

  128.                             final String downloadUrl = task.getResult().getDownloadUrl().toString();







  129.                             UsersRef.child("profileimage").setValue(downloadUrl)
  130.                                     .addOnCompleteListener(new OnCompleteListener<Void>() {
  131.                                         @Override
  132.                                         public void onComplete(@NonNull Task<Void> task)
  133.                                         {
  134.                                             if(task.isSuccessful())
  135.                                             {
  136.                                                 Intent selfIntent = new Intent(SetupActivity.this, SetupActivity.class);
  137.                                                 startActivity(selfIntent);

  138.                                                 Toast.makeText(SetupActivity.this, "Profile Image stored to  Successfully...", Toast.LENGTH_SHORT).show();
  139.                                                 loadingBar.dismiss();
  140.                                             }
  141.                                             else
  142.                                             {
  143.                                                 String message = task.getException().getMessage();
  144.                                                 Toast.makeText(SetupActivity.this, "Error Occurred: " + message, Toast.LENGTH_SHORT).show();
  145.                                                 loadingBar.dismiss();
  146.                                             }
  147.                                         }
  148.                                     });
  149.                         }
  150.                     }
  151.                 });
  152.             }
  153.             else
  154.             {
  155.                 Toast.makeText(this, "Error Occurred: Image can not be cropped. Try Again.", Toast.LENGTH_SHORT).show();
  156.                 loadingBar.dismiss();
  157.             }
  158.         }
  159.     }

  160.     private void SaveAccountSetupInformation() {

  161.         String username = UserName.getText().toString();
  162.         String fullname = FullName.getText().toString();
  163.         String country = CountryName.getText().toString();


  164.         if (TextUtils.isEmpty(username)) {
  165.             Toast.makeText(this, "Please write your username...", Toast.LENGTH_SHORT).show();
  166.         }
  167.         if (TextUtils.isEmpty(fullname)) {
  168.             Toast.makeText(this, "Please write your full name...", Toast.LENGTH_SHORT).show();
  169.         }
  170.         if (TextUtils.isEmpty(country)) {
  171.             Toast.makeText(this, "Please write your country...", Toast.LENGTH_SHORT).show();
  172.         } else {
  173.             loadingBar.setTitle("Saving Information");
  174.             loadingBar.setMessage("Please wait, while we are creating your new Account...");
  175.             loadingBar.show();
  176.             loadingBar.setCanceledOnTouchOutside(true);

  177.             HashMap userMap = new HashMap();
  178.             userMap.put("username", username);
  179.             userMap.put("fullname", fullname);
  180.             userMap.put("country", country);
  181.             userMap.put("status", "Hey there, I'm using IHSAN , developed by Mohamed EL Ghazali.");
  182.             userMap.put("gender", "none");
  183.             userMap.put("dob", "none");
  184.             userMap.put("relationshipstatus", "none");
  185.             UsersRef.updateChildren(userMap).addOnCompleteListener(new OnCompleteListener() {
  186.                 @Override
  187.                 public void onComplete(@NonNull Task task)
  188.                 {
  189.                     if(task.isSuccessful())
  190.                     {
  191.                         SendUserToMainActivity();
  192.                         Toast.makeText(SetupActivity.this, "your Account is created Successfully.", Toast.LENGTH_LONG).show();
  193.                         loadingBar.dismiss();
  194.                     }
  195.                     else
  196.                     {
  197.                         String message =  task.getException().getMessage();
  198.                         Toast.makeText(SetupActivity.this, "Error Occurred: " + message, Toast.LENGTH_SHORT).show();
  199.                         loadingBar.dismiss();
  200.                     }
  201.                 }
  202.             });

  203.         }
  204.     }

  205.     private void SendUserToMainActivity() {

  206.         Intent mainIntent = new Intent(SetupActivity.this, MainActivity.class);
  207.         mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  208.         startActivity(mainIntent);
  209.         finish();
  210.     }
  211. }
复制代码

task.getResult().getDownloadUrl().toString() 方法不起作用,我是 android studio 中 android 领域的新手,有人可以帮我解决这个问题吗? ? ?这是我的 setupActivity 类的完整代码。

回答
注意 task.getResult().getDownloadUrl()

现在已弃用,可以替换以下部分代码
  1. filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
  2.     @Override
  3.     public void onComplete(@NonNull final Task<UploadTask.TaskSnapshot> task)
  4.     {
  5.         if(task.isSuccessful())
  6.         {
  7.             Toast.makeText(SetupActivity.this, "Profile Image stored successfully...", Toast.LENGTH_SHORT).show();

  8.             final String downloadUrl = task.getResult().getDownloadUrl().toString();
复制代码


  1. filePath.putFile(resultUri)
  2.         .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
  3.             @Override
  4.             public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
  5.                 final Task<Uri> firebaseUri = taskSnapshot.getStorage().getDownloadUrl();
  6.                 firebaseUri.addOnSuccessListener(new OnSuccessListener<Uri>() {
  7.                     @Override
  8.                     public void onSuccess(Uri uri) {
  9.                         final String downloadUrl = uri.toString();
  10.                         // complete the rest of your code
  11.                     }
  12.                 });

  13.             }
  14.         });
复制代码






上一篇:在颤动的按钮上创建图像?
下一篇:未调用 FragmentPagerAdapter getItem

使用道具 举报

Archiver|手机版|小黑屋|吾爱开源 |网站地图

Copyright 2011 - 2012 Lnqq.NET.All Rights Reserved( ICP备案粤ICP备14042591号-1粤ICP14042591号 )

关于本站 - 版权申明 - 侵删联系 - Ln Studio! - 广告联系

本站资源来自互联网,仅供用户测试使用,相关版权归原作者所有

快速回复 返回顶部 返回列表