Skip to content

Instantly share code, notes, and snippets.

@oluwabajio
Last active September 3, 2021 10:44
Show Gist options
  • Save oluwabajio/b4a17dd6a8544c03f00abd70f62ded81 to your computer and use it in GitHub Desktop.
Save oluwabajio/b4a17dd6a8544c03f00abd70f62ded81 to your computer and use it in GitHub Desktop.
private void saveVideo(Uri uri) {
String videoFileName = "video_" + System.currentTimeMillis() + ".mp4";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentValues valuesvideos = new ContentValues();
valuesvideos.put(MediaStore.Video.Media.RELATIVE_PATH, Environment.DIRECTORY_MOVIES + "VidFolder");
valuesvideos.put(MediaStore.Video.Media.TITLE, videoFileName);
valuesvideos.put(MediaStore.Video.Media.DISPLAY_NAME, videoFileName);
valuesvideos.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
valuesvideos.put(MediaStore.Video.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
valuesvideos.put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis());
final Uri collection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
Uri uriSavedVideo = getActivity().getContentResolver().insert(collection, valuesvideos);
try {
ParcelFileDescriptor pfd = getActivity().getContentResolver().openFileDescriptor(uriSavedVideo, "w");
FileOutputStream out = new FileOutputStream(pfd.getFileDescriptor());
InputStream inputStream = getActivity().getContentResolver().openInputStream(uri);
byte[] buf = new byte[8192];
int len;
while ((len = inputStream.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
inputStream.close();
pfd.close();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getActivity(), "Error: "+ e.getMessage(), Toast.LENGTH_SHORT).show();
}
valuesvideos.clear();
valuesvideos.put(MediaStore.Video.Media.IS_PENDING, 0);
getActivity().getContentResolver().update(uriSavedVideo, valuesvideos, null, null);
Toast.makeText(getActivity(), "Saved Successfully", Toast.LENGTH_SHORT).show();
}
} else {
File outputDirectory = new File(Environment.getExternalStoragePublicDirectory("") + "/Video/");
if (!outputDirectory.exists()) {
outputDirectory.mkdirs();
}
String outputDir = outputDirectory.getAbsolutePath();
String outputFile = outputDir + "/vid_" + new SimpleDateFormat("yyyyMM_dd-HHmmss").format(new Date()) + ".mp3";
outputAudioPath = outputFile;
}
private void saveAudio(Uri uri) {
String audioFileName = "audio_" + System.currentTimeMillis() + ".mp3";
String outputAudioPath = "";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentValues valuesvideos = new ContentValues();
valuesvideos.put(MediaStore.Audio.Media.RELATIVE_PATH, Environment.DIRECTORY_MUSIC + "/StereoToMono");
valuesvideos.put(MediaStore.Audio.Media.TITLE, audioFileName);
valuesvideos.put(MediaStore.Audio.Media.DISPLAY_NAME, audioFileName);
valuesvideos.put(MediaStore.Audio.Media.MIME_TYPE, "audio/mpeg");
valuesvideos.put(MediaStore.Audio.Media.DATE_TAKEN, System.currentTimeMillis());
Uri outputAudioUri = getActivity().getContentResolver().insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, valuesvideos);
try {
ParcelFileDescriptor pfd = getActivity().getContentResolver().openFileDescriptor(outputAudioUri, "w");
FileDescriptor fd = pfd.getFileDescriptor();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getActivity(), "Error: "+ e.getMessage(), Toast.LENGTH_SHORT).show();
}
} else {
File outputDirectory = new File(Environment.getExternalStoragePublicDirectory("") + "/StereoToMono/");
if (!outputDirectory.exists()) {
outputDirectory.mkdirs();
}
String outputDir = outputDirectory.getAbsolutePath();
String outputFile = outputDir + "/StereoToMono_" + new SimpleDateFormat("yyyyMM_dd-HHmmss").format(new Date()) + ".mp3";
outputAudioPath = outputFile;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment