用户从相机或图库中选择图像,因此现在图像位于Imageview中以向用户显示图片。现在用户将其上传到服务器,但我上传的每张图片都是黑色的。为什么会这样? 这是代码:
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 :(得分:0)
答案 1 :(得分: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);
这对我有帮助