我想将一些音频名称传递给我的Android应用程序中的上一个Activity。我尝试了很多方法。我不知道自己在哪里犯错,我只想将记录的音频名称发送给上一个类。

这是我的代码record_Audio。

btnSaveRecord = (Button)findViewById(R.id.buttonSaveRecord);
            btnSaveRecord.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                   /* Intent intent = new Intent(Record_Audio.this , AddPost.class);
                    intent.putExtra("STRING_I_NEED", newAudioFile);
                    setResult(REQUEST_CODE, intent);
                    finish();*/

                    Intent intent = new Intent(Record_Audio.this, AddPost.class);
                    // Add any data that you wish to send
                    intent.putExtra("DATA", newAudioFile);
                    startActivityForResult(intent, REQUEST_CODE);

                    //Intent intent = new Intent(Record_Audio.this , AddPost.class);
                    //startActivity(intent);
                    //finish();

                }
            });


在以前的代码中,我要接收AudioName = AddPost活动

 protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        try
        {
            if (requestCode == REQUEST_CODE)
            {
                if (resultCode == RESULT_OK)
                {
                    String newString = data.getExtras().getString("STRING_I_NEED");
                    Log.e("newString "," = "+newString);
                    String valueToChange = data.getExtras().getString("DATA");
                    Log.e("valueToChange "," = " + valueToChange);

                }
            }

            if (requestCode == CAMERA_REQUEST)
            {  //Capture Camera Image }

            if (requestCode == RESULT_LOAD_IMG)
            {  // //Capture Gallery Image}

最佳答案

首先,从AddPost活动调用RecordAudio活动时,不能使用startActivity。您需要使用startActivityForResult()

从AddPost调用RecordAudio使用以下代码:

Intent intent=new Intent(AddPost.this, RecordAudio.class);
startActivityForResult(intent, 1010);


在您的RecordAudio文件中

getIntent().putExtra("data", "your data");
setResult(RESULT_OK, getIntent());
finish();


并在AddPost onActivityForResult()中

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==1010 && resultCode==RESULT_OK)
    {
           String newString = data.getExtras().getString("data");
           Log.e("newString "," = "+newString);
    }
}

09-12 23:12