I want to save whatsapp's statuses. By using this code I am able to save images
public void saveImage(Bitmap bitmap, String name) {
OutputStream fileOutputStream;
Uri imageUri;
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentResolver contentResolver = context.getApplicationContext().getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, "" + name);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DCIM + "/V Troid/WhatsApp");
imageUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
fileOutputStream = (FileOutputStream) contentResolver.openOutputStream(Objects.requireNonNull(imageUri));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
Objects.requireNonNull(fileOutputStream);
Toast.makeText(context.getApplicationContext(), "Image Saved", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(context.getApplicationContext(), "Error \n" + e, Toast.LENGTH_SHORT).show();
}
}
But now I want to save video file too and for this I am using this code but its not working for me the file I am getting after this code is corrupted
public void saveVideo(String name, Uri vidUri) {
OutputStream fileOutputStream;
Uri VideoUri;
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentResolver contentResolver = context.getApplicationContext().getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, "" + name);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "video/mp4");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_MOVIES + "/V Troid/WhatsApp");
VideoUri = contentResolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentValues);
fileOutputStream = (FileOutputStream) contentResolver.openOutputStream(Objects.requireNonNull(vidUri));
Uri videoFileUri = contentResolver.insert(vidUri, contentValues);
Objects.requireNonNull(fileOutputStream);
Toast.makeText(context.getApplicationContext(), "Should Work", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(context.getApplicationContext(), "Error in saving\n" + e, Toast.LENGTH_SHORT).show();
}
}
I am targeting Android 11
Try this, I use it for my WhatsApp Status Saver App:
@RequiresApi(api = Build.VERSION_CODES.Q)
private void saveVideoQ(Uri uri3){
Context context = MainActivity.this;
String videoFileName = "video_" + System.currentTimeMillis() + ".mp4";
ContentValues valuesvideos;
valuesvideos = new ContentValues();
valuesvideos.put(MediaStore.Video.Media.RELATIVE_PATH, "Movies/");
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());
valuesvideos.put(MediaStore.Video.Media.IS_PENDING, 1);
ContentResolver resolver = context.getContentResolver();
Uri collection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY); //all video files on primary external storage
Uri uriSavedVideo = resolver.insert(collection, valuesvideos);
ParcelFileDescriptor pfd;
try {
pfd = context.getContentResolver().openFileDescriptor(uriSavedVideo,"w");
assert pfd != null;
FileOutputStream out = new FileOutputStream(pfd.getFileDescriptor());
// Get the already saved video as fileinputstream from here
InputStream in = getContentResolver().openInputStream(uri3);
byte[] buf = new byte[8192];
int len;
int progress = 0;
while ((len = in.read(buf)) > 0) {
progress = progress + len;
out.write(buf, 0, len);
}
out.close();
in.close();
pfd.close();
valuesvideos.clear();
valuesvideos.put(MediaStore.Video.Media.IS_PENDING, 0);
valuesvideos.put(MediaStore.Video.Media.IS_PENDING, 0); //only your app can see the files until pending is turned into 0
context.getContentResolver().update(uriSavedVideo, valuesvideos, null, null);
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "/Movies/" + videoFileName);
} catch (Exception e) {
Toast.makeText(context, "error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}