Last active
February 19, 2021 12:03
-
-
Save daichan4649/5829564 to your computer and use it in GitHub Desktop.
外部レコーダ(音声/動画)起動後に、保存データ(録音/録画)を取得 (Android)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static final int REQ_CODE_MIC = 0; | |
private static final int REQ_CODE_MOVIE = 1; | |
private void startAudioRecorder() { | |
Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION); | |
try { | |
startActivityForResult(intent, REQ_CODE_MIC); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private void startMovieRecorder() { | |
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); | |
try { | |
startActivityForResult(intent, REQ_CODE_MOVIE); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
// 保存先パス取得 | |
if (data == null) { | |
return; | |
} | |
Uri uri = data.getData(); | |
if (uri == null) { | |
return; | |
} | |
final String savePath = getFilePathFromUri(this, uri); | |
// 同階層に別名保存 | |
move(savePath, savePath + "_"); | |
} | |
private static String getFilePathFromUri(Context context, Uri uri) { | |
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null); | |
cursor.moveToFirst(); | |
int index = cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA); | |
return cursor.getString(index); | |
} | |
private static void move(final String fromPath, String toPath) { | |
try { | |
// move(copy+delete) | |
File from = new File(fromPath); | |
File to = new File(toPath); | |
Files.copy(from, to); | |
from.delete(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment