a914-gowtham / android-video-trimmer

Helps to trim local videos with compress option on Android applications using Exoplayer 2 and FFmpeg.
Other
362 stars 115 forks source link

cant read file with android api 32 #75

Closed idnael closed 1 year ago

idnael commented 2 years ago

This problem seems to affect Android 32 devices. To produce the error, I just run the demo app in an android emulator with API 32, choose a video from gallery.

From the logs: V/VIDEO_TRIMMER ::: Video path:: content://com.android.externalstorage.documents/document/primary%3Adaniel%2Frsync%2Fcomp--videos%2Froxo.MOV

then the lib call the FileUtils.getRealPath method, which returns: V/VIDEO_TRIMMER ::: VideoUri:: /storage/emulated/0/daniel/rsync/comp--videos/roxo.MOV

The path doesn't exist. The correct should be /storage/1813-340E/fotos/videos/roxo.MOV

This is the error log:

W/System.err: java.lang.IllegalArgumentException: /sdcard/fotos/videos/roxo.MOV does not exist
W/System.err:     at android.media.MediaMetadataRetriever.setDataSource(MediaMetadataRetriever.java:255)
W/System.err:     at android.media.MediaMetadataRetriever.setDataSource(MediaMetadataRetriever.java:355)
W/System.err:     at com.gowtham.library.utils.TrimmerUtils.getDuration(TrimmerUtils.java:58)
W/System.err:     at com.gowtham.library.ui.ActVideoTrimmer.lambda$setDataInView$3$ActVideoTrimmer(ActVideoTrimmer.java:222)
W/System.err:     at com.gowtham.library.ui.ActVideoTrimmer$$ExternalSyntheticLambda2.run(Unknown Source:2)
...
E/ExoPlayerImplInternal: Playback error
      com.google.android.exoplayer2.ExoPlaybackException: Source error
        at com.google.android.exoplayer2.ExoPlayerImplInternal.handleIoException(ExoPlayerImplInternal.java:641)
        at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:613)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.os.HandlerThread.run(HandlerThread.java:67)
...
     Caused by: java.io.FileNotFoundException: /sdcard/fotos/videos/roxo.MOV: open failed: ENOENT (No such file or directory)
        at libcore.io.IoBridge.open(IoBridge.java:575)
        at java.io.RandomAccessFile.<init>(RandomAccessFile.java:289)
        at java.io.RandomAccessFile.<init>(RandomAccessFile.java:152)
        at com.google.android.exoplayer2.upstream.FileDataSource.openLocalFile(FileDataSource.java:178)
        at com.google.android.exoplayer2.upstream.FileDataSource.open(FileDataSource.java:108) 
...

SOLUTION1 I managed to fix the problem like this, but is inneficient: In app/src/main/java/com/gowtham/videotrimmer/MainActivity.java, I copy the video to a temp file, and use the new url:

    private void openTrimActivity(String data) throws IOException {
        Log.v(TAG, "data="+data);
        Uri originalUri = Uri.parse(data);
        File tmpFile = File.createTempFile(
                "video_",
                "." + FilenameUtils.getExtension(originalUri.getPath()),
                getExternalCacheDir()
        );
        IOUtils.copy(getContentResolver().openInputStream(originalUri), new FileOutputStream(tmpFile)); // using apache commons here
        data = Uri.fromFile(tmpFile).toString();
        Log.v(TAG, "data="+data);
        ...

But this is not enough. In library/src/main/java/com/gowtham/library/utils/FileUtils.java, method getRealPath, cursor will be NULL. I had to do add this:

    public static String getRealPath(Context context,Uri uri) {
        String PREFIX="file:///";
        if (uri.toString().startsWith(PREFIX))
            return uri.toString().substring(PREFIX.length()-1);
        ...

Other solutions?

a914-gowtham commented 1 year ago

fixed in v1.7.14 @idnael