gpt4 book ai didi

java - 未将图像 URL 字符串获取到 FirebaseDatabase

转载 作者:行者123 更新时间:2023-12-01 16:28:27 26 4
gpt4 key购买 nike

我希望我的 Firebase 数据库使用用户个人资料图片 URL 进行初始化我尝试添加此内容,但我认为 profileImageUrl 变量未使用 ImageUri 变量进行初始化?请帮助完成这件事...

我只想使用来自 URI 变量的图像 url 字符串初始化第 5 个变量配置文件图像链接,但它没有初始化

这是我的项目logcat

2020-05-31 11:23:23.475 23993-23993/com.ramzan.snnstocks D/WindowClient: Add to mViews: DecorView@3fca5d8[UserRegistration], this = android.view.WindowManagerGlobal@878094
2020-05-31 11:23:23.479 23993-23993/com.ramzan.snnstocks D/ViewRootImpl[UserRegistration]: hardware acceleration is enabled, this = ViewRoot{2607f29 com.ramzan.snnstocks/com.ramzan.snnstocks.UserRegistration,ident = 2}
2020-05-31 11:23:23.482 23993-23993/com.ramzan.snnstocks V/PhoneWindow: DecorView setVisiblity: visibility = 0, Parent = ViewRoot{2607f29 com.ramzan.snnstocks/com.ramzan.snnstocks.UserRegistration,ident = 2}, this = DecorView@3fca5d8[UserRegistration]
2020-05-31 11:23:23.688 23993-24035/com.ramzan.snnstocks D/OpenGLRenderer: CanvasContext() 0x90c11000 initialize window=0x90a6f700, title=com.ramzan.snnstocks/com.ramzan.snnstocks.UserRegistration
2020-05-31 11:23:26.588 23993-23993/com.ramzan.snnstocks V/PhoneWindow: DecorView setVisiblity: visibility = 4, Parent = ViewRoot{2607f29 com.ramzan.snnstocks/com.ramzan.snnstocks.UserRegistration,ident = 2}, this = DecorView@3fca5d8[UserRegistration]
2020-05-31 11:23:32.551 23993-23993/com.ramzan.snnstocks I/UserRegistration: OnActivityResult: RESULT OK
2020-05-31 11:23:32.566 23993-23993/com.ramzan.snnstocks V/PhoneWindow: DecorView setVisiblity: visibility = 0, Parent = ViewRoot{2607f29 com.ramzan.snnstocks/com.ramzan.snnstocks.UserRegistration,ident = 2}, this = DecorView@3fca5d8[UserRegistration]
2020-05-31 11:23:32.596 23993-24035/com.ramzan.snnstocks D/OpenGLRenderer: CanvasContext() 0x90c11000 initialize window=0x90a6f700, title=com.ramzan.snnstocks/com.ramzan.snnstocks.UserRegistration
2020-05-31 11:24:12.578 23993-23993/com.ramzan.snnstocks D/WindowClient: Remove from mViews: DecorView@3fca5d8[UserRegistration], this = android.view.WindowManagerGlobal@878094
2020-05-31 11:24:16.941 23993-23993/com.ramzan.snnstocks D/UserRegistration: onSuccess : https://firebasestorage.googleapis.com/v0/b/snn-stocks-c12fc.appspot.com/o/Profile_Image%2F9352682709.jpeg?alt=media&token=801f8e9d-cf5f-4d0a-890a-57823d50973a

这是我的用户注册 Activity ...

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseApp;
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.ramzan.snnstocks.Model.Users;
import com.squareup.picasso.Picasso;
import com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageView;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URI;

import de.hdodenhof.circleimageview.CircleImageView;

public class UserRegistration extends AppCompatActivity {

private Spinner spinner;
private CircleImageView mImageView;
private static final int PICK_IMAGE = 101;
private EditText et_name, et_mobile, et_pass;
private Button btn_user;
private static String name, mobile, pass, spinner_port_place,profile_image_url;

private ProgressDialog loadingBar;
private DatabaseReference mDatabase;
private Bitmap bitmap;
private static final String TAG = "UserRegistration";


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_registration);

mImageView = findViewById(R.id.profile_image);
loadingBar = new ProgressDialog(this);

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);


spinner = findViewById(R.id.spinner_port_place);

ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(UserRegistration.this,
android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.Port_Name));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(myAdapter);

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinner_port_place = parent.getItemAtPosition(position).toString();

}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});


// initializing UI components
et_name = findViewById(R.id.textInputEditTextLayout_name);
et_mobile = findViewById(R.id.textInputEditTextLayout_mobile);
et_pass = findViewById(R.id.textInputEditTextLayout_pass);
btn_user = findViewById(R.id.btn_user_registration);


//onClick listner to Button

btn_user.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

name = et_name.getText().toString();
mobile = et_mobile.getText().toString();
pass = et_pass.getText().toString();

if (name.isEmpty()) {
et_name.setError("Full Name Required");
et_name.requestFocus();

} else if (mobile.isEmpty()) {
et_mobile.setError("Mobile Number is Required");
et_mobile.requestFocus();

} else if (pass.isEmpty()) {
et_pass.setError("Choose Your Password");
et_pass.requestFocus();
} else {


uploadData();


}

}
});

//OnClick Listner on Image View

mImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager())
!= null) {
startActivityForResult(intent, PICK_IMAGE);
}

}
});
}


@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE) {
switch (resultCode) {
case RESULT_OK:

Log.i(TAG, "OnActivityResult: RESULT OK");
bitmap = (Bitmap) data.getExtras().get("data");
mImageView.setImageBitmap(bitmap);
break;

case RESULT_CANCELED:
Log.i(TAG, "OnActivityResult: RESULT CANCELLED");
break;

default:
break;

}
}
}

// upload data from here...

private void uploadData() {

loadingBar.setTitle("Registering Retailer....");
loadingBar.setMessage("Please Wait While We Are Creating Retailer Database");
loadingBar.setCanceledOnTouchOutside(false);

loadingBar.show();

handleUpload(bitmap);
//Initialize Firebase Database;

mDatabase = FirebaseDatabase.getInstance().getReference();

createUserDatabase(name, mobile, pass, spinner_port_place, profile_image_url);
}

// profile image upload

private void handleUpload(Bitmap bitmap) {

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

final StorageReference reference = FirebaseStorage.getInstance().getReference().child("Profile_Image").child(mobile + ".jpeg");

reference.putBytes(baos.toByteArray())
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(UserRegistration.this, "Upload Successfull...", Toast.LENGTH_SHORT).show();
getDownloadUrl(reference);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e(TAG, "onFailure : ", e.getCause());
}
});


}

private void getDownloadUrl(StorageReference reference) {
reference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
profile_image_url = uri.toString();
Log.d(TAG, "onSuccess : " + uri);
initializeUrl(uri);
}
});
}

private void initializeUrl(Uri uri) {

profile_image_url = uri.toString();
}

private void createUserDatabase(final String name, final String mobile, final String pass, final String spinner_port_place, final String profile_image_url) {

mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (!(dataSnapshot.child("Users").child(mobile).exists() && !(dataSnapshot.child("Admin").child(mobile).exists()))) {

Users new_user = new Users(name, mobile, pass, spinner_port_place, profile_image_url);

mDatabase.child("Users").child(mobile).setValue(new_user).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
loadingBar.dismiss();
Toast.makeText(UserRegistration.this, "Your Account Has Been Created Successfully !", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(UserRegistration.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
loadingBar.dismiss();
String msg;
msg = e.toString();
Toast.makeText(UserRegistration.this, "Error : " + msg, Toast.LENGTH_LONG).show();

}
});

}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}

}

最佳答案

你可以试试这个:

private void uploadData() {

loadingBar.setTitle("Registering Retailer....");
loadingBar.setMessage("Please Wait While We Are Creating Retailer Database");
loadingBar.setCanceledOnTouchOutside(false);

loadingBar.show();

handleUpload(bitmap);
}

private void initializeUrl(Uri uri) {

profile_image_url = uri.toString();

//Initialize Firebase Database;

mDatabase = FirebaseDatabase.getInstance().getReference();

createUserDatabase(name, mobile, pass, spinner_port_place, profile_image_url);
}

并使用addListenerForSingleValueEvent而不是addValueEventListener。更有用。

先上传个人资料图片,然后将用户保存到数据库。

您的代码并非如此。您将用户保存到数据库并传递到另一个屏幕,直到上传个人资料图片。

关于java - 未将图像 URL 字符串获取到 FirebaseDatabase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62112198/

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