Firebase图片上传,图片参数

时间:2017-07-21 16:23:33

标签: android image firebase file-upload firebase-storage

以下是我上传到Firebase的图片代码。

 private static final int GALLERY_INTENT = 2;

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

    if (requestCode == GALLERY_INTENT && resultCode == RESULT_OK) {
        mProgressDialog.setMessage("Uploading...");
        mProgressDialog.show();

        StorageReference imageRef = mStorage.child(email);
        StorageReference imageImagesRef = mStorage.child("photos/" + email);

        imageRef.getName().equals(imageImagesRef.getName());    // true
        imageRef.getPath().equals(imageImagesRef.getPath());    // false

        Uri uri = data.getData();
        imageViewRegistration.setImageURI(uri);

        imageViewRegistration.setDrawingCacheEnabled(true);
        imageViewRegistration.buildDrawingCache();
        Bitmap bitmap = imageViewRegistration.getDrawingCache();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] data2 = baos.toByteArray();

        UploadTask uploadTask = imageImagesRef.putBytes(data2);
        uploadTask.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                // Handle unsuccessful uploads
                Toast.makeText(getApplicationContext(), "Sorry, your image wasn't set, please try again",
                        Toast.LENGTH_LONG).show();
            }
        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
                @SuppressWarnings("VisibleForTests") Uri downloadUrl = taskSnapshot.getDownloadUrl();
                mProgressDialog.dismiss();
                Picasso.with(getApplicationContext()).load(downloadUrl).fit().centerCrop().into(imageViewRegistration);
                Toast.makeText(getApplicationContext(), "Upload done",
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}

public void onClick(DialogInterface dialog, int id) {
      Intent intent = new Intent(Intent.ACTION_PICK);
      intent.setType("image/*");
      startActivityForResult(intent, GALLERY_INTENT);
}

我想上传带图像参数的图像,但我认为图像是使用imageView参数上传的。如果我将图片上传到firebase,它看起来像这样:enter image description here 但我想上传没有这些黑色空间的图片。 当我在xml文件图像视图中包含wrap_content的高度和宽度时,我上传后会出现此错误

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference

有没有办法如何使用自己的参数上传图片,以便它不会有这些黑色空格?感谢。

1 个答案:

答案 0 :(得分:0)

尝试使用此

ids

而不是

Bitmap bitmap= MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);