我想将从相机捕获的图像上传到Firebase存储。一旦获得图像的Uri格式,我就知道如何存储图像,但是我从相机活动中获取了位图。所以我需要知道该怎么做?
click_picture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,2);
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = (Bitmap)data.getExtras().get("data");
image.setImageBitmap(bitmap);
}
public String getFileExtendsion(Uri uri){
ContentResolver contentResolver = getActivity().getContentResolver();
MimeTypeMap mime = MimeTypeMap.getSingleton();
return mime.getExtensionFromMimeType(contentResolver.getType(uri));
}
public void uploadFile(){
if(mImageUri!=null){
final StorageReference fileReference = mStorageRef.child("ProfilePicture"+"."+getFileExtendsion(mImageUri));
fileReference.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
fileReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
///store the uri
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i("Fail",e.getMessage());
}
});
}
}
我有函数uploadFile()将图像的Uri存储到firebase存储器中,然后获取图像的url将其存储在firebase实时数据库中。但是我需要将位图转换为Uri吗?如果是,怎么办?我发现的代码无法正常工作,如果还有其他方法,请告诉我!
谢谢您的时间:)
答案 0 :(得分:0)
如果要从相机获取位图,请参阅Firebase文档中的第一个示例,其中显示upload from data that you have in memory的情况,该示例方便地以Bitmap
开头。
从那里:
// Get the data from an ImageView as bytes imageView.setDrawingCacheEnabled(true); imageView.buildDrawingCache(); Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); byte[] data = baos.toByteArray(); UploadTask uploadTask = mountainsRef.putBytes(data);