Sir,I am making an chat app where I want to pick an image and upload it on database.for this i used intent and startActivityForResults to open a gallery then then selects required image and finally handle the data in onActivityResults.this works well for the first time but when i perform the same operation second time app stops responding.Here is my code
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
startActivityForResult(intent,1);
}
});
and handle code in same activity:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
Toast.makeText(this, "hello", Toast.LENGTH_LONG)
.show();
// When an Image is picked
if (requestCode == 1 && resultCode == RESULT_OK&& data!=null) {
// Get the Image from data
Uri selectedImageURI = data.getData();
UploadTask uploadTask = storageRef.child("images/" + "IMG" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())).putFile(selectedImageURI);
uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
Toast.makeText(Chat.this, ""+progress, Toast.LENGTH_LONG)
.show();
}
}).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
@Override
public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
System.out.println("Upload is paused");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
Toast.makeText(Chat.this, "You haven't failure Image",
Toast.LENGTH_LONG).show();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Handle successful uploads on complete
Uri downloadUrl = taskSnapshot.getMetadata().getDownloadUrl();
Toast.makeText(Chat.this, "success", Toast.LENGTH_LONG).show();
}
});
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
there is no logcat error as app doesn't crashes.it just stops responding with a dialog box asking wait or stop.Please Help!!!
Aucun commentaire:
Enregistrer un commentaire