I am trying to upload a photo to firebase storage through external storage.There are 2 types of photo one is exam and other is timetable in my application. How to pass the stringExtra while upload_timetable or upload_exam so that onActivityResult()
I can make out it is from upload_timetable or upload_exam.
I have implemented this way. But if I click on upload_timetable or upload_exam I always get error in toast message.Help me please
code where I pass string
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id)
{
case android.R.id.home:
this.finish();
return true;
case R.id.upload_timetable:
if(admin && isStoragePermissionGranted()) {
Intent intents = new Intent(Intent.ACTION_PICK);
intents.setType("image/*");
intents.putExtra(sExamType,"timetable");
setResult(RESULT_OK,intents);
startActivityForResult(intents, GALLERY_INTENT);
}
else
{
Toast.makeText(getApplicationContext(),"Action denied",Toast.LENGTH_SHORT).show();
}
return true;
case R.id.view_photo:
mStorageRef.child(college).child(branch).child(year).child(section).child("timetable").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
// Got the download URL for 'users/me/profile.png'
showPhoto(uri);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
Toast.makeText(getApplicationContext(),"Photo Doesnt Exists!",Toast.LENGTH_SHORT).show();
}
});
return true;
case R.id.view_exam:
mStorageRef.child(college).child(branch).child(year).child(section).child("exam").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
// Got the download URL for 'users/me/profile.png'
showPhoto(uri);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
Toast.makeText(getApplicationContext(),"Photo Doesnt Exists!",Toast.LENGTH_SHORT).show();
}
});
return true;
case R.id.upload_exam:
if(admin && isStoragePermissionGranted()) {
Intent intentsd = new Intent(Intent.ACTION_PICK);
intentsd.putExtra(sExamType,"exam");
intentsd.setType("image/*");
setResult(RESULT_OK,intentsd);
startActivityForResult(intentsd, GALLERY_INTENT);
}
else
{
Toast.makeText(getApplicationContext(),"Action denied",Toast.LENGTH_SHORT).show();
}
return true;
}
return super.onOptionsItemSelected(item);
}
Code where I retrieve string that is passed
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK)
{
String sTypes = data.getStringExtra(sExamType);
StorageReference reference;
if(TextUtils.equals(sTypes,"timetable") && !TextUtils.isEmpty(sTypes) {
reference = mStorageRef.child(college).child(branch).child(year).child(section).child("timetable");
}else if(TextUtils.equals(sTypes,"timetable") && !TextUtils.isEmpty(sTypes)
{
reference = mStorageRef.child(college).child(branch).child(year).child(section).child("exam");
}else
{
Toast.makeText(getApplicationContext(),"error",Toast.LENGTH_SHORT).show():
}
progressDialog.setMessage("Image Uploading....");
progressDialog.show();
Uri uri = data.getData();
reference.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(getApplicationContext(),"Photo Uploaded!!!",Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(),"Unable to Upload!!!",Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
});
}
}
Instead having only one request code(GALLERY_INTENT
) for both startActivityForResult
, create 2 different code, so that in onActivityResult
you will get that and you can verify.
Create 2 request code
private static final int GALLERY_INTENT_TIME_TABLE = 201;
private static final int GALLERY_INTENT_EXAM = 202;
Call for time table
Intent intents = new Intent(Intent.ACTION_PICK);
intents.setType("image/*");
intents.putExtra(sExamType,"timetable");
setResult(RESULT_OK,intents);
startActivityForResult(intents, GALLERY_INTENT_TIME_TABLE);
For Exam
Intent intentsd = new Intent(Intent.ACTION_PICK);
intentsd.putExtra(sExamType,"exam");
intentsd.setType("image/*");
setResult(RESULT_OK,intentsd);
startActivityForResult(intentsd, GALLERY_INTENT_EXAM);
onActivityResult code
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ((requestCode == GALLERY_INTENT_TIME_TABLE || requestCode == GALLERY_INTENT_EXAM) && resultCode == RESULT_OK) {
if(requestCode == GALLERY_INTENT_TIME_TABLE) {
//do timetable operation
} else if(requestCode == GALLERY_INTENT_EXAM) {
//do exam operation
}
}
}