我想在Firebase android的帮助下上传图像和文本吗?

时间:2019-03-04 07:24:22

标签: android firebase

txt_category.getText().toString()不会出现。它即将为空。图片网址即将到来,但onActivityResult().中没有文本,我想要一个单独的ID,其中文本类别名称和图片都应输入。如何从对话框中获取editText并放入hashMap inonactivity()方法中。由于空对象引用,应用每次崩溃。无法从对话框editText获取文本。每次都为空。图片网址即将到来,但onActivityResult().中不会出现文本,我想要一个ID,类别名称和图片都应放在其中。 txt_category.getText().toString()即将到来。它即将为空。图片网址即将到来,但onActivityResult().中没有文字

public class HomeFragment extends Fragment {
    private DatabaseReference Rootref;
    String d;
    HashMap < String, String > hashMap, hashMap1;
    FirebaseAuth mAuth;
    FirebaseRecyclerAdapter adapter;
    FirebaseUser firebaseUser;
    private String currentUserId;
    EditText txt_category;
    Button btnSelect, btnUpload;
    private ProgressDialog mProgressDialog;
    String id;
    private StorageReference imgStorageReference;
    RecyclerView recycler_category;
    Button btn_category, btn_subcat;
    private static final int GALLERY_PICK = 1;
    String catName;
    public static final String UPDATE = "UPDATE";
    public static final String DELETE = "DELETE";
    View view;
    TextView txt;

    public HomeFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
     Bundle savedInstanceState) {
     view = inflater.inflate(R.layout.fragment_home, container, false);
     intialise();
     return view;
    }

    private void intialise() {
     imgStorageReference = FirebaseStorage.getInstance().getReference();
     hashMap1 = new HashMap < > ();
     recycler_category.setLayoutManager(glm);
     id = UUID.randomUUID().toString();
     btn_category = view.findViewById(R.id.btn_category);
     showDialog();
     btn_category.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
       showDialog()
      }
     });
    }

    private void showDialog() {
     AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
     builder.setTitle("Enter Category Name : ");
     builder.setMessage("Please fill full information");
     LayoutInflater inflater = HomeFragment.this.getLayoutInflater();
     final View custom_layout =
      inflater.inflate(R.layout.custom_dialog_category, null);
     btnSelect = custom_layout.findViewById(R.id.btnSelect);
     btnUpload = custom_layout.findViewById(R.id.btnUpload);
     builder.setView(custom_layout);
     builder.setIcon(R.drawable.ic_shopping_cart_black_24dp);
     Rootref = FirebaseDatabase.getInstance().getReference("Category");
     final EditText txt_category =
      custom_layout.findViewById(R.id.edit_text_cat);
     btnSelect.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
       chooseImage();
      }
     });
     builder.show();
    }

    private void chooseImage() {
     CropImage.activity()
      .setGuidelines(CropImageView.Guidelines.ON)
      .start(getContext(), HomeFragment.this);
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);
     if (requestCode == GALLERY_PICK && resultCode == RESULT_OK) {
      Uri imageURI = data.getData();
      CropImage.activity(imageURI).
      setAspectRatio(1, 1).start(getContext(), HomeFragment.this);
     }

     if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
      CropImage.ActivityResult result = CropImage.getActivityResult(data);

      if (resultCode == RESULT_OK) {
       mProgressDialog = new ProgressDialog(getContext());
       mProgressDialog.setTitle("Saving Changes");
       mProgressDialog.setMessage("Wait untill changes are saved");
       mProgressDialog.setCanceledOnTouchOutside(false);
       mProgressDialog.show();
       Uri resultUri = result.getUri();

       StorageReference filepath =
        imgStorageReference.child("Profile_images").child(id + ".jpg");

       filepath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener < UploadTask.TaskSnapshot > () {
         @Override
         public void onComplete(@NonNull Task < UploadTask.TaskSnapshot > task) {
          if (task.isSuccessful()) {
           imgStorageReference.child("Profile_images").
           child(id + ".jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener < Uri > () {
            @Override
            public void onSuccess(Uri uri) {
             String downloadUrl = uri.toString();
             hashMap.put("imageURL", downloadUrl);
             hashMap.put("category", txt_category.getText().toString());
             Rootref.push().setValue(hashMap).addOnCompleteListener(new

              OnCompleteListener < Void > () {
               @Override
               public void onComplete(@NonNull Task < Void > task) {
                if (task.isSuccessful()) {
                 mProgressDialog.dismiss();
                 Toast.makeText(getContext(), " Image Updated",
                  Toast.LENGTH_SHORT).show();
                 onResume();
                }
               }
              });
            }
           });
          }

          Toast.makeText(SettingsActivity.this, "Working",
           Toast.LENGTH_SHORT).show();
         }
         else {
          Toast.makeText(getContext(), "Not Working",
           Toast.LENGTH_SHORT).show();
          mProgressDialog.dismiss();
         }
        }
       });
     } else if (resultCode ==
      CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
      Exception error = result.getError();
     }
    }
   }

0 个答案:

没有答案