Issue when I try to compress a video in Android API 33
See original GitHub issueHello, I am having an issue when I try to compress a video in Android API 33, this is the logcat:
Process: com.spotbros.defense, PID: 6652
java.io.FileNotFoundException: : open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:574)
at java.io.FileInputStream.<init>(FileInputStream.java:160)
at com.abedelazizshe.lightcompressorlibrary.VideoCompressor.saveVideoInExternal(VideoCompressor.kt:255)
at com.abedelazizshe.lightcompressorlibrary.VideoCompressor.saveVideoFile(VideoCompressor.kt:182)
at com.abedelazizshe.lightcompressorlibrary.VideoCompressor.access$saveVideoFile(VideoCompressor.kt:26)
at com.abedelazizshe.lightcompressorlibrary.VideoCompressor$doVideoCompression$1.invokeSuspend(VideoCompressor.kt:104)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7898)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Linux.open(Native Method)
at libcore.io.ForwardingOs.open(ForwardingOs.java:563)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:274)
at libcore.io.ForwardingOs.open(ForwardingOs.java:563)
at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7784)
at libcore.io.IoBridge.open(IoBridge.java:560)
at java.io.FileInputStream.<init>(FileInputStream.java:160)
at com.abedelazizshe.lightcompressorlibrary.VideoCompressor.saveVideoInExternal(VideoCompressor.kt:255)
at com.abedelazizshe.lightcompressorlibrary.VideoCompressor.saveVideoFile(VideoCompressor.kt:182)
at com.abedelazizshe.lightcompressorlibrary.VideoCompressor.access$saveVideoFile(VideoCompressor.kt:26)
at com.abedelazizshe.lightcompressorlibrary.VideoCompressor$doVideoCompression$1.invokeSuspend(VideoCompressor.kt:104)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7898)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
I am using a video saved in standar video directory from Android Emulator, uri: /storage/emulated/0/Movies/VID_20220830_180410.mp4, and in this part of the library code:
private fun getMediaPath(context: Context, uri: Uri): String {
val resolver = context.contentResolver
val projection = arrayOf(MediaStore.Video.Media.DATA)
var cursor: Cursor? = null
try {
cursor = resolver.query(uri, projection, null, null, null)
return if (cursor != null) {
val columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA)
cursor.moveToFirst()
cursor.getString(columnIndex)
} else ""
} catch (e: Exception) {
resolver.let {
val filePath = (context.applicationInfo.dataDir + File.separator
+ System.currentTimeMillis())
val file = File(filePath)
resolver.openInputStream(uri)?.use { inputStream ->
FileOutputStream(file).use { outputStream ->
val buf = ByteArray(4096)
var len: Int
while (inputStream.read(buf).also { len = it } > 0) outputStream.write(
buf,
0,
len
)
}
}
return file.absolutePath
}
} finally {
cursor?.close()
}
}
When this part of the code is executing:
cursor = resolver.query(uri, projection, null, null, null)
return if (cursor != null) {
val columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA)
cursor.moveToFirst()
cursor.getString(columnIndex)
} else ""
cursor is always null, so it will return always “” and for this reason I am experimenting this error.
Any help?
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:11 (1 by maintainers)
Top Results From Across the Web
Android Compress Video before Upload to Server
I want to compress the video and re-encode it with a lower bit-rate or resolution. The idea is to get a standard 360х480,...
Read more >Issues · AbedElazizShe/LightCompressor - GitHub
A powerful and easy-to-use video compression library for android uses MediaCodec API. - Issues · AbedElazizShe/LightCompressor.
Read more >Any good example of compressing videos on Android? - MSDN
I need to upload videos to a remote server, but they are too big; thus, I'd like to compress them. I've looked into...
Read more >Troubleshoot known issues with Android Emulator
This page lists known issues, workarounds, and troubleshooting tips for the Android Emulator. If you encounter an issue not listed here or ...
Read more >MediaCodec - Android Developers
For video types this is normally a single compressed video frame. For audio data this is normally a single access unit (an encoded...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
a workaround can be compressing the file into the app’s internal directory then use something like gallery_saver package to save the file in the external storage
Hi @EbelloImbox thank you for openning the issue and apolgies for the late reply. Was very busy the past 10 months.
This issue is happening due to permission issues as Google has been changing the behaviours a lot when it comes to privacy and media access.
I will release a new version later today with updated README. A new permission should be requested now for API >= 33;
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>