我想在使用SiliCompressor
库将图像上传到Firebase Storage
之前先压缩图像,但似乎无法正常工作,ProgressDialog
并未停止。我第一次做的是通过单击ImageButton将图像从图库中选取到ImageButton中。以下是我的代码。在将图像上传到Firebase存储之前压缩图像
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()
通过按钮的点击火力地堡存储其上载数据。 以下是我的代码。
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();
}
}
现在,如果你在该方法中看到的,我的本质是要压缩其在加载的ImageButton的图像,然后后来它上传到火力地堡。 这是Silicon Compressor
的行,它尝试压缩在ImageButton中加载的图像。
String filePath = SiliCompressor.with(getApplicationContext()).compress(stringUri, true);
我从Github的这个链接中得到了这个lib。 https://github.com/Tourenathan-G5organisation/SiliCompressor
所以我错了,请因为图像不上传,但我希望它在压缩时上传。
感谢@rana,我想实现'ImagePicker'类但是'getTempFile()'丢失。 –
@LutaayaHuzaifah getTempFile被添加:) –
谢谢,但我仍然感到困惑,'onActivityResult()',在哪里看到你的上传图像加上压缩,你的轨道是正确的,但请你可以尝试展示我怎么能把它在我的'startPosting()'方法中,以及我必须放在'onActivityResult()'中,因为我仍然是初学者。 –