MasayukiSuda / Mp4Composer-android

This library generate an Mp4 movie using Android MediaCodec API and apply filter, scale, trim, transcode, crop, timeScale, mute and rotate Mp4.
MIT License
956 stars 219 forks source link

How to use constructor on android 10,11 #102

Open trongtrong opened 3 years ago

trongtrong commented 3 years ago

Hi, i have a problem when using libary on andoid 10,11. i use this constructor

public GPUMp4Composer descriptor(final FileDescriptor srcFileDescriptor, @NonNull final FileDescriptor destFileDescriptor) { if (Build.VERSION.SDK_INT < 26) { throw new IllegalArgumentException("destFileDescriptor can not use"); } this.srcDataSource = new FileDescriptorDataSource(srcFileDescriptor); this.destPath = null; this.destFileDescriptor = destFileDescriptor; return this; }

I have srcFileDescriptor by method: public static FileDescriptor getSrcFileDescriptior(Context context, Uri uri) { try { ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver().openFileDescriptor(uri, "rw"); return parcelFileDescriptor.getFileDescriptor(); } catch (FileNotFoundException e) { e.printStackTrace(); } return null; }

i use bellow method to get destFileDescriptor :

` public static FileDescriptor getDesFileDescriptior(Context context) {

    Uri uriSavedVideo = null;
    try {
        ContentResolver resolver = context.getContentResolver();
        ContentValues contentValues = new ContentValues();
        contentValues.put(MediaStore.Video.Media.TITLE, System.currentTimeMillis() / 1000);
        contentValues.put(MediaStore.Video.Media.DISPLAY_NAME, System.currentTimeMillis() / 1000);
        contentValues.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
        contentValues.put(MediaStore.Video.Media.DATE_ADDED, System.currentTimeMillis() / 1000);

        Uri collection;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            collection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL);
            contentValues.put(MediaStore.Video.Media.RELATIVE_PATH, Environment.DIRECTORY_DCIM + context.getString(R.string.folder_app));

        } else {
            String videoPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString() + "/" + "test.mp4";
            collection = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            contentValues.put(MediaStore.Video.Media.DATA, videoPath);
        }
        uriSavedVideo = resolver.insert(collection, contentValues);

        return context.getContentResolver().openFileDescriptor(uriSavedVideo , "w").getFileDescriptor();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;`

But when i run, I get the following error:

W/System.err: java.lang.NullPointerException: uri 2021-04-09 10:36:20.285 8431-8431/com.example.textonvideo W/System.err: at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:128) 2021-04-09 10:36:20.285 8431-8431/com.example.textonvideo W/System.err: at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1233) 2021-04-09 10:36:20.285 8431-8431/com.example.textonvideo W/System.err: at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:1106) 2021-04-09 10:36:20.285 8431-8431/com.example.textonvideo W/System.err: at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:1060) 2021-04-09 10:36:20.285 8431-8431/com.example.textonvideo W/System.err: at com.videoart.textonvideo.utils.Utils.getDesFileDescriptior(Utils.java:109) 2021-04-09 10:36:20.286 8431-8431/com.example.textonvideo W/System.err: at com.videoart.textonvideo.services.VideoEditorService.composeMp4(VideoEditorService.java:114) 2021-04-09 10:36:20.286 8431-8431/com.example.textonvideo W/System.err: at com.videoart.textonvideo.services.VideoEditorService.onStartCommand(VideoEditorService.java:63) 2021-04-09 10:36:20.286 8431-8431/com.example.textonvideo W/System.err: at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3693) 2021-04-09 10:36:20.286 8431-8431/com.example.textonvideo W/System.err: at android.app.ActivityThread.-wrap22(Unknown Source:0) 2021-04-09 10:36:20.286 8431-8431/com.example.textonvideo W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1827) 2021-04-09 10:36:20.286 8431-8431/com.example.textonvideo W/System.err: at android.os.Handler.dispatchMessage(Handler.java:106) 2021-04-09 10:36:20.286 8431-8431/com.example.textonvideo W/System.err: at android.os.Looper.loop(Looper.java:164) 2021-04-09 10:36:20.287 8431-8431/com.example.textonvideo W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7000) 2021-04-09 10:36:20.287 8431-8431/com.example.textonvideo W/System.err: at java.lang.reflect.Method.invoke(Native Method) 2021-04-09 10:36:20.287 8431-8431/com.example.textonvideo W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441) 2021-04-09 10:36:20.287 8431-8431/com.example.textonvideo W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

how to solve this problem ?