我希望我的Firebase数据库使用用户个人资料图片图像URL进行初始化,我尝试添加此内容,但我的profileProfileUrl变量未使用ImageUri变量进行初始化?请帮助完成这件事...
我只希望第5个变量配置文件图像链接使用来自URI变量的图像URL字符串初始化,但未初始化
这是我的项目日志
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
这是我的UserRegistration活动...
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);
}
首先上传个人资料图片,然后将用户保存到数据库。
您的代码不是这种情况。您将用户保存到数据库并传递到另一个屏幕,直到上传个人资料图片。