Java 上传到Firebase UI时冻结UI

Java 上传到Firebase UI时冻结UI,java,android,firebase,firebase-storage,Java,Android,Firebase,Firebase Storage,每当我点击发送数据时,它都会成功地将文件上传到firebase存储,但在UI冻结5秒或大约10秒之后 此冻结也不允许出现“进度”对话框 我在这个活动中创建了它 public class NewPost extends AppCompatActivity { public static Bitmap bitmap; public static ImageView imageView1,imageView2; public static CardView card1,car

每当我点击发送数据时,它都会成功地将文件上传到
firebase存储
,但在
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 ();
            }
        });
    }
}
public类NewPost扩展了appcompative活动{
公共静态位图;
公共静态ImageView imageView1、imageView2;
公共静态卡查看卡1、卡2;
公共静态int[]具有_IMAGE={0,0};
公共静态整型加载=0,IMAGE1=0,IMAGE2=0;
私有存储参考mStorageRef;
SharedReferences SharedReferences;
SharedReferences.Editor;
文件文件,文件1;
图像视图背景;
日历;
int done=0;
SimpleDataFormat SimpleDataFormat,simpleTimeFormat;
FirebaseDatabase=FirebaseDatabase.getInstance();
DatabaseReference myRef=database.getReference();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u new\u post);
file=新文件(this.getCacheDir(),“1”);
file1=新文件(this.getCacheDir(),“2”);
imageView1=findViewById(R.id.image);
imageView1.setDrawingCacheEnabled(真);
imageView2=findViewById(R.id.image2);
imageView2.setDrawingCacheEnabled(真);
backk=findviewbyd(R.id.backk);
backk.bringToFront();
backk.setDrawingCacheEnabled(true);
backk.setOnClickListener(新的View.OnClickListener(){
@凌驾
公共void onClick(视图v){
具有_图像[0]=0;
具有_图像[1]=0;
加载=0;
imageView1.setImageResource(R.drawable.person用于新职位);
imageView2.setImageResource(R.drawable.person用于新职位);
完成();
覆盖转换(R.anim.closeup,R.anim.openup);
}
});
card1=findViewById(R.id.card);
card2=findViewById(R.id.card2);
calendar=calendar.getInstance();
SimpleDataFormat=新的SimpleDataFormat(“dd-MM-yyyy”,Locale.getDefault());
simpleTimeFormat=newSimpleDataFormat(“HH:mm”,Locale.getDefault());
mStorageRef=FirebaseStorage.getInstance().getReference();
SharedReferences=GetSharedReferences(“ImageDetails”,MODE_PRIVATE);
editor=SharedReferences.edit();
最终浮动操作按钮sendData=findViewById(R.id.sendData);
sendData.setDrawingCacheEnabled(true);
sendData.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
if(更新(imageView1)和更新(imageView2)){
完成=0;
位图图像1、图像2;
image1=((BitmapDrawable)imageView1.getDrawable()).getBitmap();
image2=((BitmapDrawable)imageView2.getDrawable()).getBitmap();
if(image1!=null&&image2!=null){
位图图像(图像1,“文件”,文件);
位图图像(图像2,“文件1”,文件1);
试一试{
file=new Compressor(NewPost.this).setQuality(75).setMaxWidth(640).setMaxHeight(480).setCompressFormat(Bitmap.CompressFormat.PNG).compressToFile(文件);
file1=new Compressor(NewPost.this).setQuality(75).setMaxWidth(640).setMaxHeight(480).setCompressFormat(Bitmap.CompressFormat.PNG).compressToFile(file1);
}捕获(IOE异常){
Toast.makeText(NewPost.this,e.getMessage(),Toast.LENGTH\u SHORT).show();
}
}否则
Toast.makeText(NewPost.this,“位图为空”,Toast.LENGTH\u SHORT.show();
上传数据(文件“图像”,1);
上传数据(文件1,“图像1”,2);
}否则{
Snackbar.make(findviewbyd(android.R.id.content),“在上传到服务器之前选择图像”,Toast.LENGTH\u SHORT.show();
}
}
});
card1.setOnClickListener(新建View.OnClickListener(){
@凌驾
公共void onClick(视图){
图像1=1;
imagepickerinent();
}
});
card2.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
图像2=1;
imagepickerinent();
}
});
editor.apply();
editor.commit();
}
@凌驾
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
if(requestCode==ConstantsCustomGallery.REQUEST\u CODE&&resultCode==Activity.RESULT\u OK&&data!=null){
ArrayList images=data.getParcelableArrayListXTRA(ConstantCustomGallery.INTENT\u EXTRA\u images);
Uri=Uri.fromFile(新文件(images.get(0.path));
Intent Intent=newintent(getApplicationContext(),CropActivity.class);
intent.putExtra(“uri”,uri);
intent.putExtra(“类型”、“新邮政”);
星触觉(意向);
}否则{
加载=0;
}
}
@凌驾
受保护的空onDestroy(){
editor.apply();
editor.commit();
super.ondestory();
}
@凌驾
受保护的void onStop(){
editor.apply();
editor.commit();
super.onStop();
}
@凌驾
受保护的void onPause(){
editor.apply();
editor.commit();
super.onPause();
覆盖过渡(R.anim.特写,R.anim.op