2017-06-05 15 views
1

用戶從相機或圖庫中選擇圖像,所以現在圖像位於Imageview中以向用戶顯示圖片。現在用戶將其上傳到服務器,但是我上傳的每張圖片均爲黑色。這是爲什麼發生? 這是代碼:上傳到Firebase的圖像都是黑色的。如何擺脫它?

mUpload.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 

     final StorageReference storageRef = FirebaseStorage.getInstance().getReferenceFromUrl("gs://lalala-14247.appspot.com"); 

     mImageview.setDrawingCacheEnabled(true); 
     mImageview.buildDrawingCache(); 
     Bitmap bitmap = mImageview .getDrawingCache(); 

     ByteArrayOutputStream baas = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baas); 

     byte[] data = baas.toByteArray(); 
     String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); 

     UploadTask uploadTask = storageRef.child("Photo").child(timeStamp).putBytes(data); 

     uploadTask.addOnFailureListener(new OnFailureListener() { 
      @Override 
      public void onFailure(@NonNull Exception exception) { 
       Toast.makeText(MainActivity.this, "Picture upload failed", Toast.LENGTH_LONG).show(); 
      } 
     }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { 
      @Override 
      public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 
       Toast.makeText(MainActivity.this, "Upload done", Toast.LENGTH_LONG).show(); 

      } 
     }); 

    } 

}); 

我的依賴關係:

compile 'com.android.support:appcompat-v7:25.3.1' 
compile 'com.android.support.constraint:constraint-layout:1.0.2' 
compile 'com.squareup.picasso:picasso:2.5.2' 
compile 'com.google.firebase:firebase-storage:10.0.1' 
compile 'com.google.firebase:firebase-auth:10.0.1' 
compile 'com.google.firebase:firebase-database:10.0.1' 
testCompile 'junit:junit:4.12' 
+0

嗨@Jessef,考慮使用putStream()或putFile(),也使用較少的內存。 – mdb

+0

嘿謝謝:)但我在哪裏添加? – Jessef

回答

0

試試這個,

Uri file = Uri.fromFile(new File("your_image_path")); 
StorageReference riversRef = storageRef.child("images/"+file.getLastPathSegment()); 
uploadTask = riversRef.putFile(file); 

// Register observers to listen for when the download is done or if it fails 
uploadTask.addOnFailureListener(new OnFailureListener() { 
    @Override 
    public void onFailure(@NonNull Exception exception) { 
     // Handle unsuccessful uploads 
    } 
}).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. 
     Uri downloadUrl = taskSnapshot.getDownloadUrl(); 
    } 
}); 

欲瞭解更多信息請https://firebase.google.com/docs/storage/android/upload-files

+0

對不起我抱歉:/但謝謝 – Jessef

0
final int backgroundPreviousColor = view.getDrawingCacheBackgroundColor(); 
view.setDrawingCacheEnabled(true); 
view.setDrawingCacheBackgroundColor(0xfffafafa); 
final Bitmap bitmap = view.getDrawingCache(); 
view.setDrawingCacheBackgroundColor(backgroundPreviousColor); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream); 
view.setDrawingCacheEnabled(cachePreviousState); 

幫我

相關問題