Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: crash when preview video on some phone(vivo). #270

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.huantansheng.easyphotos.ui.adapter;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.graphics.PointF;
import android.net.Uri;
import android.os.Build;
Expand Down Expand Up @@ -72,7 +75,8 @@ public void onBindViewHolder(@NonNull final PreviewPhotosViewHolder holder, int
holder.ivPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toPlayVideo(v, uri, type);
// toPlayVideo(v, uri, type);
toPlayVideo(v, path, type);
}
});
} else if (path.endsWith(Type.GIF) || type.endsWith(Type.GIF)) {
Expand Down Expand Up @@ -134,6 +138,50 @@ private void toPlayVideo(View v, Uri uri, String type) {
context.startActivity(intent);
}

/**
* play video by file path
*
* @param v
* @param path
* @param type
*/
private void toPlayVideo(View v, String path, String type) {
Uri uri = getUri(v.getContext(), path);
toPlayVideo(v, uri, type);
}

private Uri getUri(Context context, String path) {
try {
// get authority str when runtime
String authority = getAuthority(context);
File file = new File(path);
Uri uri = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(context, authority, new File(path));
} else {
uri = Uri.fromFile(file);
}
return uri;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}

/**
* get authority string when runtime
* ref from https://stackoverflow.com/questions/73508329/referencing-main-app-resources-in-library-modules-in-android-project-inquiry
*
* @param appContext
* @return
* @throws PackageManager.NameNotFoundException
*/
private static String getAuthority(final Context appContext) throws PackageManager.NameNotFoundException {
final ComponentName componentName = new ComponentName(appContext, FileProvider.class.getName());
final ProviderInfo providerInfo = appContext.getPackageManager().getProviderInfo(componentName, 0);
return providerInfo.authority;
}

private Uri getUri(Context context, String path, Intent intent) {
File file = new File(path);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Expand Down