Commit 07ebc2b8 authored by leichao.gao's avatar leichao.gao

跳转到播放器

parent b5f42aee
......@@ -209,7 +209,7 @@
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileProvider"
android:authorities="com.base.filerecoveryrecyclebin.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
......
......@@ -19,6 +19,7 @@ import com.base.filerecoveryrecyclebin.databinding.ActivityRecycleBinDetailBindi
import com.base.filerecoveryrecyclebin.help.BaseActivity
import com.base.filerecoveryrecyclebin.help.KotlinExt.toFormatSize
import com.base.filerecoveryrecyclebin.help.KotlinExt.toFormatTime
import com.base.filerecoveryrecyclebin.help.VideoPlayerUtil
import com.base.filerecoveryrecyclebin.utils.BarUtils
import com.base.filerecoveryrecyclebin.utils.LogEx
import com.bumptech.glide.Glide
......@@ -94,20 +95,9 @@ class RecycleBinDetailActivity : BaseActivity<ActivityRecycleBinDetailBinding>()
binding.ivBofang.setOnClickListener {
recycleBinBean?.let { bean ->
val videoIntent = Intent(Intent.ACTION_VIEW)
val file = File(bean.path)
val binFile = findRecycleBinFile(file.name) ?: File("")
val contentUri = FileProvider.getUriForFile(this, this.applicationContext.packageName + ".fileProvider", binFile)
LogEx.logDebug(TAG, "contentUri=$contentUri")
videoIntent.setDataAndType(contentUri, bean.mimeType)
videoIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
// videoIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
if (videoIntent.resolveActivity(this.packageManager) != null) {
startActivity(Intent.createChooser(videoIntent, "choose play video"))
} else {
Toast.makeText(this, "no app to play video!", Toast.LENGTH_SHORT).show();
}
val binFile = findRecycleBinFile(file.name)
VideoPlayerUtil.playVideo(this,binFile?.absolutePath)
}
}
......
package com.base.filerecoveryrecyclebin.help;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import java.io.File;
import android.os.Build;
import android.util.Log;
import androidx.core.content.FileProvider;
public class VideoPlayerUtil {
/**
* 打开外部视频播放器并播放指定路径的MP4文件。
*
* @param context 应用上下文
* @param videoPath MP4文件的本地路径
*/
public static void playVideo(Context context, String videoPath) {
File videoFile = new File(videoPath);
Log.d("glc",videoPath);
if (!videoFile.exists()) {
throw new IllegalArgumentException("Video file does not exist: " + videoPath);
}
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri videoUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
videoUri = FileProvider.getUriForFile(context, "com.base.filerecoveryrecyclebin.provider", videoFile);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
videoUri = Uri.fromFile(videoFile);
}
intent.setDataAndType(videoUri, "video/mp4");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
// 处理异常,例如提示用户或记录日志
}
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 指定内部存储路径 -->
<files-path name="internal_files" path="."/>
<files-path name="internal_files" path="." />
</paths>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment