Issue
I am trying to select a pdf file from my phone's internal storage using Intents. After calling startActivityForResult an app chooser pops up. A strange thing is happening in the background. Whenever the chooser pops up, the current activity from where I called the intent automatically goes back to previous activity (the chooser remains) and after selecting the pdf file nothing happens and the app stays to that previous activity.
public class Download_pdf extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exam_tab);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_add) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent,PICKFILE_REQUEST_CODE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICKFILE_REQUEST_CODE && resultCode == RESULT_OK && data != null && data.getData() != null) {
//upload pdf file
Uri uri = data.getData();
StorageReference filepath=storageRef.child(uri.getLastPathSegment());
UploadTask uploadTask=filepath.putFile(uri);
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//File uploaded
}
}
This code was working perfectly before but suddenly its not working anymore. I am not sure what happened. Please help!
Solution
Requirement add return at method onOptionsItemSelected
Sample
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.action_add){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent,1);
Toast.makeText(this, "select file", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
Answered By - Dungnbhut
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.