我正在尝试使用Intents从手机的内部存储中选择一个pdf文件。调用startActivityForResult后会弹出一个app选择器。一件奇怪的事情正在后台发生。每当选择器弹出时,我调用意图的当前活动将自动返回到先前的活动(选择器仍然存在),选择pdf文件后没有任何反应,应用程序保持原来的活动。
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
}
}
此代码之前完美运行,但突然间它不再起作用了。我不确定发生了什么。请帮忙!
答案 0 :(得分:0)
要求在方法onOptionsItemSelected
样品
@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;
}