【发布时间】:2017-03-15 08:39:42
【问题描述】:
我正在尝试在使用 SiliCompressor 库将图像上传到 Firebase Storage 之前对其进行压缩,但它似乎不起作用,ProgressDialog 不会停止。我首先做的是通过单击 ImageButton 将图像从图库中选择到 ImageButton 中。下面是我的代码。
imageSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/");
startActivityForResult(galleryIntent, GALLERY_REQUEST);
}
});
---------------------------------------------------------------------
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK) {
imageUri = data.getData();
// imageSelect.setImageBitmap(BitmapFactory.decodeFile(imageSelectFile.getAbsolutePath()));
// Compressor com = Compressor.getDefault(this).compressToFile(imageFile);
// imageSelect.setImageURI(imageUri);
Picasso.with(c).load(imageUri).fit().into(imageSelect);
}
}
所以现在我有一个方法startPosting(),它通过单击按钮将数据上传到 Firebase 存储。
下面是我的代码。
private void startPosting() {
mProgress.setMessage("Uploading Image...");
//Compressing an Image ....
String stringUri= imageUri.toString();
Uri uri_image_final;
//String filePath = SiliCompressor.with(getApplicationContext()).compress(stringUri);
String filePath = SiliCompressor.with(getApplicationContext()).compress(stringUri, true);
uri_image_final = Uri.parse(filePath);
System.out.println("Whats here :" +
""+ uri_image_final);
final String title_val = mPostTitle.getText().toString().trim();
final String desc_val = mPostDesc.getText().toString().trim();
if (!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(desc_val) && filePath != null) {
mProgress.show();
StorageReference filepath = mStorage.child("BlogImages").child(uri_image_final.getLastPathSegment());
filepath.putFile(uri_image_final).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUri = taskSnapshot.getDownloadUrl();
DatabaseReference newPost = mDatabase.push();
DatabaseReference c = mDatabase.push();
newPost.child("EventTitle").setValue(title_val);
newPost.child("EventDescription").setValue(desc_val);
newPost.child("EventImage").setValue(downloadUri.toString());
newPost.child("PostId").setValue(c);
mProgress.dismiss();
startActivity(new Intent(PostActivity.this, MainActivity.class));
}
}
);
} else if (TextUtils.isEmpty(title_val) && TextUtils.isEmpty(desc_val) && imageUri != null) {
mProgress.show();
StorageReference filepath = mStorage.child("BlogImages").child(uri_image_final.getLastPathSegment());
filepath.putFile(uri_image_final).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUri = taskSnapshot.getDownloadUrl();
DatabaseReference newPost = mDatabase.push();
newPost.child("EventTitle").setValue("");
newPost.child("EventDescription").setValue("");
newPost.child("EventImage").setValue(downloadUri.toString());
mProgress.dismiss();
// startActivity(new Intent(PostActivity.this, MainActivity.class));
Intent load= new Intent(PostActivity.this,MainActivity.class);
load.putExtra(eventname,eventname);
startActivity(load);
}
}
);
}
else if (!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(desc_val) && imageUri== null){
Toast.makeText(getApplicationContext(),"Please insert an Image and Upload ! ",Toast.LENGTH_LONG).show();
}
}
现在,如果您在该 Method 中看到,我的本质是压缩加载到 ImageButton 中的图像,然后将其上传到 firebase。
这是Silicon Compressor 的行,它试图压缩加载在 ImageButton 中的图像。
String filePath = SiliCompressor.with(getApplicationContext()).compress(stringUri, true);
我从 Github 的这个链接获得了这个库。 https://github.com/Tourenathan-G5organisation/SiliCompressor
请问我哪里错了,因为图像没有上传,但我希望它在压缩时上传。
【问题讨论】:
-
要考虑的一个工具是官方的 Firebase“调整图像大小”扩展 - firebase.google.com/products/extensions/… - 虽然尚不清楚调整后的图像的压缩程度。
标签: java android firebase firebase-storage image-compression