Java Android Firebase上载图像不工作
我尝试使用以下代码让用户选择图像,选择图像后,图像将自动上载到我的Firebase存储。我在OnCreate函数中初始化了变量storageReferenceJava Android Firebase上载图像不工作,java,android,firebase,file-upload,firebase-storage,Java,Android,Firebase,File Upload,Firebase Storage,我尝试使用以下代码让用户选择图像,选择图像后,图像将自动上载到我的Firebase存储。我在OnCreate函数中初始化了变量storageReference private void selectImage() { final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" }; AlertDialo
private void selectImage() {
final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
builder.setTitle("Add Photo!");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo"))
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
startActivityForResult(intent, 1);
}
else if (options[item].equals("Choose from Gallery"))
{
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent.createChooser(intent, "Select Image"), 2);
}
else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
private void uploadImage()
{
if(filePath!=null){
//上载时显示progressDialog的代码
ProgressDialog=新建ProgressDialog(getContext());
progressDialog.setTitle(“上载…”);
progressDialog.show();
//定义storageReference的子级
StorageReference ref=StorageReference.child(mAuth.getCurrentUser().toString());
//在上载时添加侦听器
//还是形象的失败
ref.putFile(filePath).addOnSuccessListener(新的OnSuccessListener(){
@凌驾
成功时公共无效(UploadTask.TaskSnapshot TaskSnapshot){
//已成功上载图像
//取消对话框
progressDialog.disclose();
Toast.makeText(getActivity(),“上传图像!!”,Toast.LENGTH\u SHORT.show();
}
}).addOnFailureListener(新的OnFailureListener(){
@凌驾
public void onFailure(@NonNull异常e){
//错误,未上载图像
progressDialog.disclose();
Toast.makeText(getContext(),“Failed”+e.getMessage(),Toast.LENGTH_SHORT).show();
}
})
.addOnProgressListener(新的OnProgressListener(){
//用于加载的进度侦听器
//对话框上的百分比
@凌驾
public void onProgress(UploadTask.TaskSnapshot TaskSnapshot){
双进度=(100.0*taskSnapshot.GetByTestTransferred()/taskSnapshot.getTotalByteCount());
progressDialog.setMessage(“上传”+(int)进度+“%”;
}
});
}
}
我猜uploadImage函数没有被调用,但我不知道为什么。我发现了,问题是这行代码
if(resultCode==Activity.RESULT\u OK&&data!=null&&data.getData()==null)
data.getData()==null
应该是:
data.getData()!=空
那么当您使用断点调试它时会发生什么呢?你试过那样做吗?
@SuppressLint("LongLogTag")
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// If the image is uploaded properly and the resultcode is OK
// The imagePreview is updated and the image is uploaded
if(requestCode == 1 || requestCode == 2) {
if (resultCode == Activity.RESULT_OK && data != null && data.getData() == null) {
filePath = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(requireContext().getContentResolver(), filePath);
imagePreview.setImageBitmap(bitmap);
uploadImage();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private void uploadImage()
{
if (filePath != null) {
// Code for showing progressDialog while uploading
ProgressDialog progressDialog = new ProgressDialog(getContext());
progressDialog.setTitle("Uploading...");
progressDialog.show();
// Defining the child of storageReference
StorageReference ref = storageReference.child(mAuth.getCurrentUser().toString());
// adding listeners on upload
// or failure of image
ref.putFile(filePath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Image uploaded successfully
// Dismiss dialog
progressDialog.dismiss();
Toast.makeText(getActivity(), "Image Uploaded!!", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Error, Image not uploaded
progressDialog.dismiss();
Toast.makeText(getContext(),"Failed " + e.getMessage(),Toast.LENGTH_SHORT).show();
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
// Progress Listener for loading
// percentage on the dialog box
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
progressDialog.setMessage("Uploaded " + (int)progress + "%");
}
});
}
}