Commit 5bc96ef8 authored by zhouteng's avatar zhouteng

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

parent 7768b83a
## 1.0.6
* 修复了android端分享应用沙盒内文件可能出错的bug
* 优化了android端分享时的权限请求逻辑
## 1.0.5
* Fix bugs when sharing videos to some apps like WeChat.
......
......@@ -5,7 +5,7 @@
```
dependencies:
share_extend: "^1.0.5"
share_extend: "^1.0.6"
```
## 导入
```
......
......@@ -2,6 +2,7 @@
package="com.zt.shareextend">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application>
......
......@@ -4,7 +4,7 @@ import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.os.Build;
import java.io.File;
import java.util.Map;
......@@ -69,16 +69,19 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.setType("text/plain");
} else {
File f = new File(text);
if (!f.exists()) {
throw new IllegalArgumentException("file not exists");
}
if (isPathInExternalStorage(text)) {
if (ShareUtils.shouldRequestPermission(text)) {
if (!checkPermisson()) {
requestPermission();
return;
}
}
File f = new File(text);
Uri uri = ShareUtils.getUriForFile(mRegistrar.context(), f);
Uri uri = ShareUtils.getUriForFile(mRegistrar.context(), f, type);
if ("image".equals(type)) {
shareIntent.setType("image/*");
......@@ -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() {
if (ContextCompat.checkSelfPermission(mRegistrar.context(), Manifest.permission.READ_EXTERNAL_STORAGE)
if (ContextCompat.checkSelfPermission(mRegistrar.context(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
return true;
}
......@@ -113,7 +111,7 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi
}
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
......
......@@ -6,6 +6,7 @@ import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
......@@ -16,7 +17,7 @@ import androidx.core.content.FileProvider;
public class ShareUtils {
/// 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";
......@@ -26,7 +27,11 @@ public class ShareUtils {
uri = Uri.fromFile(file);
} else {
// 使用 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();
if (uri != null && !TextUtils.isEmpty(uri.toString())) {
......@@ -46,6 +51,15 @@ public class ShareUtils {
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
*
......
name: share_extend
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>
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