Android11或Android如何保存视频文件 Q

How to save video file in Android 11 or Android Q

我想保存 whatsapp 的状态。 通过使用此代码,我可以保存图像

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();
    }
}

但现在我也想保存视频文件,为此我正在使用这段代码,但它对我不起作用,因为这段代码损坏后我得到的文件

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();
        }
    }

我的目标是 Android 11

试试这个,我将它用于我的 WhatsApp Status Saver 应用程序:

 @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();
    }
}