将值从onActivityResult传递给另一个方法

时间:2014-07-25 08:56:49

标签: android android-intent onactivityresult

问题: - 我想将Fullpath传递给uploadfile我想在uploadfile中使用它 所以我试图通过创建对象来捕获它,但它不起作用。如果任何解决方案真的很感激

public void uploadfile(View view){
        edittext = (EditText)findViewById(R.id.txtFile);
/*      Intent sharingIntent = new Intent(Intent.ACTION_SEND);
*/      if(edittext != null)
        {
        Intent intent1 = new Intent(this, CreateDB.class);
        startActivity(intent1);     
        }
        else
        {
            Toast.makeText(NewMessage.this, "No File Selected", 2000).show();
        }
    }
 // Listen for results.
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        // See which child activity is calling us back.
        if (requestCode == REQUEST_PATH){
            if (resultCode == RESULT_OK) { 
                curPathName = data.getStringExtra("GetPath"); 
                curFileName = data.getStringExtra("GetFileName");
                FullPath = curPathName+"/"+curFileName;
                edittext.setText(curFileName);

/*              Toast.makeText(NewMessage.this, resId, duration);*/
            }
         }
    }

2 个答案:

答案 0 :(得分:1)

请尝试这种方式,希望这有助于您解决问题。

在onCreate()

中移动EditText初始化
edittext = (EditText)findViewById(R.id.txtFile); 

public void uploadfile(String fullPath) {
    // write your upload file code here
}

public void onActivityResult(int requestCode, int resultCode, Intent data){
   if (requestCode == REQUEST_PATH){
      if (resultCode == RESULT_OK) {
         if(data.getStringExtra("GetPath")!=null && data.getStringExtra("GetFileName")!=null){
            edittext.setText(data.getStringExtra("GetFileName"));
            uploadfile(data.getStringExtra("GetPath")+"/"+data.getStringExtra("GetFileName"));
          }else{
            Toast.makeText(NewMessage.this, "No File Selected", 2000).show();
          }
      }
   }
}

答案 1 :(得分:0)

将您的uploadfile参数从View更改为String并删除edittext。

public void uploadfile(String fullPath){
    //do something
}

然后将其添加到onActivityResult方法

if(!TextUtil.isEmpty(curFileName)){
    tv.setText(curFileName); // show curFileName in textView
    uploadfile(fullPath); // pass fullPath to uploadfile
}else{
    // show Toast, curFileName is empty
}

ps:tv是你的textView