每当我点击发送数据时,它都会成功将文件上传到firebase storage
,但在UI
冻结5秒或大约10秒后
这种冻结也不允许在任何时候出现进度对话框 我在这个活动中创建了它。
public class NewPost extends AppCompatActivity {
public static Bitmap bitmap;
public static ImageView imageView1,imageView2;
public static CardView card1,card2;
public static int[] HAS_IMAGE= {0,0};
public static int LOADING=0,IMAGE1=0,IMAGE2=0;
private StorageReference mStorageRef;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
File file,file1;
ImageView backk;
Calendar calendar;
int done=0;
SimpleDateFormat simpleDateFormat, simpleTimeFormat;
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_new_post);
file = new File(this.getCacheDir (), "1");
file1 = new File(this.getCacheDir (), "2");
imageView1 = findViewById (R.id.image);
imageView1.setDrawingCacheEnabled (true);
imageView2 = findViewById (R.id.image2);
imageView2.setDrawingCacheEnabled (true);
backk = findViewById (R.id.backk);
backk.bringToFront ();
backk.setDrawingCacheEnabled (true);
backk.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View v) {
HAS_IMAGE[0]=0;
HAS_IMAGE[1]=0;
LOADING=0;
imageView1.setImageResource (R.drawable.person_for_new_post);
imageView2.setImageResource (R.drawable.person_for_new_post);
finish ();
overridePendingTransition (R.anim.closeup,R.anim.openup);
}
});
card1= findViewById (R.id.card);
card2= findViewById (R.id.card2);
calendar = Calendar.getInstance ();
simpleDateFormat = new SimpleDateFormat ("dd MM yyyy", Locale.getDefault ());
simpleTimeFormat = new SimpleDateFormat ("HH:mm", Locale.getDefault ());
mStorageRef = FirebaseStorage.getInstance().getReference();
sharedPreferences = getSharedPreferences ("ImageDetails", MODE_PRIVATE);
editor = sharedPreferences.edit ();
final FloatingActionButton sendData = findViewById (R.id.sendData);
sendData.setDrawingCacheEnabled (true);
sendData.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View view) {
if(update (imageView1)&& update (imageView2)){
done=0;
Bitmap image1,image2;
image1 = ((BitmapDrawable)imageView1.getDrawable ()).getBitmap ();
image2 = ((BitmapDrawable)imageView2.getDrawable ()).getBitmap ();
if(image1 != null &&image2 !=null){
bitMaptoImage (image1,"file",file);
bitMaptoImage (image2,"file1",file1);
try {
file = new Compressor (NewPost.this).setQuality (75).setMaxWidth (640).setMaxHeight (480).setCompressFormat (Bitmap.CompressFormat.PNG).compressToFile (file);
file1 = new Compressor (NewPost.this).setQuality (75).setMaxWidth (640).setMaxHeight (480).setCompressFormat (Bitmap.CompressFormat.PNG).compressToFile (file1);
}catch (IOException e) {
Toast.makeText (NewPost.this, e.getMessage (), Toast.LENGTH_SHORT).show ();
}
}else
Toast.makeText (NewPost.this, "Bitmap is empty", Toast.LENGTH_SHORT).show ();
UploadData (file, "image", 1);
UploadData (file1, "image1",2);
}else{
Snackbar.make (findViewById (android.R.id.content), "Select images before uploading to Server", Toast.LENGTH_SHORT).show ();
}
}
});
card1.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View view) {
IMAGE1=1;
ImagePickerIntent ();
}
});
card2.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View view) {
IMAGE2=1;
ImagePickerIntent ();
}
});
editor.apply ();
editor.commit ();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ConstantsCustomGallery.REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null) {
ArrayList<Image> images = data.getParcelableArrayListExtra(ConstantsCustomGallery.INTENT_EXTRA_IMAGES);
Uri uri = Uri.fromFile(new File(images.get(0).path));
Intent intent = new Intent (getApplicationContext (), CropActivity.class);
intent.putExtra ("uri",uri);
intent.putExtra ("type","NewPost");
startActivity (intent);
}else{
LOADING=0;
}
}
@Override
protected void onDestroy() {
editor.apply ();
editor.commit ();
super.onDestroy ();
}
@Override
protected void onStop() {
editor.apply ();
editor.commit ();
super.onStop ();
}
@Override
protected void onPause() {
editor.apply ();
editor.commit ();
super.onPause ();
overridePendingTransition (R.anim.closeup,R.anim.openup);
}
public void onBackPressed(){
HAS_IMAGE[0]=0;
HAS_IMAGE[1]=0;
IMAGE1=0;
IMAGE2=0;
LOADING=0;
editor.apply ();
editor.commit ();
super.onBackPressed ();
overridePendingTransition (R.anim.closeup,R.anim.openup);
}
public void ImagePickerIntent(){
if(HAS_IMAGE[0]==0 || HAS_IMAGE[1]==0){
LOADING++;
if(LOADING==1){
Intent intent = new Intent(this, AlbumSelectActivity.class);
intent.putExtra(ConstantsCustomGallery.INTENT_EXTRA_LIMIT, 1);
startActivityForResult(intent, ConstantsCustomGallery.REQUEST_CODE);
}else
Toast.makeText (NewPost.this, "Wait a second", Toast.LENGTH_SHORT).show ();
}
}
public void UploadData(final File file, final String fileName, final int i){
try {
Uri upload = Uri.fromFile(file);
StorageReference riversRef = mStorageRef.child(SplashScreen.USERNAME+"/" + "posts" + "/"+ String.valueOf (WallOfTheApp.countt)+"/"+fileName);
riversRef.putFile(upload)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot> () {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
done++;
HAS_IMAGE[0]=0;
HAS_IMAGE[1]=0;
IMAGE1=0;
IMAGE2=0;
LOADING=0;
Uri download= taskSnapshot.getMetadata ().getDownloadUrl ();
String down = download.toString ();
if(i==1){
DatabaseReference downloadUrl = myRef.child ("User")
.child (SplashScreen.USERNAME)
.child ("Posts")
.child (String.valueOf (WallOfTheApp.countt))
.child ("DownloadUrl1");
downloadUrl.setValue (down);
}else if(i==2){
DatabaseReference downloadUrl2 = myRef.child ("User")
.child (SplashScreen.USERNAME)
.child ("Posts")
.child (String.valueOf (WallOfTheApp.countt))
.child ("DownloadUrl2");
downloadUrl2.setValue (down);
databaseDateRegistry (simpleDateFormat.format (calendar.getTime ())
, simpleTimeFormat.format (calendar.getTime ()));
}
if(done==2){
HAS_IMAGE[0]=0;
HAS_IMAGE[1]=0;
LOADING=0;
WallOfTheApp.countt++;
editor.putString ("Count", String.valueOf (WallOfTheApp.countt));
UpdateScore ();
SendCount ();
finish ();
imageView1.setImageResource (R.drawable.person_for_new_post);
imageView2.setImageResource (R.drawable.person_for_new_post);
}
}
})
.addOnFailureListener(new OnFailureListener () {
@Override
public void onFailure(@NonNull Exception exception) {
Toast.makeText (NewPost.this, exception.getMessage (), Toast.LENGTH_SHORT).show ();
WallOfTheApp.countt--;
}
});
}catch (Exception e){
Toast.makeText (this, e.getMessage (), Toast.LENGTH_SHORT).show ();
}
}
public void bitMaptoImage(Bitmap bitmap, String fileName,File file){
try {
boolean success = file.createNewFile ();
if(success){
Toast.makeText (this, fileName + " Created Successfully", Toast.LENGTH_SHORT).show ();
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ();
bitmap.compress (Bitmap.CompressFormat.PNG,0,byteArrayOutputStream);
byte[] bytes = byteArrayOutputStream.toByteArray ();
FileOutputStream fileOutputStream = new FileOutputStream (file);
fileOutputStream.write (bytes);
fileOutputStream.flush ();
fileOutputStream.close ();
} catch (IOException e) {
Toast.makeText (this, e.getMessage (), Toast.LENGTH_SHORT).show ();
}
}
public boolean update(ImageView imageView){
if(imageView.getDrawable ().getConstantState ()!=getResources ().getDrawable (R.drawable.person_for_new_post,null).getConstantState ()){
return true;
}else {
return false;
}
}
public void SendCount(){
final DatabaseReference databaseReference = myRef.child ("User").child (SplashScreen.USERNAME).child ("PhotoCount");
databaseReference.setValue (String.valueOf (WallOfTheApp.countt));
}
public void databaseDateRegistry(final String Date, final String timee){
DatabaseReference date = myRef.child ("User")
.child (SplashScreen.USERNAME)
.child ("Posts")
.child (String.valueOf (WallOfTheApp.countt))
.child ("Date");
DatabaseReference time = myRef.child ("User")
.child (SplashScreen.USERNAME)
.child ("Posts")
.child (String.valueOf (WallOfTheApp.countt))
.child ("Time");
date.setValue (Date);
time.setValue (timee);
}
public void UpdateScore(){
myRef.addValueEventListener (new ValueEventListener () {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
DataSnapshot PhotoCount= dataSnapshot.child ("User").child (SplashScreen.USERNAME).child ("PhotoCount");
SplashScreen.PHOTOCOUNT = String.valueOf (PhotoCount.getValue ());
Profile_Info.imageScore.setText (SplashScreen.PHOTOCOUNT);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText (NewPost.this, databaseError.getMessage (), Toast.LENGTH_SHORT).show ();
}
});
}
}
如何从应用中删除这些冻结?
每当我使用此algorithm
在compressing
之后上传图片时。它总是冻结UI。