2023年7月3日发(作者:)
android微信分享⽂件
* 注意了: 在 app节点下添加 android:requestLegacyExternalStorage="true"否则分享的⽂件⼤⼩会是0 */public class ShareFile { /** * context 是application ⽆法获得所属页⾯的activity * * @param context * @param f */ public static void openFile(Context context, File f) {// boolean b1 = opFileByContent(context, f);// log("openFile() called with: opFileBy = [" + b1 + "]"); boolean b = doShare(context, f); if (!b) { openFileByApp(context, f); } } private static boolean opFileByContent(Context context, File f) { try { Intent intent = new Intent(_GET_CONTENT); gs(_ACTIVITY_NEW_TASK); String extension = eExtensionFromUrl(le(f).toString()); String mimetype = gleton().getMimeTypeFromExtension(extension); f = entFile(); //7.0以上需要 if (_INT >= 24) { gs(_GRANT_READ_URI_PERMISSION); Uri uri = ForFile(context, kageName() + ".ovider", f); aAndType(uri, mimetype); } else { aAndType(le(f), mimetype); } egory(RY_OPENABLE); ctivity(intent); return true; } catch (Exception e) { tackTrace(); } return false; } /** * 先尝试⽤系统的⽅式打开,系统的⽅式未找到,调⽤微信分享 * * @param context * @param f */ private static boolean openFileByApp(Context context, File f) { try { Intent intent = new Intent(_VIEW); gs(_ACTIVITY_NEW_TASK); String extension = eExtensionFromUrl(le(f).toString()); String mimetype = gleton().getMimeTypeFromExtension(extension); //7.0以上需要 if (_INT >= 24) { gs(_GRANT_READ_URI_PERMISSION); Uri uri = ForFile(context, kageName() + ".ovider", f); aAndType(uri, mimetype); } else { aAndType(le(f), mimetype); } ctivity(intent); return true; } catch (Exception e) { tackTrace(); xt(context, "打开⽂件出错,⽆⽀持的应⽤", _LONG).show(); return false; } } } private static boolean doShare(Context context, File f) { if (!(context instanceof Activity)) { /*不是activity*/ return false; } if (!()) { log("⽂件不存在"); xt(context, "⽂件不存在", _LONG).show(); return false; } final IWXAPI api = WXAPI(context, _AppID); erApp(_AppID); if (!pInstalled()) { /*未安装微信*/ return false; } if(()>10*1024*1024){ xt(context, "⽂件超过10M", _LONG).show(); return false; } log("doShare() called with: f = [" + olutePath() + "]");// f = new File(esDir(), "6221bdc_re-shm"); String filePath = null; if (checkVersionValid(context) && checkAndroidNotBelowN()) { // 要与``⾥配置的`authorities`⼀致,假设你的应⽤包名为 Uri contentUri = ForFile(context, kageName() + ".fileprovider", f); // 授权给微信访问路径 // 这⾥填微信包名 riPermission("", contentUri, _GRANT_READ_URI_PERMISSION); filePath = ng(); } else { filePath = olutePath(); } try { Activity activity = (Activity) context; ShareData shareData = new ShareData(); WXFileObject wxMiniProgramObject = new WXFileObject(); ePath(filePath); iniProgramObject(wxMiniProgramObject); re_title(e()); re_desc(e()); (activity, shareData, eSession, new allBack() { @Override public void onComplete() { log("onComplete() called"); } @Override public void onError() { log("onError() called"); } @Override public void onCancel() { log("onCancel() called"); } }); return true; } catch (Exception e) { tackTrace(); } return false; } private static void log(String s) { Log.d("ShareFile", s); } // 判断微信版本是否为7.0.13及以上 private static boolean checkVersionValid(Context context) { IWXAPI api = WXAPI(context, _AppID); IWXAPI api = WXAPI(context, _AppID); return ppSupportAPI() >= 0x27000D00; } // 判断Android版本是否7.0及以上 private static boolean checkAndroidNotBelowN() { return _INT >= N_CODES.N; }}
发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1688383491a129834.html
评论列表(0条)