2017-07-21 75 views
-1

以下是我上傳到Firebase的圖片代碼。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 但我想上傳圖片沒有這些黑色空間。 當我有在高度和WRAP_CONTENT的寬度xml文件圖像視圖然後我上傳這個錯誤

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 

後,是否有辦法如何上傳圖像與自己的參數,所以它不會有這些黑色的空間?謝謝。

回答

0

嘗試使用這個

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

代替

Bitmap bitmap = imageViewRegistration.getDrawingCache(); 
+0

感謝您的幫助的人,它可以完美運行;)謝謝。 – Saom

+0

@Saom歡迎您:)請將答案標記爲True –