android firebase上载图像不工作

g9icjywg  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(389)

我正在尝试使用以下代码让用户选择一个图像,选择图像后,它会自动上载到我的firebase存储。我在oncreate函数中初始化了变量storagereference

  1. private void selectImage() {
  2. final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };
  3. AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
  4. builder.setTitle("Add Photo!");
  5. builder.setItems(options, new DialogInterface.OnClickListener() {
  6. @Override
  7. public void onClick(DialogInterface dialog, int item) {
  8. if (options[item].equals("Take Photo"))
  9. {
  10. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  11. File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
  12. startActivityForResult(intent, 1);
  13. }
  14. else if (options[item].equals("Choose from Gallery"))
  15. {
  16. Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  17. intent.setType("image/*");
  18. startActivityForResult(intent.createChooser(intent, "Select Image"), 2);
  19. }
  20. else if (options[item].equals("Cancel")) {
  21. dialog.dismiss();
  22. }
  23. }
  24. });
  25. builder.show();
  26. }
  1. @SuppressLint("LongLogTag")
  2. @Override
  3. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  4. super.onActivityResult(requestCode, resultCode, data);
  5. // If the image is uploaded properly and the resultcode is OK
  6. // The imagePreview is updated and the image is uploaded
  7. if(requestCode == 1 || requestCode == 2) {
  8. if (resultCode == Activity.RESULT_OK && data != null && data.getData() == null) {
  9. filePath = data.getData();
  10. try {
  11. Bitmap bitmap = MediaStore.Images.Media.getBitmap(requireContext().getContentResolver(), filePath);
  12. imagePreview.setImageBitmap(bitmap);
  13. uploadImage();
  14. } catch (FileNotFoundException e) {
  15. e.printStackTrace();
  16. } catch (IOException e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. }
  21. }
  1. private void uploadImage()
  2. {
  3. if (filePath != null) {
  4. // Code for showing progressDialog while uploading
  5. ProgressDialog progressDialog = new ProgressDialog(getContext());
  6. progressDialog.setTitle("Uploading...");
  7. progressDialog.show();
  8. // Defining the child of storageReference
  9. StorageReference ref = storageReference.child(mAuth.getCurrentUser().toString());
  10. // adding listeners on upload
  11. // or failure of image
  12. ref.putFile(filePath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
  13. @Override
  14. public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
  15. // Image uploaded successfully
  16. // Dismiss dialog
  17. progressDialog.dismiss();
  18. Toast.makeText(getActivity(), "Image Uploaded!!", Toast.LENGTH_SHORT).show();
  19. }
  20. }).addOnFailureListener(new OnFailureListener() {
  21. @Override
  22. public void onFailure(@NonNull Exception e) {
  23. // Error, Image not uploaded
  24. progressDialog.dismiss();
  25. Toast.makeText(getContext(),"Failed " + e.getMessage(),Toast.LENGTH_SHORT).show();
  26. }
  27. })
  28. .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
  29. // Progress Listener for loading
  30. // percentage on the dialog box
  31. @Override
  32. public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
  33. double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
  34. progressDialog.setMessage("Uploaded " + (int)progress + "%");
  35. }
  36. });
  37. }
  38. }

我猜uploadimage函数没有被调用,但我不知道为什么。

m1m5dgzv

m1m5dgzv1#

我想出来了,问题出在这条线上
if (resultCode == Activity.RESULT_OK && data != null && data.getData() == null) data.getData() == null 应该是: data.getData() != null

相关问题