我想在上传到 Firebase 存储之前调整图片大小。
我的问题是:Firebase 根据其 Uri 上传图像。我从图库意图中获取图像并将其放置在
有没有办法获取Bitmap的Uri或ImageView的Uri?或者只是成功调整大小并上传呢?
我的问题是:Firebase 根据其 Uri 上传图像。我从图库意图中获取图像并将其放置在
ImageView
上,然后像这样调整大小:@Override
protected void onActivityResult(int requestCode, int resultCode, Intent returnIntentData) {
super.onActivityResult(requestCode, resultCode, returnIntentData);
switch(requestCode) {
case RC_SELECT_PHOTO:
if (resultCode == RESULT_OK) {
mImageUri = returnIntentData.getData();
mSelectedImage.setImageURI(mImageUri);
//get a Image obj from ImageView
Bitmap bit = ((BitmapDrawable) mSelectedImage.getDrawable()).getBitmap();
int width = bit.getWidth();
int height = bit.getHeight();
float rate = 0.0f;
int maxResolution = 1920;
int newWidth = width;
int newHeight = height;
if(width > height) {
if(maxResolution < width) {
rate = maxResolution/ (float) width;
newHeight = (int) (height * rate);
newWidth = maxResolution;
}
} else {
if(maxResolution < height) {
rate = maxResolution/(float)height;
newWidth = (int) (width * rate);
newHeight = maxResolution;
}
}
Bitmap resizedImage = Bitmap.createScaledBitmap(bit, newWidth, newHeight, true);
mSelectedImage.setImageBitmap(resizedImage);
}
并且Firebase可以根据Uri上传文件,就像这样(如果有其他上传文件的方法,那将非常有帮助!):
public void uploadPhotoToStorage() {
//present image's Ref
StorageReference presentGymPhotoRef =
mGymStorageReferences.child(mImageUri.getLastPathSegment());
//Upload file to Firebase Storage
presentGymPhotoRef.putFile(mImageUri)
.addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUri = taskSnapshot.getDownloadUrl();
//save gymPhoto Uri to instance
gymPhotoUri = downloadUri.toString();
gymName = mEditGymName.getText().toString();
.......
有没有办法获取Bitmap的Uri或ImageView的Uri?或者只是成功调整大小并上传呢?