我已经广泛地寻找这个问题的答案,但我不太明白如何做到这一点。
我正在尝试使用他们的教程 ( https://firebase.google.com/docs/storage/android/upload-files ) 将图片上传到 Firebase 进行存储。为此,我创建了一个 IntentService 来处理所有数据库调用。这就是我不足的地方。我从像这样的另一项 Activity 中打包我的照片;
public void sendToFB() {
mImageView.setDrawingCacheEnabled(true);
mImageView.buildDrawingCache();
Bitmap bitmap = mImageView.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();
Bundle bundle = new Bundle();
bundle.putByteArray("bitmap", data);
Intent intent = new Intent(this, FirebaseService.class);
intent.putExtra("picture", bundle);
this.startService(intent);
}
然后我在服务中解开 bundle ;
@Override
protected void onHandleIntent(Intent intent) {
extras = (Bundle) intent.getExtras().get("picture");
uploadPicToFirebase();
...
然后我尝试粘贴字节数组,但我需要将其转换为字节,但我不知道如何在服务中进行转换。
private void uploadPicToFirebase() {
// Get the data from an ImageView as bytes
/*mImageView.setDrawingCacheEnabled(true);
mImageView.buildDrawingCache();
Bitmap bitmap = mImageView.getDrawingCache();
*/
//Bundle extras = getIntent().getExtras();
byte[] data = extras.getByteArray("picture");
UploadTask uploadTask = mountainsRef.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
System.out.println("not ok");
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
System.out.println("helt ok");
Uri downloadUrl = taskSnapshot.getDownloadUrl();
}
});
}
当然,它以 IllegalArgumentException
结束
java.lang.IllegalArgumentException: bytes cannot be null
最佳答案
您输错了 key :
private void uploadPicToFirebase(){
// ...
byte[] data = extras.getByteArray("bitmap");
// ...
}
关于java - ByteArray 到intentService 中的byte[]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40406921/