从Mediastore检索图像为空

时间:2018-03-23 21:02:15

标签: android firebase camera firebase-storage mediastore

我正在尝试从手机的相机中保存图像,然后检索它以将其保存在firebase存储中。 我尝试了很多方法,但是当我检索它时,我总是得到一个null 这是我用相机拍摄照片的方式:

  private void camera() {
        if (ActivityCompat.checkSelfPermission(MainChatActivity.this,
                Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
            Intent i = new Intent();
            i.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i, PHOTO_CAMERA_IMAGE);
        }
    }

我将它保存在手机上:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    switch (requestCode) {
        case PICK_IMAGE:
            if (data != null)
                uploadImage(data.getData());
            break;
        case PHOTO_CAMERA_IMAGE:
            if (resultCode == RESULT_OK) {
                if (data != null) {
                    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    // Ensure that there's a camera activity to handle the intent
                    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                        // Create the File where the photo should go
                        File photoFile = null;
                        try {
                            photoFile = createImageFile();
                            // Continue only if the File was successfully created

                            if (photoFile != null) {
                                photoURI = FileProvider.getUriForFile(this,
                                        "borealos.com.chat.fileprovider",
                                        photoFile);
                                sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, photoURI));

                                takePictureIntent.putExtra("foto", photoURI);
                                Uri uri = (Uri) takePictureIntent.getExtras().get(MediaStore.EXTRA_OUTPUT);
                                uploadImage(photoURI);
                           // startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
                            }
                        } catch (IOException ex) {
                            // Error occurred while creating the Fil
                        }

                    }
                }
            }
            break;
}}

在这里我检索它以便在firebase上传:

private void uploadImage( final Uri filePath) {
    Uri filePath2=filePath;

    final String s;
    if (filePath != null) {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("Uploading...");
        progressDialog.show();

        s = UUID.randomUUID().toString();
        ref = storageReference.child("images/" + s);
        ref.putFile(filePath)
                .addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                        if (task.isSuccessful()) {
                            progressDialog.dismiss();

                            sendImage(filePath, task.getResult().getDownloadUrl().toString());

                        }
                    }

                });
    }
}

感谢您提供的建议帮助

0 个答案:

没有答案