Commit 5bc96ef8 authored by zhouteng's avatar zhouteng

修复分享app沙盒文件可能不成功的bug

parent 7768b83a
## 1.0.6
* 修复了android端分享应用沙盒内文件可能出错的bug
* 优化了android端分享时的权限请求逻辑
## 1.0.5 ## 1.0.5
* Fix bugs when sharing videos to some apps like WeChat. * Fix bugs when sharing videos to some apps like WeChat.
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
``` ```
dependencies: dependencies:
share_extend: "^1.0.5" share_extend: "^1.0.6"
``` ```
## 导入 ## 导入
``` ```
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
package="com.zt.shareextend"> package="com.zt.shareextend">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application> <application>
......
...@@ -4,7 +4,7 @@ import android.Manifest; ...@@ -4,7 +4,7 @@ import android.Manifest;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.os.Environment; import android.os.Build;
import java.io.File; import java.io.File;
import java.util.Map; import java.util.Map;
...@@ -69,16 +69,19 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi ...@@ -69,16 +69,19 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi
shareIntent.putExtra(Intent.EXTRA_TEXT, text); shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.setType("text/plain"); shareIntent.setType("text/plain");
} else { } else {
File f = new File(text);
if (!f.exists()) {
throw new IllegalArgumentException("file not exists");
}
if (isPathInExternalStorage(text)) { if (ShareUtils.shouldRequestPermission(text)) {
if (!checkPermisson()) { if (!checkPermisson()) {
requestPermission(); requestPermission();
return; return;
} }
} }
File f = new File(text); Uri uri = ShareUtils.getUriForFile(mRegistrar.context(), f, type);
Uri uri = ShareUtils.getUriForFile(mRegistrar.context(), f);
if ("image".equals(type)) { if ("image".equals(type)) {
shareIntent.setType("image/*"); shareIntent.setType("image/*");
...@@ -99,13 +102,8 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi ...@@ -99,13 +102,8 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi
} }
} }
private boolean isPathInExternalStorage(String path) {
File storagePath = Environment.getExternalStorageDirectory();
return path.startsWith(storagePath.getAbsolutePath());
}
private boolean checkPermisson() { private boolean checkPermisson() {
if (ContextCompat.checkSelfPermission(mRegistrar.context(), Manifest.permission.READ_EXTERNAL_STORAGE) if (ContextCompat.checkSelfPermission(mRegistrar.context(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) { == PackageManager.PERMISSION_GRANTED) {
return true; return true;
} }
...@@ -113,7 +111,7 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi ...@@ -113,7 +111,7 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi
} }
private void requestPermission() { private void requestPermission() {
ActivityCompat.requestPermissions(mRegistrar.activity(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, CODE_ASK_PERMISSION); ActivityCompat.requestPermissions(mRegistrar.activity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, CODE_ASK_PERMISSION);
} }
@Override @Override
......
...@@ -6,6 +6,7 @@ import android.content.Context; ...@@ -6,6 +6,7 @@ import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.text.TextUtils; import android.text.TextUtils;
...@@ -16,7 +17,7 @@ import androidx.core.content.FileProvider; ...@@ -16,7 +17,7 @@ import androidx.core.content.FileProvider;
public class ShareUtils { public class ShareUtils {
/// get the uri for file /// get the uri for file
public static Uri getUriForFile(Context context, File file) { public static Uri getUriForFile(Context context, File file, String type) {
String authorities = context.getPackageName() + ".fileprovider"; String authorities = context.getPackageName() + ".fileprovider";
...@@ -26,7 +27,11 @@ public class ShareUtils { ...@@ -26,7 +27,11 @@ public class ShareUtils {
uri = Uri.fromFile(file); uri = Uri.fromFile(file);
} else { } else {
// 使用 FileProvider 会在某些 app 下不支持(在使用FileProvider 方式情况下QQ不能支持图片、视频分享,微信不支持视频分享) // 使用 FileProvider 会在某些 app 下不支持(在使用FileProvider 方式情况下QQ不能支持图片、视频分享,微信不支持视频分享)
uri = FileProvider.getUriForFile(context, authorities, file); uri = FileProvider.getUriForFile(context, authorities, file);
if (!isPathInExternalStorage(file.getAbsolutePath()) || "file".equals(type)) {
return uri;
}
ContentResolver cR = context.getContentResolver(); ContentResolver cR = context.getContentResolver();
if (uri != null && !TextUtils.isEmpty(uri.toString())) { if (uri != null && !TextUtils.isEmpty(uri.toString())) {
...@@ -46,6 +51,15 @@ public class ShareUtils { ...@@ -46,6 +51,15 @@ public class ShareUtils {
return uri; return uri;
} }
public static boolean shouldRequestPermission(String path) {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && isPathInExternalStorage(path);
}
private static boolean isPathInExternalStorage(String path) {
File storagePath = Environment.getExternalStorageDirectory();
return path.startsWith(storagePath.getAbsolutePath());
}
/** /**
* Gets the content:// URI from the given corresponding path to a file * Gets the content:// URI from the given corresponding path to a file
* *
......
name: share_extend name: share_extend
description: A flutter plugin to share text, image, file with system ui. It is compatible with both andorid and ios. description: A flutter plugin to share text, image, file with system ui. It is compatible with both andorid and ios.
version: 1.0.5 version: 1.0.6
author: zhouteng <qfszyq@gmail.com> author: zhouteng <qfszyq@gmail.com>
homepage: https://github.com/zhouteng0217/ShareExtend homepage: https://github.com/zhouteng0217/ShareExtend
......
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