gpt4 book ai didi

java - 将图像上传到 Firebase - Java

转载 作者:行者123 更新时间:2023-12-02 00:36:25 26 4
gpt4 key购买 nike

我尝试上传图像,不幸的是,当我尝试单击按钮继续上传时,apk 总是崩溃,这是日志

它说 childName 不能为 null 或为空 Error Log

java

Java Image

package com.example.firstapplication;

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

import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.squareup.picasso.Picasso;

import de.hdodenhof.circleimageview.CircleImageView;

public class Register_two extends AppCompatActivity {

LinearLayout btn_back;
Button btn_continue, btn_upload_photo;
ImageView prof_pic;
EditText bio, nama_lengkap;

Uri photo_location;
Integer photo_max = 1;

DatabaseReference reference;
StorageReference storage;

String USERNAME_KEY = "usernamekey";
String username_key = "";
String username_key_new = "";


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

getUsernameLocal();

btn_continue = findViewById(R.id.btn_continue);
btn_back = findViewById(R.id.btn_back);
btn_upload_photo = findViewById(R.id.btn_upload_photo);
prof_pic = findViewById(R.id.prof_pic);


btn_upload_photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
findPhoto();
}
});

btn_continue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

//ubah state menjadi loading...
btn_continue.setEnabled(false);
btn_continue.setText("Loading...");

//menyimpan ke Firebase
reference = FirebaseDatabase.getInstance().getReference().child("Users").child(username_key_new);
storage = FirebaseStorage.getInstance().getReference().child("Photousers").child(username_key_new);

//validasi untuk file (apakah ada?)
if (photo_location != null){
final StorageReference storageReference1 =
storage.child(System.currentTimeMillis() + "." +
getFileExtention(photo_location));

storageReference1.putFile(photo_location)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

storageReference1.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String uri_photo = uri.toString();
reference.getRef().child("url_photo_profile").setValue(uri_photo);
reference.getRef().child("nama_lengkap").setValue(nama_lengkap.getText().toString());
reference.getRef().child("bio").setValue(bio.getText().toString());

}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
//pindah activity
Intent btncontinue = new Intent(Register_two.this,SuccessRegister.class);
startActivity(btncontinue);
}
});

}
}).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
//pindah activity
// Intent btncontinue = new Intent(Register_two.this,SuccessRegister.class);
// startActivity(btncontinue);
}
});
}


}
});

btn_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent goback = new Intent(Register_two.this,Register_one.class);
startActivity(goback);
}
});

}

String getFileExtention(Uri uri){
ContentResolver contentResolver = getContentResolver();
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
return mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(uri));
}

public void findPhoto(){
Intent pic = new Intent();
pic.setType("image/*");
pic.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(pic, photo_max);

}

@Override
public void onActivityResult (int requestCode, int resultCode, @Nullable Intent data){
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == photo_max && resultCode == RESULT_OK && data != null && data.getData() != null)
{
photo_location = data.getData();
Picasso.with(this).load(photo_location).centerCrop().fit().into(prof_pic);

}
}
public void getUsernameLocal(){
SharedPreferences sharedPreferences = getSharedPreferences(USERNAME_KEY, MODE_PRIVATE);
username_key_new = sharedPreferences.getString(username_key,"");
}
}

错误说,我将 null 传递给第 81 行的 child

storage = FirebaseStorage.getInstance().getReference().child("Photousers").child(username_key_new);

但我不知道出了什么问题

最佳答案

你尝试过这个吗?

我建议创建一个返回字符串而不是创建一个 void 方法

//Please check what is the identifier of the string in sharedpreferences
//if usernamekey is the identifier then getString("usernamekey","");
public String getUsernameLocal(){
SharedPreferences sharedPreferences = getSharedPreferences(USERNAME_KEY, MODE_PRIVATE);
String userKey= sharedPreferences.getString(USERNAME_KEY,"");
return userKey;
}

还有你的引用资料

storage = FirebaseStorage.getInstance().getReference().child("Photousers").child(getUsernameLocal());

关于java - 将图像上传到 Firebase - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57970388/

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