Android: pasa el camino del archivo entre dos Actividades
En mi aplicación de Android registro un archivo. Wav en este modo:
public class MainActivity extends ActionBarActivity { private static final int RECORDER_BPP = 16; private static final String AUDIO_RECORDER_FILE_EXT_WAV = ".wav"; private static final String AUDIO_RECORDER_FOLDER = "Audio"; private static final String AUDIO_RECORDER_TEMP_FILE = "record_temp.raw"; private static final int RECORDER_SAMPLERATE = 8000; private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO; private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT; private static final String String = null; short[] audioData; private static AudioRecord recorder = null; private static int bufferSize = 0; private Thread recordingThread = null; private boolean isRecording = false; /*Complex[] fftTempArray; Complex[] fftArray;*/ int[] bufferData; int bytesRecorded; TextView tv; private Button ca; File f2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_audio); setButtonHandlers(); enableButtons(false); bufferSize = AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_IN_MONO,AudioFormat.ENCODING_PCM_16BIT); audioData = new short [bufferSize]; //short array that pcm data is put into. tv = (TextView)findViewById(R.id.textView1); ca = (Button)findViewById(R.id.button2); } private void setButtonHandlers() { ((Button)findViewById(R.id.button1)).setOnClickListener(btnClick); ((Button)findViewById(R.id.btStop)).setOnClickListener(btnClick); } private void enableButton(int id,boolean isEnable){ ((Button)findViewById(id)).setEnabled(isEnable); } private void enableButtons(boolean isRecording) { enableButton(R.id.button1,!isRecording); enableButton(R.id.btStop,isRecording); } private String getFilename(){ String filepath = Environment.getExternalStorageDirectory().getPath(); File file = new File(filepath,AUDIO_RECORDER_FOLDER); Calendar c = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy_HHmm"); String strDate = sdf.format(c.getTime()); if(!file.exists()){ file.mkdirs(); } String fileaudio= new String("record"); f2= new File(file.getAbsolutePath() + "/" + fileaudio + "-" +strDate +AUDIO_RECORDER_FILE_EXT_WAV); return (file.getAbsolutePath() + "/" + fileaudio + "-" +strDate +AUDIO_RECORDER_FILE_EXT_WAV); } private String getTempFilename(){ String filepath = Environment.getExternalStorageDirectory().getPath(); File file = new File(filepath,AUDIO_RECORDER_FOLDER); if(!file.exists()){ file.mkdirs(); } File tempFile = new File(filepath,AUDIO_RECORDER_TEMP_FILE); if(tempFile.exists()) tempFile.delete(); return (file.getAbsolutePath() + "/" + AUDIO_RECORDER_TEMP_FILE); } private void startRecording(){ recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,RECORDER_SAMPLERATE, RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING, bufferSize); recorder.startRecording(); isRecording = true; recordingThread = new Thread(new Runnable() { public void run() { writeAudioDataToFile(); } },"Audio Thread"); recordingThread.start(); } private void writeAudioDataToFile(){ ......} private void stopRecording(){ if(null != recorder){ isRecording = false; recorder.stop(); recorder.release(); recorder = null; recordingThread = null; } copyWaveFile(getTempFilename(),getFilename()); } private void deleteTempFile() { File file = new File(getTempFilename()); file.delete(); } private void copyWaveFile(String inFilename,String outFilename){ ...... } private void WriteWaveFileHeader( ...... } private View.OnClickListener btnClick = new View.OnClickListener() { public void onClick(View v) { switch(v.getId()){ case R.id.button1:{ enableButtons(true); startRecording(); ....... } }; };
Ahora, quiero usar este archivo grabado en otra actividad que abro cuando hago clic en un botón. Por esta razón, he creado el método de "cambio" que recuerdo en onclick () del botton. En este método quiero cambiar "activity" y pasar el filepath. Este es mi código:
- Cmd Prompt no detectando la entrada de teclado
- Guardar vista como mapa de bits, sólo obtener pantalla en negro
- Excepción de tiempo de ejecución extra cuando se ejecuta una aplicación de Android en algunos dispositivos
- ¿Qué hace este método privado en esta clase singleton Java?
- Hacer una conexión HTTPS mediante URL.openConnection ()
public void change (View view){ Intent changeActivity; changeActivity = new Intent (this, SecondActivity.class); startActivity(changeActivity); String filepath = Environment.getExternalStorageDirectory().getPath(); Calendar c = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy_HHmm"); String strDate = sdf.format(c.getTime()); File file = new File(filepath,AUDIO_RECORDER_FOLDER); File f2= new File(file.getAbsolutePath() + "/" + "record" + "-" +strDate +AUDIO_RECORDER_FILE_EXT_WAV); String path = f2.getAbsolutePath(); Intent intent = new Intent(MainActivity.this, SecondActivity.class); intent.putExtra("lname", path); startActivity(intent); } }
En la segunda actividad, recuerdo el archivo en este modo:
Intent intent = getIntent(); lName = intent.getStringExtra("lname"); File storage = Environment.getExternalStorageDirectory(); File file = new File(storage,lName);
Este código no funciona bien porque no hay el paso del filepath. ¿Por qué? ¿Alguien me puede ayudar?
- El botón no funciona para AlexKolpa / fab-toolbar
- Http: // un enlace href que no funciona en Android
- La página de vista web de Android no carga correctamente la página
- Recepción de SMS en Android Manifest.xml
- Java MimetypesFileTypeMap siempre devuelve la aplicación / octet-stream en el emulador de Android
- Android: Cómo llamar a getActivity () en OnItemClickListener ()?
- Los websockets de AndroidAsync no funcionan
- Fuentes TrueType en libGDX
Debido a que está enviando la ruta absoluta del archivo de la actividad anterior para que lName
contenga la ruta completa incluyendo el archivo en lugar de sólo el nombre del archivo para crear el archivo usando el constructor de la clase File que toman un argumento como ruta absoluta del archivo:
File file = new File(lName);