gpt4 book ai didi

android - task.getResult().getDownloadUrl().toString() 方法不工作

转载 作者:行者123 更新时间:2023-11-30 05:02:17 25 4
gpt4 key购买 nike

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


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();
}
}


最佳答案

检查这个文档;为 getDownloadUrl() 设置完整的监听器 https://firebase.google.com/docs/storage/android/download-files?authuser=0#download_data_via_url

    //it is better not to use space in child name
StorageReference storageRef = FirebaseStorage.getInstance().getReference()
.child("ProfileImages")
.child(currentUserID + ".jpg")

storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
// Got the download URL for 'path/to/aFile'
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
}
});

关于android - task.getResult().getDownloadUrl().toString() 方法不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58054352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com